561{
562 MemoryContext aggcontext;
563 MemoryContext oldcontext;
565 bool skiparg =
FALSE;
566
567 int i = 0;
568
572 int num_bands = 0;
574
576
577
578 if (!AggCheckCallContext(fcinfo, &aggcontext)) {
579 elog(
580 ERROR,
581 "RASTER_summaryStats_transfn: Cannot be called in a non-aggregate context"
582 );
583 PG_RETURN_NULL();
584 }
585
586
587 oldcontext = MemoryContextSwitchTo(aggcontext);
588
589 if (PG_ARGISNULL(0)) {
591
593 if (state == NULL) {
594 MemoryContextSwitchTo(oldcontext);
595 elog(
596 ERROR,
597 "RASTER_summaryStats_transfn: Cannot allocate memory for state variable"
598 );
599 PG_RETURN_NULL();
600 }
601
603 }
604 else {
608 }
609
610
611 if (!PG_ARGISNULL(1)) {
612
613 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
614
615
617 if (raster == NULL) {
618
620 PG_FREE_IF_COPY(pgraster, 1);
621
622 MemoryContextSwitchTo(oldcontext);
623 elog(ERROR, "RASTER_summaryStats_transfn: Cannot deserialize raster");
624 PG_RETURN_NULL();
625 }
626 }
627
628 do {
629 Oid calltype;
630 int nargs = 0;
631
632 if (skiparg)
633 break;
634
635
636 nargs = PG_NARGS();
638
639 for (i = 2; i < nargs; i++) {
640 if (PG_ARGISNULL(i))
641 continue;
642
643 calltype = get_fn_expr_argtype(fcinfo->flinfo, i);
644
645
646 if (
647 (calltype == INT2OID || calltype == INT4OID) &&
648 i == 2
649 ) {
650 if (calltype == INT2OID)
652 else
654
655
657
659 if (raster != NULL) {
661 PG_FREE_IF_COPY(pgraster, 1);
662 }
663
664 MemoryContextSwitchTo(oldcontext);
665 elog(
666 ERROR,
667 "RASTER_summaryStats_transfn: Invalid band index (must use 1-based). Returning NULL"
668 );
669 PG_RETURN_NULL();
670 }
671 }
672
673 else if (
674 calltype == BOOLOID && (
675 i == 2 || i == 3
676 )
677 ) {
679 }
680
681 else if (
682 (calltype == FLOAT4OID || calltype == FLOAT8OID) &&
683 (i == 3 || i == 4)
684 ) {
685 if (calltype == FLOAT4OID)
686 state->
sample = PG_GETARG_FLOAT4(i);
687 else
688 state->
sample = PG_GETARG_FLOAT8(i);
689
690
692
694 if (raster != NULL) {
696 PG_FREE_IF_COPY(pgraster, 1);
697 }
698
699 MemoryContextSwitchTo(oldcontext);
700 elog(
701 ERROR,
702 "Invalid sample percentage (must be between 0 and 1). Returning NULL"
703 );
704
705 PG_RETURN_NULL();
706 }
709 }
710
711 else {
713 if (raster != NULL) {
715 PG_FREE_IF_COPY(pgraster, 1);
716 }
717
718 MemoryContextSwitchTo(oldcontext);
719 elog(
720 ERROR,
721 "RASTER_summaryStats_transfn: Unknown function parameter at index %d",
722 i
723 );
724 PG_RETURN_NULL();
725 }
726 }
727 }
728 while (0);
729
730
731 if (PG_ARGISNULL(1)) {
733 MemoryContextSwitchTo(oldcontext);
734 PG_RETURN_POINTER(state);
735 }
736
737
740 elog(
741 NOTICE,
742 "Raster does not have band at index %d. Skipping raster",
744 );
745
747 PG_FREE_IF_COPY(pgraster, 1);
748
749 MemoryContextSwitchTo(oldcontext);
750 PG_RETURN_POINTER(state);
751 }
752
753
755 if (!band) {
756 elog(
757 NOTICE, "Cannot find band at index %d. Skipping raster",
759 );
760
762 PG_FREE_IF_COPY(pgraster, 1);
763
764 MemoryContextSwitchTo(oldcontext);
765 PG_RETURN_POINTER(state);
766 }
767
768
772 &(state->
cK), &(state->
cM), &(state->
cQ)
773 );
774
777 PG_FREE_IF_COPY(pgraster, 1);
778
779 if (NULL == stats) {
780 elog(
781 NOTICE,
782 "Cannot compute summary statistics for band at index %d. Returning NULL",
784 );
785
787
788 MemoryContextSwitchTo(oldcontext);
789 PG_RETURN_NULL();
790 }
791
792 if (stats->
count > 0) {
801 }
802 else {
805
810 }
811 }
812
813 pfree(stats);
814
815
816 MemoryContextSwitchTo(oldcontext);
817
819
820 PG_RETURN_POINTER(state);
821}
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