1730 FuncCallContext *funcctx;
1740 if (SRF_IS_FIRSTCALL()) {
1741 MemoryContext oldcontext;
1746 int32_t bandindex = 0;
1748 bool exclude_nodata_value =
TRUE;
1750 double *quantiles = NULL;
1768 funcctx = SRF_FIRSTCALL_INIT();
1771 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1774 if (PG_ARGISNULL(0)) {
1775 MemoryContextSwitchTo(oldcontext);
1776 SRF_RETURN_DONE(funcctx);
1778 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1782 PG_FREE_IF_COPY(pgraster, 0);
1783 MemoryContextSwitchTo(oldcontext);
1784 elog(ERROR,
"RASTER_quantile: Cannot deserialize raster");
1785 SRF_RETURN_DONE(funcctx);
1789 bandindex = PG_GETARG_INT32(1);
1791 if (bandindex < 1 || bandindex > num_bands) {
1792 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
1794 PG_FREE_IF_COPY(pgraster, 0);
1795 MemoryContextSwitchTo(oldcontext);
1796 SRF_RETURN_DONE(funcctx);
1800 if (!PG_ARGISNULL(2))
1801 exclude_nodata_value = PG_GETARG_BOOL(2);
1804 if (!PG_ARGISNULL(3)) {
1805 sample = PG_GETARG_FLOAT8(3);
1806 if (sample < 0 || sample > 1) {
1807 elog(NOTICE,
"Invalid sample percentage (must be between 0 and 1). Returning NULL");
1809 PG_FREE_IF_COPY(pgraster, 0);
1810 MemoryContextSwitchTo(oldcontext);
1811 SRF_RETURN_DONE(funcctx);
1813 else if (
FLT_EQ(sample, 0.0))
1820 if (!PG_ARGISNULL(4)) {
1821 array = PG_GETARG_ARRAYTYPE_P(4);
1822 etype = ARR_ELEMTYPE(array);
1823 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1831 PG_FREE_IF_COPY(pgraster, 0);
1832 MemoryContextSwitchTo(oldcontext);
1833 elog(ERROR,
"RASTER_quantile: Invalid data type for quantiles");
1834 SRF_RETURN_DONE(funcctx);
1838 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1841 quantiles = palloc(
sizeof(
double) * n);
1842 for (i = 0, j = 0; i < n; i++) {
1843 if (nulls[i])
continue;
1847 quantile = (double) DatumGetFloat4(e[i]);
1850 quantile = (double) DatumGetFloat8(e[i]);
1854 if (quantile < 0 || quantile > 1) {
1855 elog(NOTICE,
"Invalid value for quantile (must be between 0 and 1). Returning NULL");
1858 PG_FREE_IF_COPY(pgraster, 0);
1859 MemoryContextSwitchTo(oldcontext);
1860 SRF_RETURN_DONE(funcctx);
1867 quantiles_count = j;
1878 elog(NOTICE,
"Cannot find band at index %d. Returning NULL", bandindex);
1880 PG_FREE_IF_COPY(pgraster, 0);
1881 MemoryContextSwitchTo(oldcontext);
1882 SRF_RETURN_DONE(funcctx);
1889 PG_FREE_IF_COPY(pgraster, 0);
1890 if (NULL == stats || NULL == stats->
values) {
1891 elog(NOTICE,
"Cannot retrieve summary statistics for band at index %d", bandindex);
1892 MemoryContextSwitchTo(oldcontext);
1893 SRF_RETURN_DONE(funcctx);
1895 else if (stats->
count < 1) {
1896 elog(NOTICE,
"Cannot compute quantiles for band at index %d as the band has no values", bandindex);
1897 MemoryContextSwitchTo(oldcontext);
1898 SRF_RETURN_DONE(funcctx);
1903 if (quantiles_count) pfree(quantiles);
1905 if (NULL == quant || !count) {
1906 elog(NOTICE,
"Cannot compute quantiles for band at index %d", bandindex);
1907 MemoryContextSwitchTo(oldcontext);
1908 SRF_RETURN_DONE(funcctx);
1914 funcctx->user_fctx = quant;
1917 funcctx->max_calls =
count;
1920 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
1922 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1924 "function returning record called in context " 1925 "that cannot accept type record" 1930 BlessTupleDesc(tupdesc);
1931 funcctx->tuple_desc = tupdesc;
1933 MemoryContextSwitchTo(oldcontext);
1937 funcctx = SRF_PERCALL_SETUP();
1939 call_cntr = funcctx->call_cntr;
1940 max_calls = funcctx->max_calls;
1941 tupdesc = funcctx->tuple_desc;
1942 quant2 = funcctx->user_fctx;
1945 if (call_cntr < max_calls) {
1955 values[0] = Float8GetDatum(quant2[call_cntr].quantile);
1956 values[1] = Float8GetDatum(quant2[call_cntr].
value);
1959 tuple = heap_form_tuple(tupdesc, values, nulls);
1962 result = HeapTupleGetDatum(tuple);
1964 SRF_RETURN_NEXT(funcctx, result);
1969 SRF_RETURN_DONE(funcctx);
int rt_raster_get_num_bands(rt_raster raster)
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
void rt_band_destroy(rt_band band)
Destroy a raster band.
#define POSTGIS_RT_DEBUGF(level, msg,...)
rt_quantile rt_band_get_quantiles(rt_bandstats stats, double *quantiles, int quantiles_count, uint32_t *rtn_count)
Compute the default set of or requested quantiles for a set of data the quantile formula used is same...
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
rt_bandstats rt_band_get_summary_stats(rt_band band, int exclude_nodata_value, double sample, int inc_vals, uint64_t *cK, double *cM, double *cQ)
Compute summary statistics for a band.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.