2062 bool exclude_nodata_value =
TRUE;
2068 double **value2D = NULL;
2069 int **nodata2D = NULL;
2074 Datum *value1D = NULL;
2075 bool *nodata1D = NULL;
2077 int lbound[2] = {1, 1};
2078 ArrayType *mdArray = NULL;
2085 if (PG_ARGISNULL(0))
2087 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
2091 PG_FREE_IF_COPY(pgraster, 0);
2092 elog(ERROR,
"RASTER_neighborhood: Could not deserialize raster");
2097 if (!PG_ARGISNULL(1))
2098 bandindex = PG_GETARG_INT32(1);
2100 if (bandindex < 1 || bandindex > num_bands) {
2101 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
2103 PG_FREE_IF_COPY(pgraster, 0);
2108 x = PG_GETARG_INT32(2);
2112 y = PG_GETARG_INT32(3);
2116 distance[0] = PG_GETARG_INT32(4);
2117 if (distance[0] < 0) {
2118 elog(NOTICE,
"Invalid value for distancex (must be >= zero). Returning NULL");
2120 PG_FREE_IF_COPY(pgraster, 0);
2123 distance[0] = (uint16_t) distance[0];
2126 distance[1] = PG_GETARG_INT32(5);
2127 if (distance[1] < 0) {
2128 elog(NOTICE,
"Invalid value for distancey (must be >= zero). Returning NULL");
2130 PG_FREE_IF_COPY(pgraster, 0);
2133 distance[1] = (uint16_t) distance[1];
2136 if (!PG_ARGISNULL(6))
2137 exclude_nodata_value = PG_GETARG_BOOL(6);
2142 elog(NOTICE,
"Could not find band at index %d. Returning NULL", bandindex);
2144 PG_FREE_IF_COPY(pgraster, 0);
2151 if (distance[0] > 0 || distance[1] > 0) {
2155 distance[0], distance[1],
2156 exclude_nodata_value,
2161 elog(NOTICE,
"Could not get the pixel's neighborhood for band at index %d", bandindex);
2165 PG_FREE_IF_COPY(pgraster, 0);
2182 elog(NOTICE,
"Could not get the pixel of band at index %d. Returning NULL", bandindex);
2185 PG_FREE_IF_COPY(pgraster, 0);
2208 if (npixels == NULL) {
2212 PG_FREE_IF_COPY(pgraster, 0);
2214 elog(ERROR,
"RASTER_neighborhood: Could not reallocate memory for neighborhood");
2217 npixels[count - 1].
x = _x;
2218 npixels[count - 1].
y = _y;
2219 npixels[count - 1].
nodata = 1;
2223 if (!exclude_nodata_value || !isnodata) {
2224 npixels[count - 1].
nodata = 0;
2230 PG_FREE_IF_COPY(pgraster, 0);
2235 npixels, count, NULL,
2237 distance[0], distance[1],
2240 &(dim[1]), &(dim[0])
2244 elog(NOTICE,
"Could not create 2D array of neighborhood");
2249 value1D = palloc(
sizeof(Datum) * dim[0] * dim[1]);
2250 nodata1D = palloc(
sizeof(
bool) * dim[0] * dim[1]);
2252 if (value1D == NULL || nodata1D == NULL) {
2254 for (i = 0; i < dim[0]; i++) {
2261 elog(ERROR,
"RASTER_neighborhood: Could not allocate memory for return 2D array");
2268 for (i = 0; i < dim[0]; i++) {
2270 for (j = 0; j < dim[1]; j++) {
2271 nodata1D[k] = (bool) nodata2D[i][j];
2273 value1D[k] = Float8GetDatum(value2D[i][j]);
2275 value1D[k] = PointerGetDatum(NULL);
2282 for (i = 0; i < dim[0]; i++) {
2290 get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
2292 mdArray = construct_md_array(
2296 typlen, typbyval, typalign
2302 PG_RETURN_ARRAYTYPE_P(mdArray);
int rt_raster_get_num_bands(rt_raster raster)
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
struct rt_pixel_t * rt_pixel
rt_errorstate rt_pixel_set_to_array(rt_pixel npixel, int count, rt_mask mask, int x, int y, uint16_t distancex, uint16_t distancey, double ***value, int ***nodata, int *dimx, int *dimy)
void rt_band_destroy(rt_band band)
Destroy a raster band.
#define POSTGIS_RT_DEBUGF(level, msg,...)
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
uint16_t rt_band_get_width(rt_band band)
Return width of this band.
uint16_t rt_band_get_height(rt_band band)
Return height of this band.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Datum distance(PG_FUNCTION_ARGS)
double rt_band_get_min_value(rt_band band)
Returns the minimal possible value for the band according to the pixel type.
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.