354 FuncCallContext *funcctx;
363 if (SRF_IS_FIRSTCALL()) {
364 MemoryContext oldcontext;
371 bool exclude_nodata_value =
TRUE;
372 bool nocolumnx =
FALSE;
386 funcctx = SRF_FIRSTCALL_INIT();
389 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
391 if (PG_ARGISNULL(0)) {
392 MemoryContextSwitchTo(oldcontext);
393 SRF_RETURN_DONE(funcctx);
395 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
401 nband = PG_GETARG_INT32(1);
409 bounds[0] = PG_GETARG_INT32(2);
410 bounds[1] = bounds[0];
417 bounds[2] = PG_GETARG_INT32(3);
418 bounds[3] = bounds[2];
422 if (!PG_ARGISNULL(4))
423 exclude_nodata_value = PG_GETARG_BOOL(4);
427 PG_FREE_IF_COPY(pgraster, 0);
429 errcode(ERRCODE_OUT_OF_MEMORY),
430 errmsg(
"Could not deserialize raster")
432 MemoryContextSwitchTo(oldcontext);
433 SRF_RETURN_DONE(funcctx);
438 elog(NOTICE,
"Raster is empty. Returning NULL");
440 PG_FREE_IF_COPY(pgraster, 0);
441 MemoryContextSwitchTo(oldcontext);
442 SRF_RETURN_DONE(funcctx);
451 if (nband < 1 || nband > numbands) {
452 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
454 PG_FREE_IF_COPY(pgraster, 0);
455 MemoryContextSwitchTo(oldcontext);
456 SRF_RETURN_DONE(funcctx);
461 elog(NOTICE,
"Could not find band at index %d. Returning NULL",
nband);
463 PG_FREE_IF_COPY(pgraster, 0);
464 MemoryContextSwitchTo(oldcontext);
465 SRF_RETURN_DONE(funcctx);
469 exclude_nodata_value =
FALSE;
482 bounds[0], bounds[1], bounds[2], bounds[3]);
486 for (
y = bounds[2];
y <= bounds[3];
y++) {
488 for (
x = bounds[0];
x <= bounds[1];
x++) {
496 for (i = 0; i < pixcount; i++)
498 if (pixcount) pfree(pix);
502 PG_FREE_IF_COPY(pgraster, 0);
504 MemoryContextSwitchTo(oldcontext);
505 elog(ERROR,
"RASTER_getPixelPolygons: Could not get pixel value");
506 SRF_RETURN_DONE(funcctx);
510 if (isnodata && exclude_nodata_value) {
511 POSTGIS_RT_DEBUG(5,
"pixel value is NODATA and exclude_nodata_value = TRUE");
519 for (i = 0; i < pixcount; i++)
521 if (pixcount) pfree(pix);
525 PG_FREE_IF_COPY(pgraster, 0);
527 MemoryContextSwitchTo(oldcontext);
528 elog(ERROR,
"RASTER_getPixelPolygons: Could not get pixel polygon");
529 SRF_RETURN_DONE(funcctx);
533 pix = palloc(
sizeof(
struct rt_pixel_t) * (pixcount + 1));
535 pix = repalloc(pix,
sizeof(
struct rt_pixel_t) * (pixcount + 1));
541 PG_FREE_IF_COPY(pgraster, 0);
543 MemoryContextSwitchTo(oldcontext);
544 elog(ERROR,
"RASTER_getPixelPolygons: Could not allocate memory for storing pixel polygons");
545 SRF_RETURN_DONE(funcctx);
560 if (exclude_nodata_value)
561 pix[pixcount].
nodata = isnodata;
566 pix[pixcount].
nodata = isnodata;
575 PG_FREE_IF_COPY(pgraster, 0);
579 elog(NOTICE,
"No pixels found for band %d",
nband);
580 MemoryContextSwitchTo(oldcontext);
581 SRF_RETURN_DONE(funcctx);
585 funcctx->user_fctx = pix;
588 funcctx->max_calls = pixcount;
592 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
594 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
595 errmsg(
"function returning record called in context that cannot accept type record")
599 BlessTupleDesc(tupdesc);
600 funcctx->tuple_desc = tupdesc;
602 MemoryContextSwitchTo(oldcontext);
606 funcctx = SRF_PERCALL_SETUP();
608 call_cntr = funcctx->call_cntr;
609 max_calls = funcctx->max_calls;
610 tupdesc = funcctx->tuple_desc;
611 pix2 = funcctx->user_fctx;
614 if (call_cntr < max_calls) {
621 size_t gser_size = 0;
631 values[0] = PointerGetDatum(gser);
632 if (pix2[call_cntr].nodata)
635 values[1] = Float8GetDatum(pix2[call_cntr].
value);
636 values[2] = Int32GetDatum(pix2[call_cntr].
x);
637 values[3] = Int32GetDatum(pix2[call_cntr].
y);
640 tuple = heap_form_tuple(tupdesc, values, nulls);
643 result = HeapTupleGetDatum(tuple);
645 SRF_RETURN_NEXT(funcctx, result);
650 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,...)