PostGIS  3.0.0dev-r@@SVN_REVISION@@

◆ RASTER_summaryStats_finalfn()

Datum RASTER_summaryStats_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 826 of file rtpg_statistics.c.

References rt_bandstats_t::count, rtpg_summarystats_arg_t::cQ, FALSE, rt_bandstats_t::max, rt_bandstats_t::mean, rt_bandstats_t::min, PG_FUNCTION_INFO_V1(), POSTGIS_RT_DEBUG, RASTER_histogram(), rtpg_summarystats_arg_destroy(), rt_bandstats_t::sample, rtpg_summarystats_arg_t::stats, rt_bandstats_t::stddev, rt_bandstats_t::sum, and TRUE.

Referenced by RASTER_summaryStats_transfn().

827 {
828  rtpg_summarystats_arg state = NULL;
829 
830  TupleDesc tupdesc;
831  HeapTuple tuple;
832  int values_length = 6;
833  Datum values[values_length];
834  bool nulls[values_length];
835  Datum result;
836 
837  POSTGIS_RT_DEBUG(3, "Starting...");
838 
839  /* cannot be called directly as this is exclusive aggregate function */
840  if (!AggCheckCallContext(fcinfo, NULL)) {
841  elog(ERROR, "RASTER_summaryStats_finalfn: Cannot be called in a non-aggregate context");
842  PG_RETURN_NULL();
843  }
844 
845  /* NULL, return null */
846  if (PG_ARGISNULL(0))
847  PG_RETURN_NULL();
848 
849  state = (rtpg_summarystats_arg) PG_GETARG_POINTER(0);
850 
851  if (NULL == state) {
852  elog(ERROR, "RASTER_summaryStats_finalfn: Cannot compute coverage summary stats");
853  PG_RETURN_NULL();
854  }
855 
856  /* coverage mean and deviation */
857  if (state->stats->count > 0) {
858  state->stats->mean = state->stats->sum / state->stats->count;
859  /* sample deviation */
860  if (state->stats->sample > 0 && state->stats->sample < 1)
861  state->stats->stddev = sqrt(state->cQ / (state->stats->count - 1));
862  /* standard deviation */
863  else
864  state->stats->stddev = sqrt(state->cQ / state->stats->count);
865  }
866 
867  /* Build a tuple descriptor for our result type */
868  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
870  ereport(ERROR, (
871  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
872  errmsg(
873  "function returning record called in context "
874  "that cannot accept type record"
875  )
876  ));
877  }
878 
879  BlessTupleDesc(tupdesc);
880 
881  memset(nulls, FALSE, sizeof(bool) * values_length);
882 
883  values[0] = Int64GetDatum(state->stats->count);
884  if (state->stats->count > 0) {
885  values[1] = Float8GetDatum(state->stats->sum);
886  values[2] = Float8GetDatum(state->stats->mean);
887  values[3] = Float8GetDatum(state->stats->stddev);
888  values[4] = Float8GetDatum(state->stats->min);
889  values[5] = Float8GetDatum(state->stats->max);
890  }
891  else {
892  nulls[1] = TRUE;
893  nulls[2] = TRUE;
894  nulls[3] = TRUE;
895  nulls[4] = TRUE;
896  nulls[5] = TRUE;
897  }
898 
899  /* build a tuple */
900  tuple = heap_form_tuple(tupdesc, values, nulls);
901 
902  /* make the tuple into a datum */
903  result = HeapTupleGetDatum(tuple);
904 
905  /* clean up */
907 
908  PG_RETURN_DATUM(result);
909 }
uint32_t count
Definition: librtcore.h:2360
struct rtpg_summarystats_arg_t * rtpg_summarystats_arg
#define FALSE
Definition: dbfopen.c:168
static void rtpg_summarystats_arg_destroy(rtpg_summarystats_arg arg)
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:61
#define TRUE
Definition: dbfopen.c:169
Here is the call graph for this function:
Here is the caller graph for this function: