1840{
1844 int bandindex = 1;
1845 int num_bands = 0;
1847 bool exclude_nodata_value =
TRUE;
1851
1857 int hasvalue = 0;
1858 int isnodata = 0;
1859
1860 if (PG_ARGISNULL(0))
1861 PG_RETURN_NULL();
1862 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1864 if (!raster) {
1865 PG_FREE_IF_COPY(pgraster, 0);
1866 elog(ERROR, "RASTER_nearestValue: Could not deserialize raster");
1867 PG_RETURN_NULL();
1868 }
1869
1870
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);
1878 PG_RETURN_NULL();
1879 }
1880
1881
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);
1888 PG_RETURN_NULL();
1889 }
1890
1891
1892 if (!PG_ARGISNULL(3))
1893 exclude_nodata_value = PG_GETARG_BOOL(3);
1894
1895
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);
1901 PG_RETURN_NULL();
1902 }
1903
1904
1906 if (!band) {
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);
1911 PG_RETURN_NULL();
1912 }
1913
1914
1916
1918 elog(NOTICE, "Geometry provided cannot be empty");
1920 PG_FREE_IF_COPY(pgraster, 0);
1921 PG_FREE_IF_COPY(geom, 2);
1922 PG_RETURN_NULL();
1923 }
1924
1925
1929 lwgeom = lwgeom2d;
1930 }
1931
1934
1936 raster,
1938 &x, &y,
1939 NULL
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");
1946 PG_RETURN_NULL();
1947 }
1948
1949
1950 if (
1953 ) {
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);
1960 PG_RETURN_NULL();
1961 }
1962
1963
1964 if (!exclude_nodata_value || !isnodata) {
1966 PG_FREE_IF_COPY(pgraster, 0);
1968 PG_FREE_IF_COPY(geom, 2);
1969
1970 PG_RETURN_FLOAT8(value);
1971 }
1972 }
1973
1974
1976 band,
1977 x, y,
1978 0, 0,
1979 exclude_nodata_value,
1980 &npixels
1981 );
1983
1984 if (count < 1) {
1985
1986 if (count < 0)
1987 elog(NOTICE, "Could not get the nearest value for band at index %d", bandindex);
1988
1989 else
1990 elog(NOTICE, "No nearest value found for band at index %d", bandindex);
1991
1993 PG_FREE_IF_COPY(geom, 2);
1995 PG_FREE_IF_COPY(pgraster, 0);
1996 PG_RETURN_NULL();
1997 }
1998
1999
2000 if (count > 1) {
2001 int i = 0;
2003 double lastdist = -1;
2004 double dist;
2005
2006 for (i = 0; i <
count; i++) {
2007
2009 if (!poly) {
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");
2015 PG_RETURN_NULL();
2016 }
2017
2018
2020 if (lastdist < 0 || dist < lastdist) {
2022 hasvalue = 1;
2023 }
2024 lastdist = dist;
2025
2027 }
2028 }
2029 else {
2031 hasvalue = 1;
2032 }
2033
2034 pfree(npixels);
2036 PG_FREE_IF_COPY(geom, 2);
2038 PG_FREE_IF_COPY(pgraster, 0);
2039
2040 if (hasvalue)
2041 PG_RETURN_FLOAT8(value);
2042 else
2043 PG_RETURN_NULL();
2044}
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.
uint32_t gserialized_get_type(const GSERIALIZED *g)
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)
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
double lwgeom_mindistance2d(const LWGEOM *lw1, const LWGEOM *lw2)
Function initializing min distance calculation.
void lwpoly_free(LWPOLY *poly)
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
int32_t clamp_srid(int32_t srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
LWPOLY * rt_raster_pixel_as_polygon(rt_raster raster, int x, int y)
Get a raster pixel as a polygon.
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.
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.
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...