923 FuncCallContext *funcctx;
933 if (SRF_IS_FIRSTCALL()) {
934 MemoryContext oldcontext;
939 int32_t bandindex = 1;
941 bool exclude_nodata_value =
TRUE;
944 double *bin_width = NULL;
967 funcctx = SRF_FIRSTCALL_INIT();
970 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
973 if (PG_ARGISNULL(0)) {
974 MemoryContextSwitchTo(oldcontext);
975 SRF_RETURN_DONE(funcctx);
977 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
981 PG_FREE_IF_COPY(pgraster, 0);
982 MemoryContextSwitchTo(oldcontext);
983 elog(ERROR,
"RASTER_histogram: Cannot deserialize raster");
984 SRF_RETURN_DONE(funcctx);
988 if (!PG_ARGISNULL(1))
989 bandindex = PG_GETARG_INT32(1);
991 if (bandindex < 1 || bandindex > num_bands) {
992 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
994 PG_FREE_IF_COPY(pgraster, 0);
995 MemoryContextSwitchTo(oldcontext);
996 SRF_RETURN_DONE(funcctx);
1000 if (!PG_ARGISNULL(2))
1001 exclude_nodata_value = PG_GETARG_BOOL(2);
1004 if (!PG_ARGISNULL(3)) {
1005 sample = PG_GETARG_FLOAT8(3);
1006 if (sample < 0 || sample > 1) {
1007 elog(NOTICE,
"Invalid sample percentage (must be between 0 and 1). Returning NULL");
1009 PG_FREE_IF_COPY(pgraster, 0);
1010 MemoryContextSwitchTo(oldcontext);
1011 SRF_RETURN_DONE(funcctx);
1013 else if (
FLT_EQ(sample, 0.0))
1020 if (!PG_ARGISNULL(4)) {
1021 bin_count = PG_GETARG_INT32(4);
1022 if (bin_count < 1) bin_count = 0;
1026 if (!PG_ARGISNULL(5)) {
1027 array = PG_GETARG_ARRAYTYPE_P(5);
1028 etype = ARR_ELEMTYPE(array);
1029 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1037 PG_FREE_IF_COPY(pgraster, 0);
1038 MemoryContextSwitchTo(oldcontext);
1039 elog(ERROR,
"RASTER_histogram: Invalid data type for width");
1040 SRF_RETURN_DONE(funcctx);
1044 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1047 bin_width = palloc(
sizeof(
double) * n);
1048 for (i = 0, j = 0; i < n; i++) {
1049 if (nulls[i])
continue;
1053 width = (double) DatumGetFloat4(e[i]);
1056 width = (double) DatumGetFloat8(e[i]);
1060 if (width < 0 ||
FLT_EQ(width, 0.0)) {
1061 elog(NOTICE,
"Invalid value for width (must be greater than 0). Returning NULL");
1064 PG_FREE_IF_COPY(pgraster, 0);
1065 MemoryContextSwitchTo(oldcontext);
1066 SRF_RETURN_DONE(funcctx);
1069 bin_width[j] = width;
1073 bin_width_count = j;
1082 if (!PG_ARGISNULL(6))
1083 right = PG_GETARG_BOOL(6);
1086 if (!PG_ARGISNULL(7)) min = PG_GETARG_FLOAT8(7);
1089 if (!PG_ARGISNULL(8)) max = PG_GETARG_FLOAT8(8);
1094 elog(NOTICE,
"Cannot find band at index %d. Returning NULL", bandindex);
1096 PG_FREE_IF_COPY(pgraster, 0);
1097 MemoryContextSwitchTo(oldcontext);
1098 SRF_RETURN_DONE(funcctx);
1105 PG_FREE_IF_COPY(pgraster, 0);
1106 if (NULL == stats || NULL == stats->
values) {
1107 elog(NOTICE,
"Cannot compute summary statistics for band at index %d", bandindex);
1108 MemoryContextSwitchTo(oldcontext);
1109 SRF_RETURN_DONE(funcctx);
1111 else if (stats->
count < 1) {
1112 elog(NOTICE,
"Cannot compute histogram for band at index %d as the band has no values", bandindex);
1113 MemoryContextSwitchTo(oldcontext);
1114 SRF_RETURN_DONE(funcctx);
1118 hist =
rt_band_get_histogram(stats, bin_count, bin_width, bin_width_count, right, min, max, &count);
1119 if (bin_width_count) pfree(bin_width);
1121 if (NULL == hist || !count) {
1122 elog(NOTICE,
"Cannot compute histogram for band at index %d", bandindex);
1123 MemoryContextSwitchTo(oldcontext);
1124 SRF_RETURN_DONE(funcctx);
1130 funcctx->user_fctx = hist;
1133 funcctx->max_calls =
count;
1136 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
1138 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1140 "function returning record called in context " 1141 "that cannot accept type record" 1146 BlessTupleDesc(tupdesc);
1147 funcctx->tuple_desc = tupdesc;
1149 MemoryContextSwitchTo(oldcontext);
1153 funcctx = SRF_PERCALL_SETUP();
1155 call_cntr = funcctx->call_cntr;
1156 max_calls = funcctx->max_calls;
1157 tupdesc = funcctx->tuple_desc;
1158 hist2 = funcctx->user_fctx;
1161 if (call_cntr < max_calls) {
1171 values[0] = Float8GetDatum(hist2[call_cntr].min);
1172 values[1] = Float8GetDatum(hist2[call_cntr].max);
1173 values[2] = Int64GetDatum(hist2[call_cntr].count);
1174 values[3] = Float8GetDatum(hist2[call_cntr].percent);
1177 tuple = heap_form_tuple(tupdesc, values, nulls);
1180 result = HeapTupleGetDatum(tuple);
1182 SRF_RETURN_NEXT(funcctx, result);
1187 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_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_histogram rt_band_get_histogram(rt_bandstats stats, int bin_count, double *bin_widths, int bin_widths_count, int right, double min, double max, uint32_t *rtn_count)
Count the distribution of data.
#define POSTGIS_RT_DEBUG(level, msg)
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.