1819 FuncCallContext *funcctx;
1830 if (SRF_IS_FIRSTCALL()) {
1831 MemoryContext oldcontext;
1838 double *search = NULL;
1841 bool exclude_nodata_value =
TRUE;
1852 funcctx = SRF_FIRSTCALL_INIT();
1855 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1857 if (PG_ARGISNULL(0)) {
1858 MemoryContextSwitchTo(oldcontext);
1859 SRF_RETURN_DONE(funcctx);
1861 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1864 PG_FREE_IF_COPY(pgraster, 0);
1865 MemoryContextSwitchTo(oldcontext);
1866 elog(ERROR,
"RASTER_pixelOfValue: Could not deserialize raster");
1867 SRF_RETURN_DONE(funcctx);
1872 if (num_bands < 1) {
1873 elog(NOTICE,
"Raster provided has no bands");
1875 PG_FREE_IF_COPY(pgraster, 0);
1876 MemoryContextSwitchTo(oldcontext);
1877 SRF_RETURN_DONE(funcctx);
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");
1886 PG_FREE_IF_COPY(pgraster, 0);
1887 MemoryContextSwitchTo(oldcontext);
1888 SRF_RETURN_DONE(funcctx);
1892 array = PG_GETARG_ARRAYTYPE_P(2);
1893 etype = ARR_ELEMTYPE(array);
1894 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
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);
1909 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1912 search = palloc(
sizeof(
double) * n);
1913 for (i = 0, nsearch = 0; i < n; i++) {
1914 if (nulls[i])
continue;
1918 val = (double) DatumGetFloat4(e[i]);
1921 val = (double) DatumGetFloat8(e[i]);
1925 search[nsearch] = val;
1932 elog(NOTICE,
"No search values provided. Returning NULL");
1935 PG_FREE_IF_COPY(pgraster, 0);
1936 MemoryContextSwitchTo(oldcontext);
1937 SRF_RETURN_DONE(funcctx);
1939 else if (nsearch < n)
1940 search = repalloc(search,
sizeof(
double) * nsearch);
1943 if (!PG_ARGISNULL(3))
1944 exclude_nodata_value = PG_GETARG_BOOL(3);
1949 elog(NOTICE,
"Could not find band at index %d. Returning NULL",
nband);
1951 PG_FREE_IF_COPY(pgraster, 0);
1952 MemoryContextSwitchTo(oldcontext);
1953 SRF_RETURN_DONE(funcctx);
1958 band, exclude_nodata_value,
1965 PG_FREE_IF_COPY(pgraster, 0);
1969 elog(NOTICE,
"Could not get the pixels of search values for band at index %d",
nband);
1972 elog(NOTICE,
"No pixels of search values found for band at index %d",
nband);
1974 MemoryContextSwitchTo(oldcontext);
1975 SRF_RETURN_DONE(funcctx);
1979 funcctx->user_fctx = pixels;
1982 funcctx->max_calls =
count;
1985 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
1987 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1989 "function returning record called in context "
1990 "that cannot accept type record"
1995 BlessTupleDesc(tupdesc);
1996 funcctx->tuple_desc = tupdesc;
1998 MemoryContextSwitchTo(oldcontext);
2002 funcctx = SRF_PERCALL_SETUP();
2004 call_cntr = funcctx->call_cntr;
2005 max_calls = funcctx->max_calls;
2006 tupdesc = funcctx->tuple_desc;
2007 pixels2 = funcctx->user_fctx;
2010 if (call_cntr < max_calls) {
2019 pixels2[call_cntr].
x += 1;
2020 pixels2[call_cntr].
y += 1;
2022 values[0] = Float8GetDatum(pixels2[call_cntr].
value);
2023 values[1] = Int32GetDatum(pixels2[call_cntr].
x);
2024 values[2] = Int32GetDatum(pixels2[call_cntr].
y);
2027 tuple = heap_form_tuple(tupdesc, values, nulls);
2030 result = HeapTupleGetDatum(tuple);
2032 SRF_RETURN_NEXT(funcctx,
result);
2036 SRF_RETURN_DONE(funcctx);
char result[OUT_DOUBLE_BUFFER_SIZE]
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.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
void rt_band_destroy(rt_band band)
Destroy a raster band.
uint16_t rt_raster_get_num_bands(rt_raster raster)
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.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
#define POSTGIS_RT_DEBUGF(level, msg,...)