919 FuncCallContext *funcctx;
929 if (SRF_IS_FIRSTCALL()) {
930 MemoryContext oldcontext;
935 int32_t bandindex = 1;
937 bool exclude_nodata_value =
TRUE;
940 double *bin_width = NULL;
963 funcctx = SRF_FIRSTCALL_INIT();
966 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
969 if (PG_ARGISNULL(0)) {
970 MemoryContextSwitchTo(oldcontext);
971 SRF_RETURN_DONE(funcctx);
973 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
977 PG_FREE_IF_COPY(pgraster, 0);
978 MemoryContextSwitchTo(oldcontext);
979 elog(ERROR,
"RASTER_histogram: Cannot deserialize raster");
980 SRF_RETURN_DONE(funcctx);
984 if (!PG_ARGISNULL(1))
985 bandindex = PG_GETARG_INT32(1);
987 if (bandindex < 1 || bandindex > num_bands) {
988 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
990 PG_FREE_IF_COPY(pgraster, 0);
991 MemoryContextSwitchTo(oldcontext);
992 SRF_RETURN_DONE(funcctx);
996 if (!PG_ARGISNULL(2))
997 exclude_nodata_value = PG_GETARG_BOOL(2);
1000 if (!PG_ARGISNULL(3)) {
1001 sample = PG_GETARG_FLOAT8(3);
1002 if (sample < 0 || sample > 1) {
1003 elog(NOTICE,
"Invalid sample percentage (must be between 0 and 1). Returning NULL");
1005 PG_FREE_IF_COPY(pgraster, 0);
1006 MemoryContextSwitchTo(oldcontext);
1007 SRF_RETURN_DONE(funcctx);
1009 else if (
FLT_EQ(sample, 0.0))
1016 if (!PG_ARGISNULL(4)) {
1017 bin_count = PG_GETARG_INT32(4);
1018 if (bin_count < 1) bin_count = 0;
1022 if (!PG_ARGISNULL(5)) {
1023 array = PG_GETARG_ARRAYTYPE_P(5);
1024 etype = ARR_ELEMTYPE(array);
1025 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1033 PG_FREE_IF_COPY(pgraster, 0);
1034 MemoryContextSwitchTo(oldcontext);
1035 elog(ERROR,
"RASTER_histogram: Invalid data type for width");
1036 SRF_RETURN_DONE(funcctx);
1040 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1043 bin_width = palloc(
sizeof(
double) * n);
1044 for (i = 0, j = 0; i < n; i++) {
1045 if (nulls[i])
continue;
1049 width = (double) DatumGetFloat4(e[i]);
1052 width = (double) DatumGetFloat8(e[i]);
1056 if (width < 0 ||
FLT_EQ(width, 0.0)) {
1057 elog(NOTICE,
"Invalid value for width (must be greater than 0). Returning NULL");
1060 PG_FREE_IF_COPY(pgraster, 0);
1061 MemoryContextSwitchTo(oldcontext);
1062 SRF_RETURN_DONE(funcctx);
1065 bin_width[j] = width;
1069 bin_width_count = j;
1078 if (!PG_ARGISNULL(6))
1079 right = PG_GETARG_BOOL(6);
1082 if (!PG_ARGISNULL(7)) min = PG_GETARG_FLOAT8(7);
1085 if (!PG_ARGISNULL(8)) max = PG_GETARG_FLOAT8(8);
1090 elog(NOTICE,
"Cannot find band at index %d. Returning NULL", bandindex);
1092 PG_FREE_IF_COPY(pgraster, 0);
1093 MemoryContextSwitchTo(oldcontext);
1094 SRF_RETURN_DONE(funcctx);
1101 PG_FREE_IF_COPY(pgraster, 0);
1102 if (NULL == stats || NULL == stats->
values) {
1103 elog(NOTICE,
"Cannot compute summary statistics for band at index %d", bandindex);
1104 MemoryContextSwitchTo(oldcontext);
1105 SRF_RETURN_DONE(funcctx);
1107 else if (stats->
count < 1) {
1108 elog(NOTICE,
"Cannot compute histogram for band at index %d as the band has no values", bandindex);
1109 MemoryContextSwitchTo(oldcontext);
1110 SRF_RETURN_DONE(funcctx);
1115 if (bin_width_count) pfree(bin_width);
1117 if (NULL == hist || !
count) {
1118 elog(NOTICE,
"Cannot compute histogram for band at index %d", bandindex);
1119 MemoryContextSwitchTo(oldcontext);
1120 SRF_RETURN_DONE(funcctx);
1126 funcctx->user_fctx = hist;
1129 funcctx->max_calls =
count;
1132 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
1134 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1136 "function returning record called in context "
1137 "that cannot accept type record"
1142 BlessTupleDesc(tupdesc);
1143 funcctx->tuple_desc = tupdesc;
1145 MemoryContextSwitchTo(oldcontext);
1149 funcctx = SRF_PERCALL_SETUP();
1151 call_cntr = funcctx->call_cntr;
1152 max_calls = funcctx->max_calls;
1153 tupdesc = funcctx->tuple_desc;
1154 hist2 = funcctx->user_fctx;
1157 if (call_cntr < max_calls) {
1167 values[0] = Float8GetDatum(hist2[call_cntr].min);
1168 values[1] = Float8GetDatum(hist2[call_cntr].max);
1169 values[2] = Int64GetDatum(hist2[call_cntr].
count);
1170 values[3] = Float8GetDatum(hist2[call_cntr].percent);
1173 tuple = heap_form_tuple(tupdesc, values, nulls);
1176 result = HeapTupleGetDatum(tuple);
1178 SRF_RETURN_NEXT(funcctx, result);
1183 SRF_RETURN_DONE(funcctx);
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_histogram rt_band_get_histogram(rt_bandstats stats, uint32_t bin_count, double *bin_widths, uint32_t bin_widths_count, int right, double min, double max, uint32_t *rtn_count)
Count the distribution of data.
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.
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_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)