2045{
2049 int bandindex = 1;
2050 int num_bands = 0;
2052 bool exclude_nodata_value =
TRUE;
2056
2062 int hasvalue = 0;
2063 int isnodata = 0;
2064
2065 if (PG_ARGISNULL(0))
2066 PG_RETURN_NULL();
2067 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
2069 if (!raster) {
2070 PG_FREE_IF_COPY(pgraster, 0);
2071 elog(ERROR, "RASTER_nearestValue: Could not deserialize raster");
2072 PG_RETURN_NULL();
2073 }
2074
2075
2076 if (!PG_ARGISNULL(1))
2077 bandindex = PG_GETARG_INT32(1);
2079 if (bandindex < 1 || bandindex > num_bands) {
2080 elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
2082 PG_FREE_IF_COPY(pgraster, 0);
2083 PG_RETURN_NULL();
2084 }
2085
2086
2087 geom = PG_GETARG_GSERIALIZED_P(2);
2089 elog(NOTICE, "Geometry provided must be a point");
2091 PG_FREE_IF_COPY(pgraster, 0);
2092 PG_FREE_IF_COPY(geom, 2);
2093 PG_RETURN_NULL();
2094 }
2095
2096
2097 if (!PG_ARGISNULL(3))
2098 exclude_nodata_value = PG_GETARG_BOOL(3);
2099
2100
2102 elog(NOTICE, "SRIDs of geometry and raster do not match");
2104 PG_FREE_IF_COPY(pgraster, 0);
2105 PG_FREE_IF_COPY(geom, 2);
2106 PG_RETURN_NULL();
2107 }
2108
2109
2111 if (!band) {
2112 elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
2114 PG_FREE_IF_COPY(pgraster, 0);
2115 PG_FREE_IF_COPY(geom, 2);
2116 PG_RETURN_NULL();
2117 }
2118
2119
2121
2123 elog(NOTICE, "Geometry provided cannot be empty");
2125 PG_FREE_IF_COPY(pgraster, 0);
2126 PG_FREE_IF_COPY(geom, 2);
2127 PG_RETURN_NULL();
2128 }
2129
2130
2134 lwgeom = lwgeom2d;
2135 }
2136
2139
2141 raster,
2143 &x, &y,
2144 NULL
2147 PG_FREE_IF_COPY(pgraster, 0);
2149 PG_FREE_IF_COPY(geom, 2);
2150 elog(ERROR, "RASTER_nearestValue: Could not compute pixel coordinates from spatial coordinates");
2151 PG_RETURN_NULL();
2152 }
2153
2154
2155 if (
2158 ) {
2161 PG_FREE_IF_COPY(pgraster, 0);
2163 PG_FREE_IF_COPY(geom, 2);
2164 elog(ERROR, "RASTER_nearestValue: Could not get pixel value for band at index %d", bandindex);
2165 PG_RETURN_NULL();
2166 }
2167
2168
2169 if (!exclude_nodata_value || !isnodata) {
2171 PG_FREE_IF_COPY(pgraster, 0);
2173 PG_FREE_IF_COPY(geom, 2);
2174
2175 PG_RETURN_FLOAT8(value);
2176 }
2177 }
2178
2179
2181 band,
2182 x, y,
2183 0, 0,
2184 exclude_nodata_value,
2185 &npixels
2186 );
2188
2189 if (count < 1) {
2190
2191 if (count < 0)
2192 elog(NOTICE, "Could not get the nearest value for band at index %d", bandindex);
2193
2194 else
2195 elog(NOTICE, "No nearest value found for band at index %d", bandindex);
2196
2198 PG_FREE_IF_COPY(geom, 2);
2200 PG_FREE_IF_COPY(pgraster, 0);
2201 PG_RETURN_NULL();
2202 }
2203
2204
2205 if (count > 1) {
2206 int i = 0;
2208 double lastdist = -1;
2209 double dist;
2210
2211 for (i = 0; i <
count; i++) {
2212
2214 if (!poly) {
2216 PG_FREE_IF_COPY(geom, 2);
2218 PG_FREE_IF_COPY(pgraster, 0);
2219 elog(ERROR, "RASTER_nearestValue: Could not get polygon of neighboring pixel");
2220 PG_RETURN_NULL();
2221 }
2222
2223
2225 if (lastdist < 0 || dist < lastdist) {
2227 hasvalue = 1;
2228 }
2229 lastdist = dist;
2230
2232 }
2233 }
2234 else {
2236 hasvalue = 1;
2237 }
2238
2239 pfree(npixels);
2241 PG_FREE_IF_COPY(geom, 2);
2243 PG_FREE_IF_COPY(pgraster, 0);
2244
2245 if (hasvalue)
2246 PG_RETURN_FLOAT8(value);
2247 else
2248 PG_RETURN_NULL();
2249}
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): ...