PostGIS  2.5.7dev-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 1221 of file rt_band.c.

1226  {
1227  rt_pixtype pixtype = PT_END;
1228  uint8_t* data = NULL;
1229  uint32_t offset = 0;
1230 
1231  assert(NULL != band);
1232  assert(NULL != value);
1233 
1234  /* set nodata to 0 */
1235  if (nodata != NULL)
1236  *nodata = 0;
1237 
1238  if (
1239  x < 0 || x >= band->width ||
1240  y < 0 || y >= band->height
1241  ) {
1242  rtwarn("Attempting to get pixel value with out of range raster coordinates: (%d, %d)", x, y);
1243  return ES_ERROR;
1244  }
1245 
1246  /* band is NODATA */
1247  if (band->isnodata) {
1248  RASTER_DEBUG(3, "Band's isnodata flag is TRUE. Returning NODATA value");
1249  *value = band->nodataval;
1250  if (nodata != NULL) *nodata = 1;
1251  return ES_NONE;
1252  }
1253 
1255  if (data == NULL) {
1256  rterror("rt_band_get_pixel: Cannot get band data");
1257  return ES_ERROR;
1258  }
1259 
1260  /* +1 for the nodata value */
1261  offset = x + (y * band->width);
1262 
1263  pixtype = band->pixtype;
1264 
1265  switch (pixtype) {
1266  case PT_1BB:
1267 #ifdef OPTIMIZE_SPACE
1268  {
1269  int byteOffset = offset / 8;
1270  int bitOffset = offset % 8;
1271  data += byteOffset;
1272 
1273  /* Bit to set is bitOffset into data */
1274  *value = getBits(data, val, 1, bitOffset);
1275  break;
1276  }
1277 #endif
1278  case PT_2BUI:
1279 #ifdef OPTIMIZE_SPACE
1280  {
1281  int byteOffset = offset / 4;
1282  int bitOffset = offset % 4;
1283  data += byteOffset;
1284 
1285  /* Bits to set start at bitOffset into data */
1286  *value = getBits(data, val, 2, bitOffset);
1287  break;
1288  }
1289 #endif
1290  case PT_4BUI:
1291 #ifdef OPTIMIZE_SPACE
1292  {
1293  int byteOffset = offset / 2;
1294  int bitOffset = offset % 2;
1295  data += byteOffset;
1296 
1297  /* Bits to set start at bitOffset into data */
1298  *value = getBits(data, val, 2, bitOffset);
1299  break;
1300  }
1301 #endif
1302  case PT_8BSI: {
1303  int8_t val = data[offset];
1304  *value = val;
1305  break;
1306  }
1307  case PT_8BUI: {
1308  uint8_t val = data[offset];
1309  *value = val;
1310  break;
1311  }
1312  case PT_16BSI: {
1313  int16_t *ptr = (int16_t*) data; /* we assume correct alignment */
1314  *value = ptr[offset];
1315  break;
1316  }
1317  case PT_16BUI: {
1318  uint16_t *ptr = (uint16_t*) data; /* we assume correct alignment */
1319  *value = ptr[offset];
1320  break;
1321  }
1322  case PT_32BSI: {
1323  int32_t *ptr = (int32_t*) data; /* we assume correct alignment */
1324  *value = ptr[offset];
1325  break;
1326  }
1327  case PT_32BUI: {
1328  uint32_t *ptr = (uint32_t*) data; /* we assume correct alignment */
1329  *value = ptr[offset];
1330  break;
1331  }
1332  case PT_32BF: {
1333  float *ptr = (float*) data; /* we assume correct alignment */
1334  *value = ptr[offset];
1335  break;
1336  }
1337  case PT_64BF: {
1338  double *ptr = (double*) data; /* we assume correct alignment */
1339  *value = ptr[offset];
1340  break;
1341  }
1342  default: {
1343  rterror("rt_band_get_pixel: Unknown pixeltype %d", pixtype);
1344  return ES_ERROR;
1345  }
1346  }
1347 
1348  /* set NODATA flag */
1349  if (band->hasnodata && nodata != NULL) {
1351  *nodata = 1;
1352  }
1353 
1354  return ES_NONE;
1355 }
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:199
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:295
rt_pixtype
Definition: librtcore.h:185
@ PT_32BUI
Definition: librtcore.h:194
@ PT_2BUI
Definition: librtcore.h:187
@ PT_32BSI
Definition: librtcore.h:193
@ PT_END
Definition: librtcore.h:197
@ PT_4BUI
Definition: librtcore.h:188
@ PT_32BF
Definition: librtcore.h:195
@ PT_1BB
Definition: librtcore.h:186
@ PT_16BUI
Definition: librtcore.h:192
@ PT_8BSI
Definition: librtcore.h:189
@ PT_16BSI
Definition: librtcore.h:191
@ PT_64BF
Definition: librtcore.h:196
@ PT_8BUI
Definition: librtcore.h:190
void rtwarn(const char *fmt,...)
Definition: rt_context.c:224
@ ES_NONE
Definition: librtcore.h:180
@ ES_ERROR
Definition: librtcore.h:181
int value
Definition: genraster.py:61
band
Definition: ovdump.py:57
data
Definition: ovdump.py:103
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:1798
unsigned int uint32_t
Definition: uthash.h:78
unsigned char uint8_t
Definition: uthash.h:79

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_dumpValues(), 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_of_value(), 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: