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) {
2021 value = npixels[i].
value;
2030 value = npixels[0].
value;
2036 PG_FREE_IF_COPY(geom, 2);
2038 PG_FREE_IF_COPY(pgraster, 0);
2041 PG_RETURN_FLOAT8(value);
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
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 rt_raster_get_num_bands(rt_raster raster)
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
void lwgeom_free(LWGEOM *geom)
void rt_band_destroy(rt_band band)
Destroy a raster band.
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
int lwgeom_ndims(const LWGEOM *geom)
Return the number of dimensions (2, 3, 4) in a geometry.
void lwpoly_free(LWPOLY *poly)
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
LWPOLY * rt_raster_pixel_as_polygon(rt_raster raster, int x, int y)
Get a raster pixel as a polygon.
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
int getPoint2d_p(const POINTARRAY *pa, int n, POINT2D *point)
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.
uint16_t rt_raster_get_width(rt_raster raster)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
double lwgeom_mindistance2d(const LWGEOM *lw1, const LWGEOM *lw2)
Function initialazing min distance calculation.
uint16_t rt_raster_get_height(rt_raster raster)
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
int 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.
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)...