205 FuncCallContext *funcctx;
222 if (SRF_IS_FIRSTCALL()) {
223 MemoryContext oldcontext;
229 bool exclude_nodata_value =
TRUE;
242 funcctx = SRF_FIRSTCALL_INIT();
245 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
248 if (PG_ARGISNULL(0)) {
249 MemoryContextSwitchTo(oldcontext);
250 SRF_RETURN_DONE(funcctx);
252 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
256 PG_FREE_IF_COPY(pgraster, 0);
258 errcode(ERRCODE_OUT_OF_MEMORY),
259 errmsg(
"Could not deserialize raster")
261 MemoryContextSwitchTo(oldcontext);
262 SRF_RETURN_DONE(funcctx);
279 elog(NOTICE,
"Raster provided has no bands");
281 PG_FREE_IF_COPY(pgraster, 0);
282 MemoryContextSwitchTo(oldcontext);
283 SRF_RETURN_DONE(funcctx);
290 PG_FREE_IF_COPY(pgraster, 0);
291 MemoryContextSwitchTo(oldcontext);
292 elog(ERROR,
"RASTER_dumpValues: Could not initialize argument structure");
293 SRF_RETURN_DONE(funcctx);
297 if (!PG_ARGISNULL(1)) {
298 array = PG_GETARG_ARRAYTYPE_P(1);
299 etype = ARR_ELEMTYPE(array);
300 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
309 PG_FREE_IF_COPY(pgraster, 0);
310 MemoryContextSwitchTo(oldcontext);
311 elog(ERROR,
"RASTER_dumpValues: Invalid data type for band indexes");
312 SRF_RETURN_DONE(funcctx);
316 deconstruct_array(array, etype, typlen, typbyval, typalign, &e, &nulls, &(arg1->
numbands));
319 if (arg1->
nbands == NULL) {
322 PG_FREE_IF_COPY(pgraster, 0);
323 MemoryContextSwitchTo(oldcontext);
324 elog(ERROR,
"RASTER_dumpValues: Could not allocate memory for band indexes");
325 SRF_RETURN_DONE(funcctx);
328 for (i = 0, j = 0; i < arg1->
numbands; i++) {
329 if (nulls[i])
continue;
333 arg1->
nbands[j] = DatumGetInt16(e[i]) - 1;
336 arg1->
nbands[j] = DatumGetInt32(e[i]) - 1;
343 if (j < arg1->numbands) {
345 if (arg1->
nbands == NULL) {
348 PG_FREE_IF_COPY(pgraster, 0);
349 MemoryContextSwitchTo(oldcontext);
350 elog(ERROR,
"RASTER_dumpValues: Could not reallocate memory for band indexes");
351 SRF_RETURN_DONE(funcctx);
358 for (i = 0; i < arg1->
numbands; i++) {
360 elog(NOTICE,
"Band at index %d not found in raster", arg1->
nbands[i] + 1);
363 PG_FREE_IF_COPY(pgraster, 0);
364 MemoryContextSwitchTo(oldcontext);
365 SRF_RETURN_DONE(funcctx);
375 if (arg1->
nbands == NULL) {
378 PG_FREE_IF_COPY(pgraster, 0);
379 MemoryContextSwitchTo(oldcontext);
380 elog(ERROR,
"RASTER_dumpValues: Could not allocate memory for band indexes");
381 SRF_RETURN_DONE(funcctx);
384 for (i = 0; i < arg1->
numbands; i++) {
394 if (!PG_ARGISNULL(2))
395 exclude_nodata_value = PG_GETARG_BOOL(2);
404 PG_FREE_IF_COPY(pgraster, 0);
405 MemoryContextSwitchTo(oldcontext);
406 elog(ERROR,
"RASTER_dumpValues: Could not allocate memory for pixel values");
407 SRF_RETURN_DONE(funcctx);
413 for (z = 0; z < arg1->
numbands; z++) {
423 PG_FREE_IF_COPY(pgraster, 0);
424 MemoryContextSwitchTo(oldcontext);
425 elog(ERROR,
"RASTER_dumpValues: Could not get band at index %d", nband);
426 SRF_RETURN_DONE(funcctx);
432 if (arg1->
values[z] == NULL || arg1->
nodata[z] == NULL) {
435 PG_FREE_IF_COPY(pgraster, 0);
436 MemoryContextSwitchTo(oldcontext);
437 elog(ERROR,
"RASTER_dumpValues: Could not allocate memory for pixel values");
438 SRF_RETURN_DONE(funcctx);
447 for (i = (arg1->
rows * arg1->
columns) - 1; i >= 0; i--)
452 for (y = 0; y < arg1->
rows; y++) {
453 for (x = 0; x < arg1->
columns; x++) {
459 PG_FREE_IF_COPY(pgraster, 0);
460 MemoryContextSwitchTo(oldcontext);
461 elog(ERROR,
"RASTER_dumpValues: Could not pixel (%d, %d) of band %d", x, y, nband);
462 SRF_RETURN_DONE(funcctx);
465 arg1->
values[z][i] = Float8GetDatum(val);
469 if (exclude_nodata_value && isnodata) {
483 PG_FREE_IF_COPY(pgraster, 0);
486 funcctx->user_fctx = arg1;
489 funcctx->max_calls = arg1->
numbands;
492 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
493 MemoryContextSwitchTo(oldcontext);
495 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
497 "function returning record called in context " 498 "that cannot accept type record" 503 BlessTupleDesc(tupdesc);
504 funcctx->tuple_desc = tupdesc;
506 MemoryContextSwitchTo(oldcontext);
510 funcctx = SRF_PERCALL_SETUP();
512 call_cntr = funcctx->call_cntr;
513 max_calls = funcctx->max_calls;
514 tupdesc = funcctx->tuple_desc;
515 arg2 = funcctx->user_fctx;
518 if (call_cntr < max_calls) {
523 ArrayType *mdValues = NULL;
526 int lbound[2] = {1, 1};
533 values[0] = Int32GetDatum(arg2->
nbands[call_cntr] + 1);
536 get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
539 if (arg2->
values[call_cntr] == NULL)
543 mdValues = construct_md_array(
547 typlen, typbyval, typalign
549 values[1] = PointerGetDatum(mdValues);
552 tuple = heap_form_tuple(tupdesc, values, nulls);
553 result = HeapTupleGetDatum(tuple);
555 SRF_RETURN_NEXT(funcctx, result);
560 SRF_RETURN_DONE(funcctx);
int rt_raster_get_num_bands(rt_raster raster)
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
#define POSTGIS_RT_DEBUGF(level, msg,...)
static void rtpg_dumpvalues_arg_destroy(rtpg_dumpvalues_arg arg)
static rtpg_dumpvalues_arg rtpg_dumpvalues_arg_init()
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
int rt_band_clamped_value_is_nodata(rt_band band, double val)
Compare clamped value to band's clamped NODATA value.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
uint16_t rt_raster_get_width(rt_raster raster)
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
uint16_t rt_raster_get_height(rt_raster raster)
#define POSTGIS_RT_DEBUG(level, msg)
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.