563{
564 MemoryContext aggcontext;
565 MemoryContext oldcontext;
567 bool skiparg =
FALSE;
568
569 int i = 0;
570
574 int num_bands = 0;
576
578
579
580 if (!AggCheckCallContext(fcinfo, &aggcontext)) {
581 elog(
582 ERROR,
583 "RASTER_summaryStats_transfn: Cannot be called in a non-aggregate context"
584 );
585 PG_RETURN_NULL();
586 }
587
588
589 oldcontext = MemoryContextSwitchTo(aggcontext);
590
591 if (PG_ARGISNULL(0)) {
593
595 if (state == NULL) {
596 MemoryContextSwitchTo(oldcontext);
597 elog(
598 ERROR,
599 "RASTER_summaryStats_transfn: Cannot allocate memory for state variable"
600 );
601 PG_RETURN_NULL();
602 }
603
605 }
606 else {
610 }
611
612
613 if (!PG_ARGISNULL(1)) {
614
615 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
616
617
619 if (raster == NULL) {
620
622 PG_FREE_IF_COPY(pgraster, 1);
623
624 MemoryContextSwitchTo(oldcontext);
625 elog(ERROR, "RASTER_summaryStats_transfn: Cannot deserialize raster");
626 PG_RETURN_NULL();
627 }
628 }
629
630 do {
631 Oid calltype;
632 int nargs = 0;
633
634 if (skiparg)
635 break;
636
637
638 nargs = PG_NARGS();
640
641 for (i = 2; i < nargs; i++) {
642 if (PG_ARGISNULL(i))
643 continue;
644
645 calltype = get_fn_expr_argtype(fcinfo->flinfo, i);
646
647
648 if (
649 (calltype == INT2OID || calltype == INT4OID) &&
650 i == 2
651 ) {
652 if (calltype == INT2OID)
654 else
656
657
659
661 if (raster != NULL) {
663 PG_FREE_IF_COPY(pgraster, 1);
664 }
665
666 MemoryContextSwitchTo(oldcontext);
667 elog(
668 ERROR,
669 "RASTER_summaryStats_transfn: Invalid band index (must use 1-based). Returning NULL"
670 );
671 PG_RETURN_NULL();
672 }
673 }
674
675 else if (
676 calltype == BOOLOID && (
677 i == 2 || i == 3
678 )
679 ) {
681 }
682
683 else if (
684 (calltype == FLOAT4OID || calltype == FLOAT8OID) &&
685 (i == 3 || i == 4)
686 ) {
687 if (calltype == FLOAT4OID)
688 state->
sample = PG_GETARG_FLOAT4(i);
689 else
690 state->
sample = PG_GETARG_FLOAT8(i);
691
692
694
696 if (raster != NULL) {
698 PG_FREE_IF_COPY(pgraster, 1);
699 }
700
701 MemoryContextSwitchTo(oldcontext);
702 elog(
703 ERROR,
704 "Invalid sample percentage (must be between 0 and 1). Returning NULL"
705 );
706
707 PG_RETURN_NULL();
708 }
711 }
712
713 else {
715 if (raster != NULL) {
717 PG_FREE_IF_COPY(pgraster, 1);
718 }
719
720 MemoryContextSwitchTo(oldcontext);
721 elog(
722 ERROR,
723 "RASTER_summaryStats_transfn: Unknown function parameter at index %d",
724 i
725 );
726 PG_RETURN_NULL();
727 }
728 }
729 }
730 while (0);
731
732
733 if (PG_ARGISNULL(1)) {
735 MemoryContextSwitchTo(oldcontext);
736 PG_RETURN_POINTER(state);
737 }
738
739
742 elog(
743 NOTICE,
744 "Raster does not have band at index %d. Skipping raster",
746 );
747
749 PG_FREE_IF_COPY(pgraster, 1);
750
751 MemoryContextSwitchTo(oldcontext);
752 PG_RETURN_POINTER(state);
753 }
754
755
757 if (!band) {
758 elog(
759 NOTICE, "Cannot find band at index %d. Skipping raster",
761 );
762
764 PG_FREE_IF_COPY(pgraster, 1);
765
766 MemoryContextSwitchTo(oldcontext);
767 PG_RETURN_POINTER(state);
768 }
769
770
774 &(state->
cK), &(state->
cM), &(state->
cQ)
775 );
776
779 PG_FREE_IF_COPY(pgraster, 1);
780
781 if (NULL == stats) {
782 elog(
783 NOTICE,
784 "Cannot compute summary statistics for band at index %d. Returning NULL",
786 );
787
789
790 MemoryContextSwitchTo(oldcontext);
791 PG_RETURN_NULL();
792 }
793
794 if (stats->
count > 0) {
803 }
804 else {
807
812 }
813 }
814
815 pfree(stats);
816
817
818 MemoryContextSwitchTo(oldcontext);
819
821
822 PG_RETURN_POINTER(state);
823}
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
void rt_band_destroy(rt_band band)
Destroy a raster band.
uint16_t rt_raster_get_num_bands(rt_raster raster)
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.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
struct rtpg_summarystats_arg_t * rtpg_summarystats_arg
static void rtpg_summarystats_arg_destroy(rtpg_summarystats_arg arg)
static rtpg_summarystats_arg rtpg_summarystats_arg_init()
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)
bool exclude_nodata_value