Retrieve a point value from the raster using a world coordinate and bilinear interpolation.
1415{
1416 double xcenter, ycenter;
1417 double values[2][2];
1418 double nodatavalue = 0.0;
1419 int nodatas[2][2];
1422 int xcell, ycell;
1423 int xdir, ydir;
1424 int i, j;
1425 uint16_t width, height;
1426
1427
1428 xcell = (int)floor(xr);
1429 ycell = (int)floor(yr);
1430 xcenter = xcell + 0.5;
1431 ycenter = ycell + 0.5;
1432
1433
1436
1437
1438 if(xcell < 0 || ycell < 0 || xcell >= width || ycell >= height) {
1439 rtwarn(
"Attempting to get pixel value with out of range raster coordinates: (%d, %d)", xcell, ycell);
1441 }
1442
1443
1444 xdir = xr < xcenter ? 1 : 0;
1445 ydir = yr < ycenter ? 1 : 0;
1446
1449 }
1450 else {
1451 nodatavalue = 0.0;
1452 }
1453
1454
1455 for (i = 0; i < 2; i++) {
1456 for (j = 0; j < 2; j++) {
1457 double value = nodatavalue;
1458 int nodata = 0;
1459 int xij = xcell + (i - xdir);
1460 int yij = ycell + (j - ydir);
1461
1462 if(xij < 0 || yij < 0 || xij >= width || yij >= height) {
1463 nodata = 1;
1464 }
1465 else {
1467 band, xij, yij,
1468 &value, &nodata
1469 );
1471 nodata = 1;
1472 }
1475 values[i][j] =
value;
1476 nodatas[i][j] = nodata;
1477 }
1478 }
1479
1480
1481 if (nodatas[xdir][ydir]) {
1482 *r_value = nodatavalue;
1483 *r_nodata = 1;
1485 }
1486
1487
1488
1489 xr = xr - (
x[0][0] + 0.5);
1490 yr = yr - (
y[0][0] + 0.5);
1491
1492
1493
1494
1495 for (i = 0; i < 2; i++) {
1496 for (j = 0; j < 2; j++) {
1497 if (nodatas[i][j])
1498 values[i][j] = values[xdir][ydir];
1499 }
1500 }
1501
1502
1503
1504 *r_nodata = 0;
1505 *r_value = values[0][0] * (1-xr) * (1-yr) +
1506 values[1][0] * (1-yr) * xr +
1507 values[0][1] * (1-xr) * yr +
1508 values[1][1] * xr * yr;
1509
1511}
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
rt_errorstate
Enum definitions.
uint16_t rt_band_get_width(rt_band band)
Return width of this band.
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
uint16_t rt_band_get_height(rt_band band)
Return height of this band.