PostGIS  2.4.9dev-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, TRUE, and VALUES_LENGTH.

Referenced by RASTER_summaryStats_transfn().

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