PostGIS  3.4.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 1376 of file rt_band.c.

1381  {
1382  rt_pixtype pixtype = PT_END;
1383  uint8_t* data = NULL;
1384  uint32_t offset = 0;
1385 
1386  assert(NULL != band);
1387  assert(NULL != value);
1388 
1389  /* set nodata to 0 */
1390  if (nodata != NULL)
1391  *nodata = 0;
1392 
1393  if (
1394  x < 0 || x >= band->width ||
1395  y < 0 || y >= band->height
1396  ) {
1397  rtwarn("Attempting to get pixel value with out of range raster coordinates: (%d, %d)", x, y);
1398  return ES_ERROR;
1399  }
1400 
1401  /* band is NODATA */
1402  if (band->isnodata) {
1403  RASTER_DEBUG(3, "Band's isnodata flag is TRUE. Returning NODATA value");
1404  *value = band->nodataval;
1405  if (nodata != NULL) *nodata = 1;
1406  return ES_NONE;
1407  }
1408 
1410  if (data == NULL) {
1411  rterror("rt_band_get_pixel: Cannot get band data");
1412  return ES_ERROR;
1413  }
1414 
1415  /* +1 for the nodata value */
1416  offset = x + (y * band->width);
1417 
1418  pixtype = band->pixtype;
1419 
1420  switch (pixtype) {
1421  case PT_1BB:
1422 #ifdef OPTIMIZE_SPACE
1423  {
1424  int byteOffset = offset / 8;
1425  int bitOffset = offset % 8;
1426  data += byteOffset;
1427 
1428  /* Bit to set is bitOffset into data */
1429  *value = getBits(data, val, 1, bitOffset);
1430  break;
1431  }
1432 #endif
1433  case PT_2BUI:
1434 #ifdef OPTIMIZE_SPACE
1435  {
1436  int byteOffset = offset / 4;
1437  int bitOffset = offset % 4;
1438  data += byteOffset;
1439 
1440  /* Bits to set start at bitOffset into data */
1441  *value = getBits(data, val, 2, bitOffset);
1442  break;
1443  }
1444 #endif
1445  case PT_4BUI:
1446 #ifdef OPTIMIZE_SPACE
1447  {
1448  int byteOffset = offset / 2;
1449  int bitOffset = offset % 2;
1450  data += byteOffset;
1451 
1452  /* Bits to set start at bitOffset into data */
1453  *value = getBits(data, val, 2, bitOffset);
1454  break;
1455  }
1456 #endif
1457  case PT_8BSI: {
1458  int8_t val = (int8_t)data[offset];
1459  *value = val;
1460  break;
1461  }
1462  case PT_8BUI: {
1463  uint8_t val = data[offset];
1464  *value = val;
1465  break;
1466  }
1467  case PT_16BSI: {
1468  int16_t *ptr = (int16_t*) data; /* we assume correct alignment */
1469  *value = ptr[offset];
1470  break;
1471  }
1472  case PT_16BUI: {
1473  uint16_t *ptr = (uint16_t*) data; /* we assume correct alignment */
1474  *value = ptr[offset];
1475  break;
1476  }
1477  case PT_32BSI: {
1478  int32_t *ptr = (int32_t*) data; /* we assume correct alignment */
1479  *value = ptr[offset];
1480  break;
1481  }
1482  case PT_32BUI: {
1483  uint32_t *ptr = (uint32_t*) data; /* we assume correct alignment */
1484  *value = ptr[offset];
1485  break;
1486  }
1487  case PT_32BF: {
1488  float *ptr = (float*) data; /* we assume correct alignment */
1489  *value = ptr[offset];
1490  break;
1491  }
1492  case PT_64BF: {
1493  double *ptr = (double*) data; /* we assume correct alignment */
1494  *value = ptr[offset];
1495  break;
1496  }
1497  default: {
1498  rterror("rt_band_get_pixel: Unknown pixeltype %d", pixtype);
1499  return ES_ERROR;
1500  }
1501  }
1502 
1503  /* set NODATA flag */
1504  if (band->hasnodata && nodata != NULL) {
1506  *nodata = 1;
1507  }
1508 
1509  return ES_NONE;
1510 }
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:219
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:302
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
void rtwarn(const char *fmt,...)
Definition: rt_context.c:244
@ 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:400
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:1955

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_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: