PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ rt_band_get_nearest_pixel()

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.

Parameters
band: the band to get nearest pixel(s) from
x: the column of the pixel (0-based)
y: the line of the pixel (0-based)
distancex: the number of pixels around the specified pixel along the X axis
distancey: the number of pixels around the specified pixel along the Y axis
exclude_nodata_value: if non-zero, ignore nodata values to check for pixels with value
npixels: return set of rt_pixel object or NULL
Returns
-1 on error, otherwise the number of rt_pixel objects in npixels

Definition at line 1241 of file rt_band.c.

References genraster::count, distance(), ES_NONE, FALSE, rt_band_t::hasnodata, rt_band_t::height, rt_band_t::isnodata, rt_pixel_t::nodata, rt_band_t::nodataval, rt_band_t::pixtype, pixval::pixval, RASTER_DEBUG, RASTER_DEBUGF, rt_band_get_pixel(), rt_pixtype_get_min_value(), rt_pixtype_name(), rtalloc(), rtdealloc(), rterror(), rtrealloc(), rt_pixel_t::value, rt_band_t::width, rt_pixel_t::x, and rt_pixel_t::y.

Referenced by RASTER_nearestValue(), RASTER_neighborhood(), rt_raster_iterator(), test_band_get_nearest_pixel(), and test_pixel_set_to_array().

1247  {
1248  rt_pixel npixel = NULL;
1249  int extent[4] = {0};
1250  int max_extent[4] = {0};
1251  int d0 = 0;
1252  int distance[2] = {0};
1253  uint32_t _d[2] = {0};
1254  uint32_t i = 0;
1255  uint32_t j = 0;
1256  uint32_t k = 0;
1257  int _max = 0;
1258  int _x = 0;
1259  int _y = 0;
1260  int *_min = NULL;
1261  double pixval = 0;
1262  double minval = 0;
1263  uint32_t count = 0;
1264  int isnodata = 0;
1265 
1266  int inextent = 0;
1267 
1268  assert(NULL != band);
1269  assert(NULL != npixels);
1270 
1271  RASTER_DEBUG(3, "Starting");
1272 
1273  /* process distance */
1274  distance[0] = distancex;
1275  distance[1] = distancey;
1276 
1277  /* no distance, means get nearest pixels and return */
1278  if (!distance[0] && !distance[1])
1279  d0 = 1;
1280 
1281  RASTER_DEBUGF(4, "Selected pixel: %d x %d", x, y);
1282  RASTER_DEBUGF(4, "Distances: %d x %d", distance[0], distance[1]);
1283 
1284  /* shortcuts if outside band extent */
1285  if (
1286  exclude_nodata_value && (
1287  (x < 0 || x > band->width) ||
1288  (y < 0 || y > band->height)
1289  )
1290  ) {
1291  /* no distances specified, jump to pixel close to extent */
1292  if (d0) {
1293  if (x < 0)
1294  x = -1;
1295  else if (x > band->width)
1296  x = band->width;
1297 
1298  if (y < 0)
1299  y = -1;
1300  else if (y > band->height)
1301  y = band->height;
1302 
1303  RASTER_DEBUGF(4, "Moved selected pixel: %d x %d", x, y);
1304  }
1305  /*
1306  distances specified
1307  if distances won't capture extent of band, return 0
1308  */
1309  else if (
1310  ((x < 0 && abs(x) > distance[0]) || (x - band->width >= distance[0])) ||
1311  ((y < 0 && abs(y) > distance[1]) || (y - band->height >= distance[1]))
1312  ) {
1313  RASTER_DEBUG(4, "No nearest pixels possible for provided pixel and distances");
1314  return 0;
1315  }
1316  }
1317 
1318  /* no NODATA, exclude is FALSE */
1319  if (!band->hasnodata)
1320  exclude_nodata_value = FALSE;
1321  /* band is NODATA and excluding NODATA */
1322  else if (exclude_nodata_value && band->isnodata) {
1323  RASTER_DEBUG(4, "No nearest pixels possible as band is NODATA and excluding NODATA values");
1324  return 0;
1325  }
1326 
1327  /* determine the maximum distance to prevent an infinite loop */
1328  if (d0) {
1329  int a, b;
1330 
1331  /* X axis */
1332  a = abs(x);
1333  b = abs(x - band->width);
1334 
1335  if (a > b)
1336  distance[0] = a;
1337  else
1338  distance[0] = b;
1339 
1340  /* Y axis */
1341  a = abs(y);
1342  b = abs(y - band->height);
1343  if (a > b)
1344  distance[1] = a;
1345  else
1346  distance[1] = b;
1347 
1348  RASTER_DEBUGF(4, "Maximum distances: %d x %d", distance[0], distance[1]);
1349  }
1350 
1351  /* minimum possible value for pixel type */
1352  minval = rt_pixtype_get_min_value(band->pixtype);
1353  RASTER_DEBUGF(4, "pixtype: %s", rt_pixtype_name(band->pixtype));
1354  RASTER_DEBUGF(4, "minval: %f", minval);
1355 
1356  /* set variables */
1357  count = 0;
1358  *npixels = NULL;
1359 
1360  /* maximum extent */
1361  max_extent[0] = x - distance[0]; /* min X */
1362  max_extent[1] = y - distance[1]; /* min Y */
1363  max_extent[2] = x + distance[0]; /* max X */
1364  max_extent[3] = y + distance[1]; /* max Y */
1365  RASTER_DEBUGF(4, "Maximum Extent: (%d, %d, %d, %d)",
1366  max_extent[0], max_extent[1], max_extent[2], max_extent[3]);
1367 
1368  _d[0] = 0;
1369  _d[1] = 0;
1370  do {
1371  _d[0]++;
1372  _d[1]++;
1373 
1374  extent[0] = x - _d[0]; /* min x */
1375  extent[1] = y - _d[1]; /* min y */
1376  extent[2] = x + _d[0]; /* max x */
1377  extent[3] = y + _d[1]; /* max y */
1378 
1379  RASTER_DEBUGF(4, "Processing distances: %d x %d", _d[0], _d[1]);
1380  RASTER_DEBUGF(4, "Extent: (%d, %d, %d, %d)",
1381  extent[0], extent[1], extent[2], extent[3]);
1382 
1383  for (i = 0; i < 2; i++) {
1384 
1385  /* by row */
1386  if (i < 1)
1387  _max = extent[2] - extent[0] + 1;
1388  /* by column */
1389  else
1390  _max = extent[3] - extent[1] + 1;
1391  _max = abs(_max);
1392 
1393  for (j = 0; j < 2; j++) {
1394  /* by row */
1395  if (i < 1) {
1396  _x = extent[0];
1397  _min = &_x;
1398 
1399  /* top row */
1400  if (j < 1)
1401  _y = extent[1];
1402  /* bottom row */
1403  else
1404  _y = extent[3];
1405  }
1406  /* by column */
1407  else {
1408  _y = extent[1] + 1;
1409  _min = &_y;
1410 
1411  /* left column */
1412  if (j < 1) {
1413  _x = extent[0];
1414  _max -= 2;
1415  }
1416  /* right column */
1417  else
1418  _x = extent[2];
1419  }
1420 
1421  RASTER_DEBUGF(4, "_min, _max: %d, %d", *_min, _max);
1422  for (k = 0; k < _max; k++) {
1423  /* check that _x and _y are not outside max extent */
1424  if (
1425  _x < max_extent[0] || _x > max_extent[2] ||
1426  _y < max_extent[1] || _y > max_extent[3]
1427  ) {
1428  (*_min)++;
1429  continue;
1430  }
1431 
1432  /* outside band extent, set to NODATA */
1433  if (
1434  (_x < 0 || _x >= band->width) ||
1435  (_y < 0 || _y >= band->height)
1436  ) {
1437  /* no NODATA, set to minimum possible value */
1438  if (!band->hasnodata)
1439  pixval = minval;
1440  /* has NODATA, use NODATA */
1441  else
1442  pixval = band->nodataval;
1443  RASTER_DEBUGF(4, "NODATA pixel outside band extent: (x, y, val) = (%d, %d, %f)", _x, _y, pixval);
1444  inextent = 0;
1445  isnodata = 1;
1446  }
1447  else {
1448  if (rt_band_get_pixel(
1449  band,
1450  _x, _y,
1451  &pixval,
1452  &isnodata
1453  ) != ES_NONE) {
1454  rterror("rt_band_get_nearest_pixel: Could not get pixel value");
1455  if (count) rtdealloc(*npixels);
1456  return -1;
1457  }
1458  RASTER_DEBUGF(4, "Pixel: (x, y, val) = (%d, %d, %f)", _x, _y, pixval);
1459  inextent = 1;
1460  }
1461 
1462  /* use pixval? */
1463  if (!exclude_nodata_value || (exclude_nodata_value && !isnodata)) {
1464  /* add pixel to result set */
1465  RASTER_DEBUGF(4, "Adding pixel to set of nearest pixels: (x, y, val) = (%d, %d, %f)", _x, _y, pixval);
1466  count++;
1467 
1468  if (*npixels == NULL)
1469  *npixels = (rt_pixel) rtalloc(sizeof(struct rt_pixel_t) * count);
1470  else
1471  *npixels = (rt_pixel) rtrealloc(*npixels, sizeof(struct rt_pixel_t) * count);
1472  if (*npixels == NULL) {
1473  rterror("rt_band_get_nearest_pixel: Could not allocate memory for nearest pixel(s)");
1474  return -1;
1475  }
1476 
1477  npixel = &((*npixels)[count - 1]);
1478  npixel->x = _x;
1479  npixel->y = _y;
1480  npixel->value = pixval;
1481 
1482  /* special case for when outside band extent */
1483  if (!inextent && !band->hasnodata)
1484  npixel->nodata = 1;
1485  else
1486  npixel->nodata = 0;
1487  }
1488 
1489  (*_min)++;
1490  }
1491  }
1492  }
1493 
1494  /* distance threshholds met */
1495  if (_d[0] >= distance[0] && _d[1] >= distance[1])
1496  break;
1497  else if (d0 && count)
1498  break;
1499  }
1500  while (1);
1501 
1502  RASTER_DEBUGF(3, "Nearest pixels in return: %d", count);
1503 
1504  return count;
1505 }
struct rt_pixel_t * rt_pixel
Definition: librtcore.h:147
rt_pixtype pixtype
Definition: librtcore.h:2265
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:199
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:171
void * rtrealloc(void *mem, size_t size)
Definition: rt_context.c:179
uint16_t height
Definition: librtcore.h:2268
double value
Definition: librtcore.h:2289
unsigned int uint32_t
Definition: uthash.h:78
Definition: pixval.py:1
int count
Definition: genraster.py:56
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
Definition: rt_band.c:1088
double nodataval
Definition: librtcore.h:2272
double rt_pixtype_get_min_value(rt_pixtype pixtype)
Return minimum value possible for pixel type.
Definition: rt_pixel.c:148
#define RASTER_DEBUGF(level, msg,...)
Definition: librtcore.h:299
uint8_t nodata
Definition: librtcore.h:2288
uint16_t width
Definition: librtcore.h:2267
Datum distance(PG_FUNCTION_ARGS)
pixval
Definition: pixval.py:93
void rtdealloc(void *mem)
Definition: rt_context.c:186
int32_t isnodata
Definition: librtcore.h:2270
#define FALSE
Definition: dbfopen.c:168
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:295
const char * rt_pixtype_name(rt_pixtype pixtype)
Definition: rt_pixel.c:110
int32_t hasnodata
Definition: librtcore.h:2269
Here is the call graph for this function:
Here is the caller graph for this function: