PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ rt_band_get_pixel()

rt_errorstate rt_band_get_pixel ( rt_band  band,
int  x,
int  y,
double *  value,
int *  nodata 
)

Get pixel value.

If band's isnodata flag is TRUE, value returned will be the band's NODATA value

Parameters
band: the band to set nodata value to
x: x ordinate (0-based)
y: x ordinate (0-based)
*value: pixel value
*nodata: 0 if pixel is not NODATA
Returns
0 on success, -1 on error (value out of valid range).

Definition at line 1527 of file rt_band.c.

1532  {
1533  rt_pixtype pixtype = PT_END;
1534  uint8_t* data = NULL;
1535  uint32_t offset = 0;
1536 
1537  assert(NULL != band);
1538  assert(NULL != value);
1539 
1540  /* set nodata to 0 */
1541  if (nodata != NULL)
1542  *nodata = 0;
1543 
1544  if (
1545  x < 0 || x >= band->width ||
1546  y < 0 || y >= band->height
1547  ) {
1548  rtwarn("Attempting to get pixel value with out of range raster coordinates: (%d, %d)", x, y);
1549  return ES_ERROR;
1550  }
1551 
1552  /* band is NODATA */
1553  if (band->isnodata) {
1554  RASTER_DEBUG(3, "Band's isnodata flag is TRUE. Returning NODATA value");
1555  *value = band->nodataval;
1556  if (nodata != NULL) *nodata = 1;
1557  return ES_NONE;
1558  }
1559 
1561  if (data == NULL) {
1562  rterror("rt_band_get_pixel: Cannot get band data");
1563  return ES_ERROR;
1564  }
1565 
1566  /* +1 for the nodata value */
1567  offset = x + (y * band->width);
1568 
1569  pixtype = band->pixtype;
1570 
1571  switch (pixtype) {
1572  case PT_1BB:
1573 #ifdef OPTIMIZE_SPACE
1574  {
1575  int byteOffset = offset / 8;
1576  int bitOffset = offset % 8;
1577  data += byteOffset;
1578 
1579  /* Bit to set is bitOffset into data */
1580  *value = getBits(data, val, 1, bitOffset);
1581  break;
1582  }
1583 #endif
1584  case PT_2BUI:
1585 #ifdef OPTIMIZE_SPACE
1586  {
1587  int byteOffset = offset / 4;
1588  int bitOffset = offset % 4;
1589  data += byteOffset;
1590 
1591  /* Bits to set start at bitOffset into data */
1592  *value = getBits(data, val, 2, bitOffset);
1593  break;
1594  }
1595 #endif
1596  case PT_4BUI:
1597 #ifdef OPTIMIZE_SPACE
1598  {
1599  int byteOffset = offset / 2;
1600  int bitOffset = offset % 2;
1601  data += byteOffset;
1602 
1603  /* Bits to set start at bitOffset into data */
1604  *value = getBits(data, val, 2, bitOffset);
1605  break;
1606  }
1607 #endif
1608  case PT_8BSI: {
1609  int8_t val = (int8_t)data[offset];
1610  *value = val;
1611  break;
1612  }
1613  case PT_8BUI: {
1614  uint8_t val = data[offset];
1615  *value = val;
1616  break;
1617  }
1618  case PT_16BSI: {
1619  int16_t *ptr = (int16_t*) data; /* we assume correct alignment */
1620  *value = ptr[offset];
1621  break;
1622  }
1623  case PT_16BUI: {
1624  uint16_t *ptr = (uint16_t*) data; /* we assume correct alignment */
1625  *value = ptr[offset];
1626  break;
1627  }
1628  case PT_32BSI: {
1629  int32_t *ptr = (int32_t*) data; /* we assume correct alignment */
1630  *value = ptr[offset];
1631  break;
1632  }
1633  case PT_32BUI: {
1634  uint32_t *ptr = (uint32_t*) data; /* we assume correct alignment */
1635  *value = ptr[offset];
1636  break;
1637  }
1638  case PT_32BF: {
1639  float *ptr = (float*) data; /* we assume correct alignment */
1640  *value = ptr[offset];
1641  break;
1642  }
1643  case PT_64BF: {
1644  double *ptr = (double*) data; /* we assume correct alignment */
1645  *value = ptr[offset];
1646  break;
1647  }
1648  default: {
1649  rterror("rt_band_get_pixel: Unknown pixeltype %d", pixtype);
1650  return ES_ERROR;
1651  }
1652  }
1653 
1654  /* set NODATA flag */
1655  if (band->hasnodata && nodata != NULL) {
1657  *nodata = 1;
1658  }
1659 
1660  return ES_NONE;
1661 }
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:302
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
rt_pixtype
Definition: librtcore.h:187
@ PT_32BUI
Definition: librtcore.h:196
@ PT_2BUI
Definition: librtcore.h:189
@ PT_32BSI
Definition: librtcore.h:195
@ PT_END
Definition: librtcore.h:199
@ PT_4BUI
Definition: librtcore.h:190
@ PT_32BF
Definition: librtcore.h:197
@ PT_1BB
Definition: librtcore.h:188
@ PT_16BUI
Definition: librtcore.h:194
@ PT_8BSI
Definition: librtcore.h:191
@ PT_16BSI
Definition: librtcore.h:193
@ PT_64BF
Definition: librtcore.h:198
@ PT_8BUI
Definition: librtcore.h:192
@ ES_NONE
Definition: librtcore.h:182
@ ES_ERROR
Definition: librtcore.h:183
int value
Definition: genraster.py:62
band
Definition: ovdump.py:58
data
Definition: ovdump.py:104
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
Definition: rt_band.c:551
int rt_band_clamped_value_is_nodata(rt_band band, double val)
Compare clamped value to band's clamped NODATA value.
Definition: rt_band.c:2106

References ovdump::band, ovdump::data, ES_ERROR, ES_NONE, PT_16BSI, PT_16BUI, PT_1BB, PT_2BUI, PT_32BF, PT_32BSI, PT_32BUI, PT_4BUI, PT_64BF, PT_8BSI, PT_8BUI, PT_END, RASTER_DEBUG, rt_band_clamped_value_is_nodata(), rt_band_get_data(), rterror(), rtwarn(), genraster::value, pixval::x, and pixval::y.

Referenced by _rti_raster_get_band_perimeter(), RASTER_clip(), RASTER_dumpValues(), RASTER_getPixelCentroids(), RASTER_getPixelPolygons(), RASTER_getPixelValue(), RASTER_mapAlgebra2(), RASTER_mapAlgebraExpr(), RASTER_mapAlgebraFct(), RASTER_mapAlgebraFctNgb(), RASTER_nearestValue(), RASTER_neighborhood(), RASTER_setPixelValuesArray(), RASTER_setPixelValuesGeomval(), rt_band_check_is_nodata(), rt_band_get_nearest_pixel(), rt_band_get_pixel_bilinear(), rt_band_get_pixel_of_value(), rt_band_get_pixel_resample(), rt_band_get_quantiles_stream(), rt_band_get_summary_stats(), rt_band_get_value_count(), rt_band_reclass(), rt_band_reclass_exact(), rt_band_set_pixel_line(), rt_raster_gdal_rasterize(), rt_raster_intersects(), rt_raster_intersects_algorithm(), rt_raster_iterator(), rt_raster_to_gdal_mem(), test_band_metadata(), test_band_pixtype_16BSI(), test_band_pixtype_16BUI(), test_band_pixtype_1BB(), test_band_pixtype_2BUI(), test_band_pixtype_32BF(), test_band_pixtype_32BSI(), test_band_pixtype_32BUI(), test_band_pixtype_4BUI(), test_band_pixtype_64BF(), test_band_pixtype_8BSI(), test_band_pixtype_8BUI(), test_band_reclass(), test_gdal_to_raster(), test_gdal_warp(), test_pixel_set_to_array(), test_raster_colormap(), and test_raster_wkb().

Here is the call graph for this function:
Here is the caller graph for this function: