PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ RASTER_summaryStats_finalfn()

Datum RASTER_summaryStats_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 824 of file rtpg_statistics.c.

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

References rt_bandstats_t::count, rtpg_summarystats_arg_t::cQ, FALSE, rt_bandstats_t::max, rt_bandstats_t::mean, rt_bandstats_t::min, POSTGIS_RT_DEBUG, result, 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.

Here is the call graph for this function: