666 FuncCallContext *funcctx;
674 if (SRF_IS_FIRSTCALL()) {
675 MemoryContext oldcontext;
681 bool hasband =
FALSE;
682 bool exclude_nodata_value =
TRUE;
683 bool nocolumnx =
TRUE;
697 funcctx = SRF_FIRSTCALL_INIT();
700 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
703 if (PG_ARGISNULL(0)) {
704 MemoryContextSwitchTo(oldcontext);
705 SRF_RETURN_DONE(funcctx);
707 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
710 if (!PG_ARGISNULL(1)) {
711 nband = PG_GETARG_INT32(1);
716 if (!PG_ARGISNULL(2)) {
717 bounds[0] = PG_GETARG_INT32(2);
718 bounds[1] = bounds[0];
723 if (!PG_ARGISNULL(3)) {
724 bounds[2] = PG_GETARG_INT32(3);
725 bounds[3] = bounds[2];
730 if (!PG_ARGISNULL(4))
731 exclude_nodata_value = PG_GETARG_BOOL(4);
736 PG_FREE_IF_COPY(pgraster, 0);
738 errcode(ERRCODE_OUT_OF_MEMORY),
739 errmsg(
"Could not deserialize raster")
741 MemoryContextSwitchTo(oldcontext);
742 SRF_RETURN_DONE(funcctx);
747 elog(NOTICE,
"Raster is empty. Returning empty set");
749 PG_FREE_IF_COPY(pgraster, 0);
750 MemoryContextSwitchTo(oldcontext);
751 SRF_RETURN_DONE(funcctx);
761 if (nband < 1 || nband > numbands) {
762 elog(NOTICE,
"Invalid band index (must use 1-based). Returning empty set");
764 PG_FREE_IF_COPY(pgraster, 0);
765 MemoryContextSwitchTo(oldcontext);
766 SRF_RETURN_DONE(funcctx);
772 elog(NOTICE,
"Could not find band at index %d. Returning empty set",
nband);
775 PG_FREE_IF_COPY(pgraster, 0);
777 MemoryContextSwitchTo(oldcontext);
778 SRF_RETURN_DONE(funcctx);
782 exclude_nodata_value =
FALSE;
795 bounds[0], bounds[1], bounds[2], bounds[3]);
799 for (
y = bounds[2];
y <= bounds[3];
y++) {
801 for (
x = bounds[0];
x <= bounds[1];
x++) {
809 for (i = 0; i < pixcount; i++)
811 if (pixcount) pfree(pix);
815 PG_FREE_IF_COPY(pgraster, 0);
817 MemoryContextSwitchTo(oldcontext);
818 elog(ERROR,
"RASTER_getPixelCentroids: Could not get pixel value");
819 SRF_RETURN_DONE(funcctx);
823 if (isnodata && exclude_nodata_value) {
824 POSTGIS_RT_DEBUG(5,
"pixel value is NODATA and exclude_nodata_value = TRUE");
833 for (i = 0; i < pixcount; i++)
835 if (pixcount) pfree(pix);
839 PG_FREE_IF_COPY(pgraster, 0);
841 MemoryContextSwitchTo(oldcontext);
842 elog(ERROR,
"RASTER_getPixelCentroids: Could not get pixel centroid");
843 SRF_RETURN_DONE(funcctx);
848 pix = palloc(
sizeof(
struct rt_pixel_t) * (pixcount + 1));
851 pix = repalloc(pix,
sizeof(
struct rt_pixel_t) * (pixcount + 1));
858 PG_FREE_IF_COPY(pgraster, 0);
860 MemoryContextSwitchTo(oldcontext);
861 elog(ERROR,
"RASTER_getPixelCentroids: Could not allocate memory for storing pixel centroids");
862 SRF_RETURN_DONE(funcctx);
879 if (exclude_nodata_value)
880 pix[pixcount].
nodata = isnodata;
885 pix[pixcount].
nodata = isnodata;
896 PG_FREE_IF_COPY(pgraster, 0);
900 elog(NOTICE,
"No pixels found for band %d",
nband);
901 MemoryContextSwitchTo(oldcontext);
902 SRF_RETURN_DONE(funcctx);
906 funcctx->user_fctx = pix;
909 funcctx->max_calls = pixcount;
913 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
915 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
916 errmsg(
"function returning record called in context that cannot accept type record")
920 BlessTupleDesc(tupdesc);
921 funcctx->tuple_desc = tupdesc;
923 MemoryContextSwitchTo(oldcontext);
927 funcctx = SRF_PERCALL_SETUP();
929 call_cntr = funcctx->call_cntr;
930 max_calls = funcctx->max_calls;
931 tupdesc = funcctx->tuple_desc;
932 pix2 = funcctx->user_fctx;
935 if (call_cntr < max_calls) {
942 size_t gser_size = 0;
953 values[0] = PointerGetDatum(gser);
954 if (pix2[call_cntr].nodata)
957 values[1] = Float8GetDatum(pix2[call_cntr].
value);
958 values[2] = Int32GetDatum(pix2[call_cntr].
x);
959 values[3] = Int32GetDatum(pix2[call_cntr].
y);
962 tuple = heap_form_tuple(tupdesc, values, nulls);
965 result = HeapTupleGetDatum(tuple);
967 SRF_RETURN_NEXT(funcctx,
result);
972 SRF_RETURN_DONE(funcctx);
char result[OUT_DOUBLE_BUFFER_SIZE]
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
void lwpoint_free(LWPOINT *pt)
void lwgeom_free(LWGEOM *geom)
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.
LWPOINT * rt_raster_pixel_as_centroid_point(rt_raster rast, int x, int y)
Get a raster pixel centroid point.
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,...)