PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ rt_band_get_pixel_of_value()

int rt_band_get_pixel_of_value ( rt_band  band,
int  exclude_nodata_value,
double *  searchset,
int  searchcount,
rt_pixel pixels 
)

Search band for pixel(s) with search values.

Parameters
band: the band to query for minimum and maximum pixel values
exclude_nodata_value: if non-zero, ignore nodata values
searchset: array of values to count
searchcount: the number of search values
pixels: pixels with the search value
Returns
-1 on error, otherwise number of pixels

Definition at line 1604 of file rt_band.c.

References genraster::count, ES_NONE, FALSE, FLT_NEQ, rt_band_t::hasnodata, rt_band_t::height, rt_band_t::isnodata, rt_pixel_t::nodata, pixval::pixel, rt_band_t::pixtype, pixval::pixval, RASTER_DEBUG, rt_band_get_pixel(), rt_pixtype_compare_clamped_values(), rtalloc(), rterror(), rtrealloc(), rt_pixel_t::value, rt_band_t::width, pixval::x, rt_pixel_t::x, pixval::y, and rt_pixel_t::y.

Referenced by RASTER_pixelOfValue(), and test_band_get_pixel_of_value().

1608  {
1609  int x;
1610  int y;
1611  int i;
1612  double pixval;
1613  int err;
1614  int count = 0;
1615  int isnodata = 0;
1616  int isequal = 0;
1617 
1618  rt_pixel pixel = NULL;
1619 
1620  assert(NULL != band);
1621  assert(NULL != pixels);
1622  assert(NULL != searchset && searchcount > 0);
1623 
1624  if (!band->hasnodata)
1625  exclude_nodata_value = FALSE;
1626  /* band is NODATA and exclude_nodata_value = TRUE, nothing to search */
1627  else if (exclude_nodata_value && band->isnodata) {
1628  RASTER_DEBUG(4, "Pixels cannot be searched as band is NODATA and excluding NODATA values");
1629  return 0;
1630  }
1631 
1632  for (x = 0; x < band->width; x++) {
1633  for (y = 0; y < band->height; y++) {
1634  err = rt_band_get_pixel(band, x, y, &pixval, &isnodata);
1635  if (err != ES_NONE) {
1636  rterror("rt_band_get_pixel_of_value: Cannot get band pixel");
1637  return -1;
1638  }
1639  else if (exclude_nodata_value && isnodata)
1640  continue;
1641 
1642  for (i = 0; i < searchcount; i++) {
1643  if (rt_pixtype_compare_clamped_values(band->pixtype, searchset[i], pixval, &isequal) != ES_NONE) {
1644  continue;
1645  }
1646 
1647  if (FLT_NEQ(pixval, searchset[i]) || !isequal)
1648  continue;
1649 
1650  /* match found */
1651  count++;
1652  if (*pixels == NULL)
1653  *pixels = (rt_pixel) rtalloc(sizeof(struct rt_pixel_t) * count);
1654  else
1655  *pixels = (rt_pixel) rtrealloc(*pixels, sizeof(struct rt_pixel_t) * count);
1656  if (*pixels == NULL) {
1657  rterror("rt_band_get_pixel_of_value: Could not allocate memory for pixel(s)");
1658  return -1;
1659  }
1660 
1661  pixel = &((*pixels)[count - 1]);
1662  pixel->x = x;
1663  pixel->y = y;
1664  pixel->nodata = 0;
1665  pixel->value = pixval;
1666  }
1667  }
1668  }
1669 
1670  return count;
1671 }
pixel
Definition: pixval.py:90
struct rt_pixel_t * rt_pixel
Definition: librtcore.h:147
rt_pixtype pixtype
Definition: librtcore.h:2294
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:2297
double value
Definition: librtcore.h:2318
#define FLT_NEQ(x, y)
Definition: librtcore.h:2213
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:1173
uint8_t nodata
Definition: librtcore.h:2317
uint16_t width
Definition: librtcore.h:2296
pixval
Definition: pixval.py:93
int32_t isnodata
Definition: librtcore.h:2299
#define FALSE
Definition: dbfopen.c:168
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:295
int32_t hasnodata
Definition: librtcore.h:2298
rt_errorstate rt_pixtype_compare_clamped_values(rt_pixtype pixtype, double val, double refval, int *isequal)
Test to see if two values are equal when clamped.
Definition: rt_pixel.c:200
Here is the call graph for this function:
Here is the caller graph for this function: