564 MemoryContext aggcontext;
565 MemoryContext oldcontext;
567 bool skiparg =
FALSE;
580 if (!AggCheckCallContext(fcinfo, &aggcontext)) {
583 "RASTER_summaryStats_transfn: Cannot be called in a non-aggregate context"
589 oldcontext = MemoryContextSwitchTo(aggcontext);
591 if (PG_ARGISNULL(0)) {
596 MemoryContextSwitchTo(oldcontext);
599 "RASTER_summaryStats_transfn: Cannot allocate memory for state variable"
613 if (!PG_ARGISNULL(1)) {
615 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
622 PG_FREE_IF_COPY(pgraster, 1);
624 MemoryContextSwitchTo(oldcontext);
625 elog(ERROR,
"RASTER_summaryStats_transfn: Cannot deserialize raster");
641 for (i = 2; i < nargs; i++) {
645 calltype = get_fn_expr_argtype(fcinfo->flinfo, i);
649 (calltype == INT2OID || calltype == INT4OID) &&
652 if (calltype == INT2OID)
663 PG_FREE_IF_COPY(pgraster, 1);
666 MemoryContextSwitchTo(oldcontext);
669 "RASTER_summaryStats_transfn: Invalid band index (must use 1-based). Returning NULL"
676 calltype == BOOLOID && (
684 (calltype == FLOAT4OID || calltype == FLOAT8OID) &&
687 if (calltype == FLOAT4OID)
688 state->
sample = PG_GETARG_FLOAT4(i);
690 state->
sample = PG_GETARG_FLOAT8(i);
698 PG_FREE_IF_COPY(pgraster, 1);
701 MemoryContextSwitchTo(oldcontext);
704 "Invalid sample percentage (must be between 0 and 1). Returning NULL"
717 PG_FREE_IF_COPY(pgraster, 1);
720 MemoryContextSwitchTo(oldcontext);
723 "RASTER_summaryStats_transfn: Unknown function parameter at index %d",
733 if (PG_ARGISNULL(1)) {
735 MemoryContextSwitchTo(oldcontext);
736 PG_RETURN_POINTER(state);
744 "Raster does not have band at index %d. Skipping raster",
749 PG_FREE_IF_COPY(pgraster, 1);
751 MemoryContextSwitchTo(oldcontext);
752 PG_RETURN_POINTER(state);
759 NOTICE,
"Cannot find band at index %d. Skipping raster",
764 PG_FREE_IF_COPY(pgraster, 1);
766 MemoryContextSwitchTo(oldcontext);
767 PG_RETURN_POINTER(state);
774 &(state->
cK), &(state->
cM), &(state->
cQ)
779 PG_FREE_IF_COPY(pgraster, 1);
784 "Cannot compute summary statistics for band at index %d. Returning NULL",
790 MemoryContextSwitchTo(oldcontext);
794 if (stats->
count > 0) {
818 MemoryContextSwitchTo(oldcontext);
822 PG_RETURN_POINTER(state);
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_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): ...
struct rtpg_summarystats_arg_t * rtpg_summarystats_arg
static void rtpg_summarystats_arg_destroy(rtpg_summarystats_arg arg)
static rtpg_summarystats_arg rtpg_summarystats_arg_init()
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)
bool exclude_nodata_value