PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ rt_band_get_nearest_pixel()

uint32_t rt_band_get_nearest_pixel ( rt_band  band,
int  x,
int  y,
uint16_t  distancex,
uint16_t  distancey,
int  exclude_nodata_value,
rt_pixel npixels 
)

Get nearest pixel(s) with value (not NODATA) to specified pixel.

Parameters
band: the band to get nearest pixel(s) from
x: pixel column (0-based)
y: pixel row (0-based)
distancex: the number of pixels around the specified pixel along the X axis
distancey: the number of pixels around the specified pixel along the Y axis
exclude_nodata_value: if non-zero, ignore nodata values to check for pixels with value
npixels: return set of rt_pixel object or NULL
Returns
-1 on error, otherwise the number of rt_pixel objects in npixels
Parameters
band: the band to get nearest pixel(s) from
x: the column of the pixel (0-based)
y: the line of the pixel (0-based)
distancex: the number of pixels around the specified pixel along the X axis
distancey: the number of pixels around the specified pixel along the Y axis
exclude_nodata_value: if non-zero, ignore nodata values to check for pixels with value
npixels: return set of rt_pixel object or NULL
Returns
-1 on error, otherwise the number of rt_pixel objects in npixels

Definition at line 1709 of file rt_band.c.

1715 {
1716 rt_pixel npixel = NULL;
1717 int extent[4] = {0};
1718 int max_extent[4] = {0};
1719 int d0 = 0;
1720 uint32_t distance[2] = {0};
1721 uint32_t _d[2] = {0};
1722 uint32_t i = 0;
1723 uint32_t j = 0;
1724 uint32_t k = 0;
1725 int _max = 0;
1726 int _x = 0;
1727 int _y = 0;
1728 int *_min = NULL;
1729 double pixval = 0;
1730 double minval = 0;
1731 uint32_t count = 0;
1732 int isnodata = 0;
1733
1734 int inextent = 0;
1735
1736 assert(NULL != band);
1737 assert(NULL != npixels);
1738
1739 RASTER_DEBUG(3, "Starting");
1740
1741 /* process distance */
1742 distance[0] = distancex;
1743 distance[1] = distancey;
1744
1745 /* no distance, means get nearest pixels and return */
1746 if (!distance[0] && !distance[1])
1747 d0 = 1;
1748
1749 RASTER_DEBUGF(4, "Selected pixel: %d x %d", x, y);
1750 RASTER_DEBUGF(4, "Distances: %d x %d", distance[0], distance[1]);
1751
1752 /* shortcuts if outside band extent */
1753 if (
1754 exclude_nodata_value && (
1755 (x < 0 || x > band->width) ||
1756 (y < 0 || y > band->height)
1757 )
1758 ) {
1759 /* no distances specified, jump to pixel close to extent */
1760 if (d0) {
1761 if (x < 0)
1762 x = -1;
1763 else if (x > band->width)
1764 x = band->width;
1765
1766 if (y < 0)
1767 y = -1;
1768 else if (y > band->height)
1769 y = band->height;
1770
1771 RASTER_DEBUGF(4, "Moved selected pixel: %d x %d", x, y);
1772 }
1773 /*
1774 distances specified
1775 if distances won't capture extent of band, return 0
1776 */
1777 else if (
1778 ((x < 0 && (uint32_t) abs(x) > distance[0]) || (x - band->width >= (int)distance[0])) ||
1779 ((y < 0 && (uint32_t) abs(y) > distance[1]) || (y - band->height >= (int)distance[1]))
1780 ) {
1781 RASTER_DEBUG(4, "No nearest pixels possible for provided pixel and distances");
1782 return 0;
1783 }
1784 }
1785
1786 /* no NODATA, exclude is FALSE */
1787 if (!band->hasnodata)
1788 exclude_nodata_value = FALSE;
1789 /* band is NODATA and excluding NODATA */
1790 else if (exclude_nodata_value && band->isnodata) {
1791 RASTER_DEBUG(4, "No nearest pixels possible as band is NODATA and excluding NODATA values");
1792 return 0;
1793 }
1794
1795 /* determine the maximum distance to prevent an infinite loop */
1796 if (d0) {
1797 int a, b;
1798
1799 /* X axis */
1800 a = abs(x);
1801 b = abs(x - band->width);
1802
1803 if (a > b)
1804 distance[0] = a;
1805 else
1806 distance[0] = b;
1807
1808 /* Y axis */
1809 a = abs(y);
1810 b = abs(y - band->height);
1811 if (a > b)
1812 distance[1] = a;
1813 else
1814 distance[1] = b;
1815
1816 RASTER_DEBUGF(4, "Maximum distances: %d x %d", distance[0], distance[1]);
1817 }
1818
1819 /* minimum possible value for pixel type */
1820 minval = rt_pixtype_get_min_value(band->pixtype);
1821 RASTER_DEBUGF(4, "pixtype: %s", rt_pixtype_name(band->pixtype));
1822 RASTER_DEBUGF(4, "minval: %f", minval);
1823
1824 /* set variables */
1825 count = 0;
1826 *npixels = NULL;
1827
1828 /* maximum extent */
1829 max_extent[0] = x - (int)distance[0]; /* min X */
1830 max_extent[1] = y - (int)distance[1]; /* min Y */
1831 max_extent[2] = x + (int)distance[0]; /* max X */
1832 max_extent[3] = y + (int)distance[1]; /* max Y */
1833 RASTER_DEBUGF(4, "Maximum Extent: (%d, %d, %d, %d)",
1834 max_extent[0], max_extent[1], max_extent[2], max_extent[3]);
1835
1836 _d[0] = 0;
1837 _d[1] = 0;
1838 do {
1839 _d[0]++;
1840 _d[1]++;
1841
1842 extent[0] = x - (int)_d[0]; /* min x */
1843 extent[1] = y - (int)_d[1]; /* min y */
1844 extent[2] = x + (int)_d[0]; /* max x */
1845 extent[3] = y + (int)_d[1]; /* max y */
1846
1847 RASTER_DEBUGF(4, "Processing distances: %d x %d", _d[0], _d[1]);
1848 RASTER_DEBUGF(4, "Extent: (%d, %d, %d, %d)",
1849 extent[0], extent[1], extent[2], extent[3]);
1850
1851 for (i = 0; i < 2; i++) {
1852
1853 /* by row */
1854 if (i < 1)
1855 _max = extent[2] - extent[0] + 1;
1856 /* by column */
1857 else
1858 _max = extent[3] - extent[1] + 1;
1859 _max = abs(_max);
1860
1861 for (j = 0; j < 2; j++) {
1862 /* by row */
1863 if (i < 1) {
1864 _x = extent[0];
1865 _min = &_x;
1866
1867 /* top row */
1868 if (j < 1)
1869 _y = extent[1];
1870 /* bottom row */
1871 else
1872 _y = extent[3];
1873 }
1874 /* by column */
1875 else {
1876 _y = extent[1] + 1;
1877 _min = &_y;
1878
1879 /* left column */
1880 if (j < 1) {
1881 _x = extent[0];
1882 _max -= 2;
1883 }
1884 /* right column */
1885 else
1886 _x = extent[2];
1887 }
1888
1889 RASTER_DEBUGF(4, "_min, _max: %d, %d", *_min, _max);
1890 for (k = 0; k < (uint32_t) _max; k++) {
1891 /* check that _x and _y are not outside max extent */
1892 if (
1893 _x < max_extent[0] || _x > max_extent[2] ||
1894 _y < max_extent[1] || _y > max_extent[3]
1895 ) {
1896 (*_min)++;
1897 continue;
1898 }
1899
1900 /* outside band extent, set to NODATA */
1901 if (
1902 (_x < 0 || _x >= band->width) ||
1903 (_y < 0 || _y >= band->height)
1904 ) {
1905 /* no NODATA, set to minimum possible value */
1906 if (!band->hasnodata)
1907 pixval = minval;
1908 /* has NODATA, use NODATA */
1909 else
1910 pixval = band->nodataval;
1911 RASTER_DEBUGF(4, "NODATA pixel outside band extent: (x, y, val) = (%d, %d, %f)", _x, _y, pixval);
1912 inextent = 0;
1913 isnodata = 1;
1914 }
1915 else {
1917 band,
1918 _x, _y,
1919 &pixval,
1920 &isnodata
1921 ) != ES_NONE) {
1922 rterror("rt_band_get_nearest_pixel: Could not get pixel value");
1923 if (count) rtdealloc(*npixels);
1924 return -1;
1925 }
1926 RASTER_DEBUGF(4, "Pixel: (x, y, val) = (%d, %d, %f)", _x, _y, pixval);
1927 inextent = 1;
1928 }
1929
1930 /* use pixval? */
1931 if (!exclude_nodata_value || (exclude_nodata_value && !isnodata)) {
1932 /* add pixel to result set */
1933 RASTER_DEBUGF(4, "Adding pixel to set of nearest pixels: (x, y, val) = (%d, %d, %f)", _x, _y, pixval);
1934 count++;
1935
1936 if (*npixels == NULL)
1937 *npixels = (rt_pixel) rtalloc(sizeof(struct rt_pixel_t) * count);
1938 else
1939 *npixels = (rt_pixel) rtrealloc(*npixels, sizeof(struct rt_pixel_t) * count);
1940 if (*npixels == NULL) {
1941 rterror("rt_band_get_nearest_pixel: Could not allocate memory for nearest pixel(s)");
1942 return -1;
1943 }
1944
1945 npixel = &((*npixels)[count - 1]);
1946 npixel->x = _x;
1947 npixel->y = _y;
1948 npixel->value = pixval;
1949
1950 /* special case for when outside band extent */
1951 if (!inextent && !band->hasnodata)
1952 npixel->nodata = 1;
1953 else
1954 npixel->nodata = 0;
1955 }
1956
1957 (*_min)++;
1958 }
1959 }
1960 }
1961
1962 /* distance thresholds met */
1963 if (_d[0] >= distance[0] && _d[1] >= distance[1])
1964 break;
1965 else if (d0 && count)
1966 break;
1967 }
1968 while (1);
1969
1970 RASTER_DEBUGF(3, "Nearest pixels in return: %d", count);
1971
1972 return count;
1973}
#define FALSE
Definition dbfopen.c:72
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition rt_context.c:191
#define RASTER_DEBUG(level, msg)
Definition librtcore.h:304
#define RASTER_DEBUGF(level, msg,...)
Definition librtcore.h:308
double rt_pixtype_get_min_value(rt_pixtype pixtype)
Return minimum value possible for pixel type.
Definition rt_pixel.c:156
struct rt_pixel_t * rt_pixel
Definition librtcore.h:148
const char * rt_pixtype_name(rt_pixtype pixtype)
Definition rt_pixel.c:114
@ ES_NONE
Definition librtcore.h:182
void * rtrealloc(void *mem, size_t size)
Definition rt_context.c:199
void rtdealloc(void *mem)
Definition rt_context.c:206
static double distance(double x1, double y1, double x2, double y2)
Definition lwtree.c:1032
int count
Definition genraster.py:57
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
Definition rt_band.c:1551
double value
Definition librtcore.h:2540
uint8_t nodata
Definition librtcore.h:2539

References distance(), ES_NONE, FALSE, rt_pixel_t::nodata, RASTER_DEBUG, RASTER_DEBUGF, rt_band_get_pixel(), rt_pixtype_get_min_value(), rt_pixtype_name(), rtalloc(), rtdealloc(), rterror(), rtrealloc(), rt_pixel_t::value, rt_pixel_t::x, and rt_pixel_t::y.

Referenced by RASTER_nearestValue(), RASTER_neighborhood(), rt_raster_iterator(), test_band_get_nearest_pixel(), and test_pixel_set_to_array().

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