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

◆ RASTER_pixelOfValue()

Datum RASTER_pixelOfValue ( PG_FUNCTION_ARGS  )

Definition at line 1817 of file rtpg_pixel.c.

1818{
1819 FuncCallContext *funcctx;
1820 TupleDesc tupdesc;
1821
1822 rt_pixel pixels = NULL;
1823 rt_pixel pixels2 = NULL;
1824 int count = 0;
1825 int i = 0;
1826 int n = 0;
1827 int call_cntr;
1828 int max_calls;
1829
1830 if (SRF_IS_FIRSTCALL()) {
1831 MemoryContext oldcontext;
1832
1833 rt_pgraster *pgraster = NULL;
1834 rt_raster raster = NULL;
1835 rt_band band = NULL;
1836 int nband = 1;
1837 int num_bands = 0;
1838 double *search = NULL;
1839 int nsearch = 0;
1840 double val;
1841 bool exclude_nodata_value = TRUE;
1842
1843 ArrayType *array;
1844 Oid etype;
1845 Datum *e;
1846 bool *nulls;
1847 int16 typlen;
1848 bool typbyval;
1849 char typalign;
1850
1851 /* create a function context for cross-call persistence */
1852 funcctx = SRF_FIRSTCALL_INIT();
1853
1854 /* switch to memory context appropriate for multiple function calls */
1855 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1856
1857 if (PG_ARGISNULL(0)) {
1858 MemoryContextSwitchTo(oldcontext);
1859 SRF_RETURN_DONE(funcctx);
1860 }
1861 pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1862 raster = rt_raster_deserialize(pgraster, FALSE);
1863 if (!raster) {
1864 PG_FREE_IF_COPY(pgraster, 0);
1865 MemoryContextSwitchTo(oldcontext);
1866 elog(ERROR, "RASTER_pixelOfValue: Could not deserialize raster");
1867 SRF_RETURN_DONE(funcctx);
1868 }
1869
1870 /* num_bands */
1871 num_bands = rt_raster_get_num_bands(raster);
1872 if (num_bands < 1) {
1873 elog(NOTICE, "Raster provided has no bands");
1874 rt_raster_destroy(raster);
1875 PG_FREE_IF_COPY(pgraster, 0);
1876 MemoryContextSwitchTo(oldcontext);
1877 SRF_RETURN_DONE(funcctx);
1878 }
1879
1880 /* band index is 1-based */
1881 if (!PG_ARGISNULL(1))
1882 nband = PG_GETARG_INT32(1);
1883 if (nband < 1 || nband > num_bands) {
1884 elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
1885 rt_raster_destroy(raster);
1886 PG_FREE_IF_COPY(pgraster, 0);
1887 MemoryContextSwitchTo(oldcontext);
1888 SRF_RETURN_DONE(funcctx);
1889 }
1890
1891 /* search values */
1892 array = PG_GETARG_ARRAYTYPE_P(2);
1893 etype = ARR_ELEMTYPE(array);
1894 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1895
1896 switch (etype) {
1897 case FLOAT4OID:
1898 case FLOAT8OID:
1899 break;
1900 default:
1901 rt_raster_destroy(raster);
1902 PG_FREE_IF_COPY(pgraster, 0);
1903 MemoryContextSwitchTo(oldcontext);
1904 elog(ERROR, "RASTER_pixelOfValue: Invalid data type for pixel values");
1905 SRF_RETURN_DONE(funcctx);
1906 break;
1907 }
1908
1909 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1910 &nulls, &n);
1911
1912 search = palloc(sizeof(double) * n);
1913 for (i = 0, nsearch = 0; i < n; i++) {
1914 if (nulls[i]) continue;
1915
1916 switch (etype) {
1917 case FLOAT4OID:
1918 val = (double) DatumGetFloat4(e[i]);
1919 break;
1920 case FLOAT8OID:
1921 val = (double) DatumGetFloat8(e[i]);
1922 break;
1923 }
1924
1925 search[nsearch] = val;
1926 POSTGIS_RT_DEBUGF(3, "search[%d] = %f", nsearch, search[nsearch]);
1927 nsearch++;
1928 }
1929
1930 /* not searching for anything */
1931 if (nsearch < 1) {
1932 elog(NOTICE, "No search values provided. Returning NULL");
1933 pfree(search);
1934 rt_raster_destroy(raster);
1935 PG_FREE_IF_COPY(pgraster, 0);
1936 MemoryContextSwitchTo(oldcontext);
1937 SRF_RETURN_DONE(funcctx);
1938 }
1939 else if (nsearch < n)
1940 search = repalloc(search, sizeof(double) * nsearch);
1941
1942 /* exclude_nodata_value flag */
1943 if (!PG_ARGISNULL(3))
1944 exclude_nodata_value = PG_GETARG_BOOL(3);
1945
1946 /* get band */
1947 band = rt_raster_get_band(raster, nband - 1);
1948 if (!band) {
1949 elog(NOTICE, "Could not find band at index %d. Returning NULL", nband);
1950 rt_raster_destroy(raster);
1951 PG_FREE_IF_COPY(pgraster, 0);
1952 MemoryContextSwitchTo(oldcontext);
1953 SRF_RETURN_DONE(funcctx);
1954 }
1955
1956 /* get pixels of values */
1958 band, exclude_nodata_value,
1959 search, nsearch,
1960 &pixels
1961 );
1962 pfree(search);
1963 rt_band_destroy(band);
1964 rt_raster_destroy(raster);
1965 PG_FREE_IF_COPY(pgraster, 0);
1966 if (count < 1) {
1967 /* error */
1968 if (count < 0)
1969 elog(NOTICE, "Could not get the pixels of search values for band at index %d", nband);
1970 /* no nearest pixel */
1971 else
1972 elog(NOTICE, "No pixels of search values found for band at index %d", nband);
1973
1974 MemoryContextSwitchTo(oldcontext);
1975 SRF_RETURN_DONE(funcctx);
1976 }
1977
1978 /* Store needed information */
1979 funcctx->user_fctx = pixels;
1980
1981 /* total number of tuples to be returned */
1982 funcctx->max_calls = count;
1983
1984 /* Build a tuple descriptor for our result type */
1985 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
1986 ereport(ERROR, (
1987 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1988 errmsg(
1989 "function returning record called in context "
1990 "that cannot accept type record"
1991 )
1992 ));
1993 }
1994
1995 BlessTupleDesc(tupdesc);
1996 funcctx->tuple_desc = tupdesc;
1997
1998 MemoryContextSwitchTo(oldcontext);
1999 }
2000
2001 /* stuff done on every call of the function */
2002 funcctx = SRF_PERCALL_SETUP();
2003
2004 call_cntr = funcctx->call_cntr;
2005 max_calls = funcctx->max_calls;
2006 tupdesc = funcctx->tuple_desc;
2007 pixels2 = funcctx->user_fctx;
2008
2009 /* do when there is more left to send */
2010 if (call_cntr < max_calls) {
2011 Datum values[VALUES_LENGTH];
2012 bool nulls[VALUES_LENGTH];
2013 HeapTuple tuple;
2014 Datum result;
2015
2016 memset(nulls, FALSE, sizeof(bool) * VALUES_LENGTH);
2017
2018 /* 0-based to 1-based */
2019 pixels2[call_cntr].x += 1;
2020 pixels2[call_cntr].y += 1;
2021
2022 values[0] = Float8GetDatum(pixels2[call_cntr].value);
2023 values[1] = Int32GetDatum(pixels2[call_cntr].x);
2024 values[2] = Int32GetDatum(pixels2[call_cntr].y);
2025
2026 /* build a tuple */
2027 tuple = heap_form_tuple(tupdesc, values, nulls);
2028
2029 /* make the tuple into a datum */
2030 result = HeapTupleGetDatum(tuple);
2031
2032 SRF_RETURN_NEXT(funcctx, result);
2033 }
2034 else {
2035 pfree(pixels2);
2036 SRF_RETURN_DONE(funcctx);
2037 }
2038}
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition cu_print.c:267
#define TRUE
Definition dbfopen.c:73
#define FALSE
Definition dbfopen.c:72
int rt_band_get_pixel_of_value(rt_band band, int exclude_nodata_value, double *searchset, int searchcount, rt_pixel *pixels)
Search band for pixel(s) with search values.
Definition rt_band.c:1989
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition rt_raster.c:86
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition rt_band.c:499
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition rt_raster.c:376
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition rt_raster.c:385
int count
Definition genraster.py:57
nband
Definition pixval.py:56
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition rtrowdump.py:125
#define VALUES_LENGTH
Definition rtpg_pixel.c:399
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition rtpostgis.h:69
Struct definitions.
Definition librtcore.h:2452

References FALSE, POSTGIS_RT_DEBUGF, result, rt_band_destroy(), rt_band_get_pixel_of_value(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_band(), rt_raster_get_num_bands(), TRUE, VALUES_LENGTH, rt_pixel_t::x, and rt_pixel_t::y.

Here is the call graph for this function: