Get nearest pixel(s) with value (not NODATA) to specified pixel.
1380 {
1382 int extent[4] = {0};
1383 int max_extent[4] = {0};
1384 int d0 = 0;
1386 uint32_t _d[2] = {0};
1387 uint32_t i = 0;
1388 uint32_t j = 0;
1389 uint32_t k = 0;
1390 int _max = 0;
1391 int _x = 0;
1392 int _y = 0;
1393 int *_min = NULL;
1395 double minval = 0;
1397 int isnodata = 0;
1398
1399 int inextent = 0;
1400
1401 assert(NULL != band);
1402 assert(NULL != npixels);
1403
1405
1406
1409
1410
1412 d0 = 1;
1413
1416
1417
1418 if (
1419 exclude_nodata_value && (
1420 (x < 0 || x >
band->width) ||
1421 (y < 0 || y >
band->height)
1422 )
1423 ) {
1424
1425 if (d0) {
1426 if (x < 0)
1428 else if (x >
band->width)
1430
1431 if (y < 0)
1433 else if (y >
band->height)
1435
1437 }
1438
1439
1440
1441
1442 else if (
1445 ) {
1446 RASTER_DEBUG(4,
"No nearest pixels possible for provided pixel and distances");
1447 return 0;
1448 }
1449 }
1450
1451
1452 if (!
band->hasnodata)
1453 exclude_nodata_value =
FALSE;
1454
1455 else if (exclude_nodata_value &&
band->isnodata) {
1456 RASTER_DEBUG(4,
"No nearest pixels possible as band is NODATA and excluding NODATA values");
1457 return 0;
1458 }
1459
1460
1461 if (d0) {
1462 int a, b;
1463
1464
1465 a = abs(x);
1466 b = abs(x -
band->width);
1467
1468 if (a > b)
1470 else
1472
1473
1474 a = abs(y);
1475 b = abs(y -
band->height);
1476 if (a > b)
1478 else
1480
1482 }
1483
1484
1488
1489
1491 *npixels = NULL;
1492
1493
1499 max_extent[0], max_extent[1], max_extent[2], max_extent[3]);
1500
1501 _d[0] = 0;
1502 _d[1] = 0;
1503 do {
1504 _d[0]++;
1505 _d[1]++;
1506
1507 extent[0] =
x - (int)_d[0];
1508 extent[1] =
y - (int)_d[1];
1509 extent[2] =
x + (int)_d[0];
1510 extent[3] =
y + (int)_d[1];
1511
1512 RASTER_DEBUGF(4,
"Processing distances: %d x %d", _d[0], _d[1]);
1514 extent[0], extent[1], extent[2], extent[3]);
1515
1516 for (i = 0; i < 2; i++) {
1517
1518
1519 if (i < 1)
1520 _max = extent[2] - extent[0] + 1;
1521
1522 else
1523 _max = extent[3] - extent[1] + 1;
1524 _max = abs(_max);
1525
1526 for (j = 0; j < 2; j++) {
1527
1528 if (i < 1) {
1529 _x = extent[0];
1530 _min = &_x;
1531
1532
1533 if (j < 1)
1534 _y = extent[1];
1535
1536 else
1537 _y = extent[3];
1538 }
1539
1540 else {
1541 _y = extent[1] + 1;
1542 _min = &_y;
1543
1544
1545 if (j < 1) {
1546 _x = extent[0];
1547 _max -= 2;
1548 }
1549
1550 else
1551 _x = extent[2];
1552 }
1553
1555 for (k = 0; k < (uint32_t) _max; k++) {
1556
1557 if (
1558 _x < max_extent[0] || _x > max_extent[2] ||
1559 _y < max_extent[1] || _y > max_extent[3]
1560 ) {
1561 (*_min)++;
1562 continue;
1563 }
1564
1565
1566 if (
1567 (_x < 0 || _x >=
band->width) ||
1568 (_y < 0 || _y >=
band->height)
1569 ) {
1570
1571 if (!
band->hasnodata)
1573
1574 else
1576 RASTER_DEBUGF(4,
"NODATA pixel outside band extent: (x, y, val) = (%d, %d, %f)", _x, _y,
pixval);
1577 inextent = 0;
1578 isnodata = 1;
1579 }
1580 else {
1582 band,
1583 _x, _y,
1585 &isnodata
1587 rterror(
"rt_band_get_nearest_pixel: Could not get pixel value");
1589 return -1;
1590 }
1592 inextent = 1;
1593 }
1594
1595
1596 if (!exclude_nodata_value || (exclude_nodata_value && !isnodata)) {
1597
1598 RASTER_DEBUGF(4,
"Adding pixel to set of nearest pixels: (x, y, val) = (%d, %d, %f)", _x, _y,
pixval);
1600
1601 if (*npixels == NULL)
1603 else
1605 if (*npixels == NULL) {
1606 rterror(
"rt_band_get_nearest_pixel: Could not allocate memory for nearest pixel(s)");
1607 return -1;
1608 }
1609
1610 npixel = &((*npixels)[
count - 1]);
1614
1615
1616 if (!inextent && !
band->hasnodata)
1618 else
1620 }
1621
1622 (*_min)++;
1623 }
1624 }
1625 }
1626
1627
1629 break;
1630 else if (d0 && count)
1631 break;
1632 }
1633 while (1);
1634
1636
1638}
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
#define RASTER_DEBUG(level, msg)
#define RASTER_DEBUGF(level, msg,...)
double rt_pixtype_get_min_value(rt_pixtype pixtype)
Return minimum value possible for pixel type.
struct rt_pixel_t * rt_pixel
const char * rt_pixtype_name(rt_pixtype pixtype)
void * rtrealloc(void *mem, size_t size)
void rtdealloc(void *mem)
static double distance(double x1, double y1, double x2, double y2)
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.