PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ RASTER_summaryStats_finalfn()

Datum RASTER_summaryStats_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 830 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().

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