921 FuncCallContext *funcctx;
931 if (SRF_IS_FIRSTCALL()) {
932 MemoryContext oldcontext;
937 int32_t bandindex = 1;
939 bool exclude_nodata_value =
TRUE;
941 uint32_t bin_count = 0;
942 double *bin_width = NULL;
943 uint32_t bin_width_count = 0;
965 funcctx = SRF_FIRSTCALL_INIT();
968 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
971 if (PG_ARGISNULL(0)) {
972 MemoryContextSwitchTo(oldcontext);
973 SRF_RETURN_DONE(funcctx);
975 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
979 PG_FREE_IF_COPY(pgraster, 0);
980 MemoryContextSwitchTo(oldcontext);
981 elog(ERROR,
"RASTER_histogram: Cannot deserialize raster");
982 SRF_RETURN_DONE(funcctx);
986 if (!PG_ARGISNULL(1))
987 bandindex = PG_GETARG_INT32(1);
989 if (bandindex < 1 || bandindex > num_bands) {
990 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
992 PG_FREE_IF_COPY(pgraster, 0);
993 MemoryContextSwitchTo(oldcontext);
994 SRF_RETURN_DONE(funcctx);
998 if (!PG_ARGISNULL(2))
999 exclude_nodata_value = PG_GETARG_BOOL(2);
1002 if (!PG_ARGISNULL(3)) {
1003 sample = PG_GETARG_FLOAT8(3);
1004 if (sample < 0 || sample > 1) {
1005 elog(NOTICE,
"Invalid sample percentage (must be between 0 and 1). Returning NULL");
1007 PG_FREE_IF_COPY(pgraster, 0);
1008 MemoryContextSwitchTo(oldcontext);
1009 SRF_RETURN_DONE(funcctx);
1011 else if (
FLT_EQ(sample, 0.0))
1018 if (!PG_ARGISNULL(4)) {
1019 bin_count = PG_GETARG_INT32(4);
1020 if (bin_count < 1) bin_count = 0;
1024 if (!PG_ARGISNULL(5)) {
1025 array = PG_GETARG_ARRAYTYPE_P(5);
1026 etype = ARR_ELEMTYPE(array);
1027 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1035 PG_FREE_IF_COPY(pgraster, 0);
1036 MemoryContextSwitchTo(oldcontext);
1037 elog(ERROR,
"RASTER_histogram: Invalid data type for width");
1038 SRF_RETURN_DONE(funcctx);
1042 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1045 bin_width = palloc(
sizeof(
double) * n);
1046 for (i = 0, j = 0; i < n; i++) {
1047 if (nulls[i])
continue;
1051 width = (double) DatumGetFloat4(e[i]);
1054 width = (double) DatumGetFloat8(e[i]);
1058 if (width < 0 ||
FLT_EQ(width, 0.0)) {
1059 elog(NOTICE,
"Invalid value for width (must be greater than 0). Returning NULL");
1062 PG_FREE_IF_COPY(pgraster, 0);
1063 MemoryContextSwitchTo(oldcontext);
1064 SRF_RETURN_DONE(funcctx);
1067 bin_width[j] = width;
1071 bin_width_count = j;
1080 if (!PG_ARGISNULL(6))
1081 right = PG_GETARG_BOOL(6);
1084 if (!PG_ARGISNULL(7)) min = PG_GETARG_FLOAT8(7);
1087 if (!PG_ARGISNULL(8)) max = PG_GETARG_FLOAT8(8);
1092 elog(NOTICE,
"Cannot find band at index %d. Returning NULL", bandindex);
1094 PG_FREE_IF_COPY(pgraster, 0);
1095 MemoryContextSwitchTo(oldcontext);
1096 SRF_RETURN_DONE(funcctx);
1103 PG_FREE_IF_COPY(pgraster, 0);
1104 if (NULL == stats || NULL == stats->
values) {
1105 elog(NOTICE,
"Cannot compute summary statistics for band at index %d", bandindex);
1106 MemoryContextSwitchTo(oldcontext);
1107 SRF_RETURN_DONE(funcctx);
1109 else if (stats->
count < 1) {
1110 elog(NOTICE,
"Cannot compute histogram for band at index %d as the band has no values", bandindex);
1111 MemoryContextSwitchTo(oldcontext);
1112 SRF_RETURN_DONE(funcctx);
1117 if (bin_width_count) pfree(bin_width);
1119 if (NULL == hist || !
count) {
1120 elog(NOTICE,
"Cannot compute histogram for band at index %d", bandindex);
1121 MemoryContextSwitchTo(oldcontext);
1122 SRF_RETURN_DONE(funcctx);
1128 funcctx->user_fctx = hist;
1131 funcctx->max_calls =
count;
1134 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
1136 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1138 "function returning record called in context "
1139 "that cannot accept type record"
1144 BlessTupleDesc(tupdesc);
1145 funcctx->tuple_desc = tupdesc;
1147 MemoryContextSwitchTo(oldcontext);
1151 funcctx = SRF_PERCALL_SETUP();
1153 call_cntr = funcctx->call_cntr;
1154 max_calls = funcctx->max_calls;
1155 tupdesc = funcctx->tuple_desc;
1156 hist2 = funcctx->user_fctx;
1159 if (call_cntr < max_calls) {
1169 values[0] = Float8GetDatum(hist2[call_cntr].min);
1170 values[1] = Float8GetDatum(hist2[call_cntr].max);
1171 values[2] = Int64GetDatum(hist2[call_cntr].
count);
1172 values[3] = Float8GetDatum(hist2[call_cntr].percent);
1175 tuple = heap_form_tuple(tupdesc, values, nulls);
1178 result = HeapTupleGetDatum(tuple);
1180 SRF_RETURN_NEXT(funcctx,
result);
1185 SRF_RETURN_DONE(funcctx);
char result[OUT_DOUBLE_BUFFER_SIZE]
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,...)