PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ RASTER_getPixelValue()

Datum RASTER_getPixelValue ( PG_FUNCTION_ARGS  )

Definition at line 77 of file rtpg_pixel.c.

78{
79 rt_pgraster *pgraster = NULL;
80 rt_raster raster = NULL;
81 rt_band band = NULL;
82 double pixvalue = 0;
83 int32_t bandindex = 0;
84 int32_t x = 0;
85 int32_t y = 0;
86 int result = 0;
87 bool exclude_nodata_value = TRUE;
88 int isnodata = 0;
89
90 /* Index is 1-based */
91 bandindex = PG_GETARG_INT32(1);
92 if ( bandindex < 1 ) {
93 elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
94 PG_RETURN_NULL();
95 }
96
97 x = PG_GETARG_INT32(2);
98
99 y = PG_GETARG_INT32(3);
100
101 exclude_nodata_value = PG_GETARG_BOOL(4);
102
103 POSTGIS_RT_DEBUGF(3, "Pixel coordinates (%d, %d)", x, y);
104
105 /* Deserialize raster */
106 if (PG_ARGISNULL(0)) PG_RETURN_NULL();
107 pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
108
110 if (!raster) {
111 PG_FREE_IF_COPY(pgraster, 0);
112 elog(ERROR, "RASTER_getPixelValue: Could not deserialize raster");
113 PG_RETURN_NULL();
114 }
115
116 /* Fetch Nth band using 0-based internal index */
117 band = rt_raster_get_band(raster, bandindex - 1);
118 if (! band) {
119 elog(NOTICE, "Could not find raster band of index %d when getting pixel "
120 "value. Returning NULL", bandindex);
121 rt_raster_destroy(raster);
122 PG_FREE_IF_COPY(pgraster, 0);
123 PG_RETURN_NULL();
124 }
125 /* Fetch pixel using 0-based coordinates */
126 result = rt_band_get_pixel(band, x - 1, y - 1, &pixvalue, &isnodata);
127
128 /* If the result is -1 or the value is nodata and we take nodata into account
129 * then return nodata = NULL */
130 if (result != ES_NONE || (exclude_nodata_value && isnodata)) {
131 rt_raster_destroy(raster);
132 PG_FREE_IF_COPY(pgraster, 0);
133 PG_RETURN_NULL();
134 }
135
136 rt_raster_destroy(raster);
137 PG_FREE_IF_COPY(pgraster, 0);
138
139 PG_RETURN_FLOAT8(pixvalue);
140}
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition cu_print.c:267
#define TRUE
Definition dbfopen.c:73
#define FALSE
Definition dbfopen.c:72
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
Definition rt_band.c:1551
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition rt_raster.c:86
@ ES_NONE
Definition librtcore.h:182
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition rt_raster.c:385
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition rtrowdump.py:125
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition rtpostgis.h:69
Struct definitions.
Definition librtcore.h:2452

References ES_NONE, FALSE, POSTGIS_RT_DEBUGF, result, rt_band_get_pixel(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_band(), and TRUE.

Here is the call graph for this function: