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

◆ RASTER_getPixelValueResample()

Datum RASTER_getPixelValueResample ( PG_FUNCTION_ARGS  )

Definition at line 165 of file rtpg_pixel.c.

166{
167 rt_raster raster = NULL;
168 rt_band band = NULL;
169 rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
170 int32_t bandnum = PG_GETARG_INT32(1);
171 GSERIALIZED *gser;
172 LWPOINT *lwpoint;
173 LWGEOM *lwgeom;
174 bool exclude_nodata_value = PG_GETARG_BOOL(3);
175 rt_resample_type resample_type = RT_NEAREST;
176 double x, y, xr, yr;
177 double pixvalue = 0.0;
178 int isnodata = 0;
179 rt_errorstate err;
180
181 /* Index is 1-based */
182 if (bandnum < 1) {
183 elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
184 PG_RETURN_NULL();
185 }
186
187 gser = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(2));
189 elog(ERROR, "Attempting to get the value of a pixel with a non-point geometry");
190 PG_RETURN_NULL();
191 }
192
194 if (!raster) {
195 elog(ERROR, "RASTER_getPixelValue: Could not deserialize raster");
196 PG_RETURN_NULL();
197 }
198
199 if (gserialized_get_srid(gser) != rt_raster_get_srid(raster)) {
200 elog(ERROR, "Raster and geometry do not have the same SRID");
201 PG_RETURN_NULL();
202 }
203
204 if (PG_NARGS() > 4) {
205 text *resample_text = PG_GETARG_TEXT_P(4);
206 resample_type = resample_text_to_type(resample_text);
207 }
208
209 /* Fetch Nth band using 0-based internal index */
210 band = rt_raster_get_band(raster, bandnum - 1);
211 if (!band) {
212 elog(NOTICE, "Could not find raster band of index %d when getting pixel "
213 "value. Returning NULL", bandnum);
214 PG_RETURN_NULL();
215 }
216
217 /* Get the X/Y coordinates */
218 lwgeom = lwgeom_from_gserialized(gser);
219 lwpoint = lwgeom_as_lwpoint(lwgeom);
220 x = lwpoint_get_x(lwpoint);
221 y = lwpoint_get_y(lwpoint);
222
223 /* Convert X/Y world coordinates into raster coordinates */
224 err = rt_raster_geopoint_to_rasterpoint(raster, x, y, &xr, &yr, NULL);
225 if (err != ES_NONE) {
226 elog(ERROR, "Could not convert world coordinate to raster coordinate");
227 PG_RETURN_NULL();
228 }
229
230 /* Use appropriate resample algorithm */
232 band,
233 xr, yr,
234 resample_type,
235 &pixvalue, &isnodata
236 );
237
238 /* If the result is -1 or the value is nodata and we take nodata into account
239 * then return nodata = NULL */
240 rt_raster_destroy(raster);
241 lwgeom_free(lwgeom);
242 if (err != ES_NONE || (exclude_nodata_value && isnodata)) {
243 PG_RETURN_NULL();
244 }
245 PG_RETURN_FLOAT8(pixvalue);
246}
#define FALSE
Definition dbfopen.c:72
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
void lwgeom_free(LWGEOM *geom)
Definition lwgeom.c:1246
double lwpoint_get_x(const LWPOINT *point)
Definition lwpoint.c:63
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition liblwgeom.h:102
double lwpoint_get_y(const LWPOINT *point)
Definition lwpoint.c:76
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition rt_raster.c:360
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition rt_raster.c:86
rt_errorstate rt_raster_geopoint_to_rasterpoint(rt_raster raster, double xw, double yw, double *xr, double *yr, double *igt)
Convert an xw,yw map point to a xr,yr raster point.
Definition rt_raster.c:730
rt_resample_type
Definition librtcore.h:1478
@ RT_NEAREST
Definition librtcore.h:1479
rt_errorstate
Enum definitions.
Definition librtcore.h:181
@ ES_NONE
Definition librtcore.h:182
rt_errorstate rt_band_get_pixel_resample(rt_band band, double xr, double yr, rt_resample_type resample, double *r_value, int *r_nodata)
Retrieve a point value from the raster using a world coordinate and selected resampling method.
Definition rt_band.c:1396
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
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition lwinline.h:127
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition rtrowdump.py:125
static rt_resample_type resample_text_to_type(text *txt)
Definition rtpg_pixel.c:142
Struct definitions.
Definition librtcore.h:2452

References ES_NONE, FALSE, gserialized_get_srid(), gserialized_get_type(), gserialized_is_empty(), lwgeom_as_lwpoint(), lwgeom_free(), lwgeom_from_gserialized(), lwpoint_get_x(), lwpoint_get_y(), POINTTYPE, resample_text_to_type(), rt_band_get_pixel_resample(), RT_NEAREST, rt_raster_deserialize(), rt_raster_destroy(), rt_raster_geopoint_to_rasterpoint(), rt_raster_get_band(), and rt_raster_get_srid().

Here is the call graph for this function: