357 FuncCallContext *funcctx;
366 if (SRF_IS_FIRSTCALL()) {
367 MemoryContext oldcontext;
374 bool exclude_nodata_value =
TRUE;
375 bool nocolumnx =
FALSE;
389 funcctx = SRF_FIRSTCALL_INIT();
392 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
394 if (PG_ARGISNULL(0)) {
395 MemoryContextSwitchTo(oldcontext);
396 SRF_RETURN_DONE(funcctx);
398 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
404 nband = PG_GETARG_INT32(1);
412 bounds[0] = PG_GETARG_INT32(2);
413 bounds[1] = bounds[0];
420 bounds[2] = PG_GETARG_INT32(3);
421 bounds[3] = bounds[2];
425 if (!PG_ARGISNULL(4))
426 exclude_nodata_value = PG_GETARG_BOOL(4);
430 PG_FREE_IF_COPY(pgraster, 0);
432 errcode(ERRCODE_OUT_OF_MEMORY),
433 errmsg(
"Could not deserialize raster")
435 MemoryContextSwitchTo(oldcontext);
436 SRF_RETURN_DONE(funcctx);
441 elog(NOTICE,
"Raster is empty. Returning NULL");
443 PG_FREE_IF_COPY(pgraster, 0);
444 MemoryContextSwitchTo(oldcontext);
445 SRF_RETURN_DONE(funcctx);
454 if (nband < 1 || nband > numbands) {
455 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
457 PG_FREE_IF_COPY(pgraster, 0);
458 MemoryContextSwitchTo(oldcontext);
459 SRF_RETURN_DONE(funcctx);
464 elog(NOTICE,
"Could not find band at index %d. Returning NULL",
nband);
466 PG_FREE_IF_COPY(pgraster, 0);
467 MemoryContextSwitchTo(oldcontext);
468 SRF_RETURN_DONE(funcctx);
472 exclude_nodata_value =
FALSE;
485 bounds[0], bounds[1], bounds[2], bounds[3]);
489 for (
y = bounds[2];
y <= bounds[3];
y++) {
491 for (
x = bounds[0];
x <= bounds[1];
x++) {
499 for (i = 0; i < pixcount; i++)
501 if (pixcount) pfree(pix);
505 PG_FREE_IF_COPY(pgraster, 0);
507 MemoryContextSwitchTo(oldcontext);
508 elog(ERROR,
"RASTER_getPixelPolygons: Could not get pixel value");
509 SRF_RETURN_DONE(funcctx);
513 if (isnodata && exclude_nodata_value) {
514 POSTGIS_RT_DEBUG(5,
"pixel value is NODATA and exclude_nodata_value = TRUE");
522 for (i = 0; i < pixcount; i++)
524 if (pixcount) pfree(pix);
528 PG_FREE_IF_COPY(pgraster, 0);
530 MemoryContextSwitchTo(oldcontext);
531 elog(ERROR,
"RASTER_getPixelPolygons: Could not get pixel polygon");
532 SRF_RETURN_DONE(funcctx);
536 pix = palloc(
sizeof(
struct rt_pixel_t) * (pixcount + 1));
538 pix = repalloc(pix,
sizeof(
struct rt_pixel_t) * (pixcount + 1));
544 PG_FREE_IF_COPY(pgraster, 0);
546 MemoryContextSwitchTo(oldcontext);
547 elog(ERROR,
"RASTER_getPixelPolygons: Could not allocate memory for storing pixel polygons");
548 SRF_RETURN_DONE(funcctx);
563 if (exclude_nodata_value)
564 pix[pixcount].
nodata = isnodata;
569 pix[pixcount].
nodata = isnodata;
578 PG_FREE_IF_COPY(pgraster, 0);
582 elog(NOTICE,
"No pixels found for band %d",
nband);
583 MemoryContextSwitchTo(oldcontext);
584 SRF_RETURN_DONE(funcctx);
588 funcctx->user_fctx = pix;
591 funcctx->max_calls = pixcount;
595 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
597 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
598 errmsg(
"function returning record called in context that cannot accept type record")
602 BlessTupleDesc(tupdesc);
603 funcctx->tuple_desc = tupdesc;
605 MemoryContextSwitchTo(oldcontext);
609 funcctx = SRF_PERCALL_SETUP();
611 call_cntr = funcctx->call_cntr;
612 max_calls = funcctx->max_calls;
613 tupdesc = funcctx->tuple_desc;
614 pix2 = funcctx->user_fctx;
617 if (call_cntr < max_calls) {
624 size_t gser_size = 0;
634 values[0] = PointerGetDatum(gser);
635 if (pix2[call_cntr].nodata)
638 values[1] = Float8GetDatum(pix2[call_cntr].
value);
639 values[2] = Int32GetDatum(pix2[call_cntr].
x);
640 values[3] = Int32GetDatum(pix2[call_cntr].
y);
643 tuple = heap_form_tuple(tupdesc, values, nulls);
646 result = HeapTupleGetDatum(tuple);
648 SRF_RETURN_NEXT(funcctx, result);
653 SRF_RETURN_DONE(funcctx);
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
void lwgeom_free(LWGEOM *geom)
void lwpoly_free(LWPOLY *poly)
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
LWPOLY * rt_raster_pixel_as_polygon(rt_raster raster, int x, int y)
Get a raster pixel as a polygon.
void rt_band_destroy(rt_band band)
Destroy a raster band.
uint16_t rt_raster_get_num_bands(rt_raster raster)
uint16_t rt_raster_get_height(rt_raster raster)
uint16_t rt_raster_get_width(rt_raster raster)
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)