1847 bool exclude_nodata_value =
TRUE;
1860 if (PG_ARGISNULL(0))
1862 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1865 PG_FREE_IF_COPY(pgraster, 0);
1866 elog(ERROR,
"RASTER_nearestValue: Could not deserialize raster");
1871 if (!PG_ARGISNULL(1))
1872 bandindex = PG_GETARG_INT32(1);
1874 if (bandindex < 1 || bandindex > num_bands) {
1875 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
1877 PG_FREE_IF_COPY(pgraster, 0);
1882 geom = PG_GETARG_GSERIALIZED_P(2);
1884 elog(NOTICE,
"Geometry provided must be a point");
1886 PG_FREE_IF_COPY(pgraster, 0);
1887 PG_FREE_IF_COPY(geom, 2);
1892 if (!PG_ARGISNULL(3))
1893 exclude_nodata_value = PG_GETARG_BOOL(3);
1897 elog(NOTICE,
"SRIDs of geometry and raster do not match");
1899 PG_FREE_IF_COPY(pgraster, 0);
1900 PG_FREE_IF_COPY(geom, 2);
1907 elog(NOTICE,
"Could not find band at index %d. Returning NULL", bandindex);
1909 PG_FREE_IF_COPY(pgraster, 0);
1910 PG_FREE_IF_COPY(geom, 2);
1918 elog(NOTICE,
"Geometry provided cannot be empty");
1920 PG_FREE_IF_COPY(pgraster, 0);
1921 PG_FREE_IF_COPY(geom, 2);
1942 PG_FREE_IF_COPY(pgraster, 0);
1944 PG_FREE_IF_COPY(geom, 2);
1945 elog(ERROR,
"RASTER_nearestValue: Could not compute pixel coordinates from spatial coordinates");
1956 PG_FREE_IF_COPY(pgraster, 0);
1958 PG_FREE_IF_COPY(geom, 2);
1959 elog(ERROR,
"RASTER_nearestValue: Could not get pixel value for band at index %d", bandindex);
1964 if (!exclude_nodata_value || !isnodata) {
1966 PG_FREE_IF_COPY(pgraster, 0);
1968 PG_FREE_IF_COPY(geom, 2);
1970 PG_RETURN_FLOAT8(
value);
1979 exclude_nodata_value,
1987 elog(NOTICE,
"Could not get the nearest value for band at index %d", bandindex);
1990 elog(NOTICE,
"No nearest value found for band at index %d", bandindex);
1993 PG_FREE_IF_COPY(geom, 2);
1995 PG_FREE_IF_COPY(pgraster, 0);
2003 double lastdist = -1;
2006 for (i = 0; i <
count; i++) {
2011 PG_FREE_IF_COPY(geom, 2);
2013 PG_FREE_IF_COPY(pgraster, 0);
2014 elog(ERROR,
"RASTER_nearestValue: Could not get polygon of neighboring pixel");
2020 if (lastdist < 0 || dist < lastdist) {
2036 PG_FREE_IF_COPY(geom, 2);
2038 PG_FREE_IF_COPY(pgraster, 0);
2041 PG_RETURN_FLOAT8(
value);
int32_t gserialized_get_srid(const GSERIALIZED *s)
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.
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
int lwgeom_ndims(const LWGEOM *geom)
Return the number of dimensions (2, 3, 4) in a geometry.
void lwgeom_free(LWGEOM *geom)
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
double lwgeom_mindistance2d(const LWGEOM *lw1, const LWGEOM *lw2)
Function initializing min distance calculation.
int clamp_srid(int srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
void lwpoly_free(LWPOLY *poly)
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
rt_errorstate rt_raster_geopoint_to_cell(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.
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)
uint32_t rt_band_get_nearest_pixel(rt_band band, int x, int y, uint16_t distancex, uint16_t distancey, int exclude_nodata_value, rt_pixel *npixels)
Get nearest pixel(s) with value (not NODATA) to specified pixel.
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.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...