PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_summaryStats_transfn()

Datum RASTER_summaryStats_transfn ( PG_FUNCTION_ARGS  )

Definition at line 566 of file rtpg_statistics.c.

References ovdump::band, rtpg_summarystats_arg_t::band_index, rtpg_summarystats_arg_t::cK, rtpg_summarystats_arg_t::cM, rt_bandstats_t::count, rtpg_summarystats_arg_t::cQ, rtpg_summarystats_arg_t::exclude_nodata_value, FALSE, FLT_EQ, rt_bandstats_t::max, rt_bandstats_t::mean, rt_bandstats_t::min, PG_FUNCTION_INFO_V1(), POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, rtrowdump::raster, RASTER_summaryStats_finalfn(), rt_band_destroy(), rt_band_get_summary_stats(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_band(), rt_raster_get_num_bands(), rtpg_summarystats_arg_destroy(), rtpg_summarystats_arg_init(), rtpg_summarystats_arg_t::sample, rt_bandstats_t::sample, rtpg_summarystats_arg_t::stats, rt_bandstats_t::stddev, rt_bandstats_t::sum, and TRUE.

Referenced by rtpg_summarystats_arg_init().

567 {
568  MemoryContext aggcontext;
569  MemoryContext oldcontext;
570  rtpg_summarystats_arg state = NULL;
571  bool skiparg = FALSE;
572 
573  int i = 0;
574 
575  rt_pgraster *pgraster = NULL;
576  rt_raster raster = NULL;
577  rt_band band = NULL;
578  int num_bands = 0;
579  rt_bandstats stats = NULL;
580 
581  POSTGIS_RT_DEBUG(3, "Starting...");
582 
583  /* cannot be called directly as this is exclusive aggregate function */
584  if (!AggCheckCallContext(fcinfo, &aggcontext)) {
585  elog(
586  ERROR,
587  "RASTER_summaryStats_transfn: Cannot be called in a non-aggregate context"
588  );
589  PG_RETURN_NULL();
590  }
591 
592  /* switch to aggcontext */
593  oldcontext = MemoryContextSwitchTo(aggcontext);
594 
595  if (PG_ARGISNULL(0)) {
596  POSTGIS_RT_DEBUG(3, "Creating state variable");
597 
598  state = rtpg_summarystats_arg_init();
599  if (state == NULL) {
600  MemoryContextSwitchTo(oldcontext);
601  elog(
602  ERROR,
603  "RASTER_summaryStats_transfn: Cannot allocate memory for state variable"
604  );
605  PG_RETURN_NULL();
606  }
607 
608  skiparg = FALSE;
609  }
610  else {
611  POSTGIS_RT_DEBUG(3, "State variable already exists");
612  state = (rtpg_summarystats_arg) PG_GETARG_POINTER(0);
613  skiparg = TRUE;
614  }
615 
616  /* raster arg is NOT NULL */
617  if (!PG_ARGISNULL(1)) {
618  /* deserialize raster */
619  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
620 
621  /* Get raster object */
622  raster = rt_raster_deserialize(pgraster, FALSE);
623  if (raster == NULL) {
624 
626  PG_FREE_IF_COPY(pgraster, 1);
627 
628  MemoryContextSwitchTo(oldcontext);
629  elog(ERROR, "RASTER_summaryStats_transfn: Cannot deserialize raster");
630  PG_RETURN_NULL();
631  }
632  }
633 
634  do {
635  Oid calltype;
636  int nargs = 0;
637 
638  if (skiparg)
639  break;
640 
641  /* 4 or 5 total possible args */
642  nargs = PG_NARGS();
643  POSTGIS_RT_DEBUGF(4, "nargs = %d", nargs);
644 
645  for (i = 2; i < nargs; i++) {
646  if (PG_ARGISNULL(i))
647  continue;
648 
649  calltype = get_fn_expr_argtype(fcinfo->flinfo, i);
650 
651  /* band index */
652  if (
653  (calltype == INT2OID || calltype == INT4OID) &&
654  i == 2
655  ) {
656  if (calltype == INT2OID)
657  state->band_index = PG_GETARG_INT16(i);
658  else
659  state->band_index = PG_GETARG_INT32(i);
660 
661  /* basic check, > 0 */
662  if (state->band_index < 1) {
663 
665  if (raster != NULL) {
666  rt_raster_destroy(raster);
667  PG_FREE_IF_COPY(pgraster, 1);
668  }
669 
670  MemoryContextSwitchTo(oldcontext);
671  elog(
672  ERROR,
673  "RASTER_summaryStats_transfn: Invalid band index (must use 1-based). Returning NULL"
674  );
675  PG_RETURN_NULL();
676  }
677  }
678  /* exclude_nodata_value */
679  else if (
680  calltype == BOOLOID && (
681  i == 2 || i == 3
682  )
683  ) {
684  state->exclude_nodata_value = PG_GETARG_BOOL(i);
685  }
686  /* sample rate */
687  else if (
688  (calltype == FLOAT4OID || calltype == FLOAT8OID) &&
689  (i == 3 || i == 4)
690  ) {
691  if (calltype == FLOAT4OID)
692  state->sample = PG_GETARG_FLOAT4(i);
693  else
694  state->sample = PG_GETARG_FLOAT8(i);
695 
696  /* basic check, 0 <= sample <= 1 */
697  if (state->sample < 0. || state->sample > 1.) {
698 
700  if (raster != NULL) {
701  rt_raster_destroy(raster);
702  PG_FREE_IF_COPY(pgraster, 1);
703  }
704 
705  MemoryContextSwitchTo(oldcontext);
706  elog(
707  ERROR,
708  "Invalid sample percentage (must be between 0 and 1). Returning NULL"
709  );
710 
711  PG_RETURN_NULL();
712  }
713  else if (FLT_EQ(state->sample, 0.0))
714  state->sample = 1;
715  }
716  /* unknown arg */
717  else {
719  if (raster != NULL) {
720  rt_raster_destroy(raster);
721  PG_FREE_IF_COPY(pgraster, 1);
722  }
723 
724  MemoryContextSwitchTo(oldcontext);
725  elog(
726  ERROR,
727  "RASTER_summaryStats_transfn: Unknown function parameter at index %d",
728  i
729  );
730  PG_RETURN_NULL();
731  }
732  }
733  }
734  while (0);
735 
736  /* null raster, return */
737  if (PG_ARGISNULL(1)) {
738  POSTGIS_RT_DEBUG(4, "NULL raster so processing required");
739  MemoryContextSwitchTo(oldcontext);
740  PG_RETURN_POINTER(state);
741  }
742 
743  /* inspect number of bands */
744  num_bands = rt_raster_get_num_bands(raster);
745  if (state->band_index > num_bands) {
746  elog(
747  NOTICE,
748  "Raster does not have band at index %d. Skipping raster",
749  state->band_index
750  );
751 
752  rt_raster_destroy(raster);
753  PG_FREE_IF_COPY(pgraster, 1);
754 
755  MemoryContextSwitchTo(oldcontext);
756  PG_RETURN_POINTER(state);
757  }
758 
759  /* get band */
760  band = rt_raster_get_band(raster, state->band_index - 1);
761  if (!band) {
762  elog(
763  NOTICE, "Cannot find band at index %d. Skipping raster",
764  state->band_index
765  );
766 
767  rt_raster_destroy(raster);
768  PG_FREE_IF_COPY(pgraster, 1);
769 
770  MemoryContextSwitchTo(oldcontext);
771  PG_RETURN_POINTER(state);
772  }
773 
774  /* we don't need the raw values, hence the zero parameter */
776  band, (int) state->exclude_nodata_value,
777  state->sample, 0,
778  &(state->cK), &(state->cM), &(state->cQ)
779  );
780 
781  rt_band_destroy(band);
782  rt_raster_destroy(raster);
783  PG_FREE_IF_COPY(pgraster, 1);
784 
785  if (NULL == stats) {
786  elog(
787  NOTICE,
788  "Cannot compute summary statistics for band at index %d. Returning NULL",
789  state->band_index
790  );
791 
793 
794  MemoryContextSwitchTo(oldcontext);
795  PG_RETURN_NULL();
796  }
797 
798  if (stats->count > 0) {
799  if (state->stats->count < 1) {
800  state->stats->sample = stats->sample;
801  state->stats->count = stats->count;
802  state->stats->min = stats->min;
803  state->stats->max = stats->max;
804  state->stats->sum = stats->sum;
805  state->stats->mean = stats->mean;
806  state->stats->stddev = -1;
807  }
808  else {
809  state->stats->count += stats->count;
810  state->stats->sum += stats->sum;
811 
812  if (stats->min < state->stats->min)
813  state->stats->min = stats->min;
814  if (stats->max > state->stats->max)
815  state->stats->max = stats->max;
816  }
817  }
818 
819  pfree(stats);
820 
821  /* switch back to local context */
822  MemoryContextSwitchTo(oldcontext);
823 
824  POSTGIS_RT_DEBUG(3, "Finished");
825 
826  PG_RETURN_POINTER(state);
827 }
static rtpg_summarystats_arg rtpg_summarystats_arg_init()
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
uint32_t count
Definition: librtcore.h:2311
raster
Be careful!! Zeros function&#39;s input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
band
Definition: ovdump.py:57
#define FLT_EQ(x, y)
Definition: librtcore.h:2185
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:242
struct rtpg_summarystats_arg_t * rtpg_summarystats_arg
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
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.
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2201
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
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:717
Here is the call graph for this function:
Here is the caller graph for this function: