665{
666 FuncCallContext *funcctx;
667 TupleDesc tupdesc;
670 int call_cntr;
671 int max_calls;
672 int i = 0;
673
674 if (SRF_IS_FIRSTCALL()) {
675 MemoryContext oldcontext;
680 int numbands;
681 bool hasband =
FALSE;
682 bool exclude_nodata_value =
TRUE;
683 bool nocolumnx =
TRUE;
687 int bounds[4] = {0};
688 int pixcount = 0;
690 int isnodata = 0;
691
693
695
696
697 funcctx = SRF_FIRSTCALL_INIT();
698
699
700 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
701
702
703 if (PG_ARGISNULL(0)) {
704 MemoryContextSwitchTo(oldcontext);
705 SRF_RETURN_DONE(funcctx);
706 }
707 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
708
709
710 if (!PG_ARGISNULL(1)) {
711 nband = PG_GETARG_INT32(1);
713 }
714
715
716 if (!PG_ARGISNULL(2)) {
717 bounds[0] = PG_GETARG_INT32(2);
718 bounds[1] = bounds[0];
720 }
721
722
723 if (!PG_ARGISNULL(3)) {
724 bounds[2] = PG_GETARG_INT32(3);
725 bounds[3] = bounds[2];
727 }
728
729
730 if (!PG_ARGISNULL(4))
731 exclude_nodata_value = PG_GETARG_BOOL(4);
732
733
735 if (!raster) {
736 PG_FREE_IF_COPY(pgraster, 0);
737 ereport(ERROR, (
738 errcode(ERRCODE_OUT_OF_MEMORY),
739 errmsg("Could not deserialize raster")
740 ));
741 MemoryContextSwitchTo(oldcontext);
742 SRF_RETURN_DONE(funcctx);
743 }
744
745
747 elog(NOTICE, "Raster is empty. Returning empty set");
749 PG_FREE_IF_COPY(pgraster, 0);
750 MemoryContextSwitchTo(oldcontext);
751 SRF_RETURN_DONE(funcctx);
752 }
753
754
755 if (hasband) {
759
760
761 if (nband < 1 || nband > numbands) {
762 elog(NOTICE, "Invalid band index (must use 1-based). Returning empty set");
764 PG_FREE_IF_COPY(pgraster, 0);
765 MemoryContextSwitchTo(oldcontext);
766 SRF_RETURN_DONE(funcctx);
767 }
768
769
771 if (!band) {
772 elog(NOTICE, "Could not find band at index %d. Returning empty set", nband);
773
775 PG_FREE_IF_COPY(pgraster, 0);
776
777 MemoryContextSwitchTo(oldcontext);
778 SRF_RETURN_DONE(funcctx);
779 }
780
782 exclude_nodata_value =
FALSE;
783 }
784
785
786 if (nocolumnx) {
787 bounds[0] = 1;
789 }
790 if (norowy) {
791 bounds[2] = 1;
793 }
795 bounds[0], bounds[1], bounds[2], bounds[3]);
796
797
798 pixcount = 0;
799 for (y = bounds[2];
y <= bounds[3];
y++) {
800
801 for (x = bounds[0];
x <= bounds[1];
x++) {
802
805
806 if (hasband) {
808
809 for (i = 0; i < pixcount; i++)
811 if (pixcount) pfree(pix);
812
815 PG_FREE_IF_COPY(pgraster, 0);
816
817 MemoryContextSwitchTo(oldcontext);
818 elog(ERROR, "RASTER_getPixelCentroids: Could not get pixel value");
819 SRF_RETURN_DONE(funcctx);
820 }
821
822
823 if (isnodata && exclude_nodata_value) {
824 POSTGIS_RT_DEBUG(5,
"pixel value is NODATA and exclude_nodata_value = TRUE");
825 continue;
826 }
827 }
828
829
831 if (!point) {
832
833 for (i = 0; i < pixcount; i++)
835 if (pixcount) pfree(pix);
836
839 PG_FREE_IF_COPY(pgraster, 0);
840
841 MemoryContextSwitchTo(oldcontext);
842 elog(ERROR, "RASTER_getPixelCentroids: Could not get pixel centroid");
843 SRF_RETURN_DONE(funcctx);
844 }
845
846
847 if (!pixcount) {
848 pix = palloc(
sizeof(
struct rt_pixel_t) * (pixcount + 1));
849 }
850 else {
851 pix = repalloc(pix,
sizeof(
struct rt_pixel_t) * (pixcount + 1));
852 }
853 if (pix == NULL) {
855
858 PG_FREE_IF_COPY(pgraster, 0);
859
860 MemoryContextSwitchTo(oldcontext);
861 elog(ERROR, "RASTER_getPixelCentroids: Could not allocate memory for storing pixel centroids");
862 SRF_RETURN_DONE(funcctx);
863 }
864
865
869
870
873
874
876
877
878 if (hasband) {
879 if (exclude_nodata_value)
880 pix[pixcount].
nodata = isnodata;
881 else
883 }
884 else {
885 pix[pixcount].
nodata = isnodata;
886 }
887
888
889 pixcount++;
890 }
891 }
892
893
896 PG_FREE_IF_COPY(pgraster, 0);
897
898
899 if (pixcount < 1) {
900 elog(NOTICE, "No pixels found for band %d", nband);
901 MemoryContextSwitchTo(oldcontext);
902 SRF_RETURN_DONE(funcctx);
903 }
904
905
906 funcctx->user_fctx = pix;
907
908
909 funcctx->max_calls = pixcount;
911
912
913 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
914 ereport(ERROR, (
915 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
916 errmsg("function returning record called in context that cannot accept type record")
917 ));
918 }
919
920 BlessTupleDesc(tupdesc);
921 funcctx->tuple_desc = tupdesc;
922
923 MemoryContextSwitchTo(oldcontext);
924 }
925
926
927 funcctx = SRF_PERCALL_SETUP();
928
929 call_cntr = funcctx->call_cntr;
930 max_calls = funcctx->max_calls;
931 tupdesc = funcctx->tuple_desc;
932 pix2 = funcctx->user_fctx;
933
934
935 if (call_cntr < max_calls) {
938 HeapTuple tuple;
940
942 size_t gser_size = 0;
943
945
947
948
951
952
953 values[0] = PointerGetDatum(gser);
954 if (pix2[call_cntr].nodata)
956 else
957 values[1] = Float8GetDatum(pix2[call_cntr].value);
958 values[2] = Int32GetDatum(pix2[call_cntr].x);
959 values[3] = Int32GetDatum(pix2[call_cntr].y);
960
961
962 tuple = heap_form_tuple(tupdesc, values, nulls);
963
964
965 result = HeapTupleGetDatum(tuple);
966
967 SRF_RETURN_NEXT(funcctx,
result);
968 }
969
970 else {
971 pfree(pix2);
972 SRF_RETURN_DONE(funcctx);
973 }
974}
char result[OUT_DOUBLE_BUFFER_SIZE]
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
void lwpoint_free(LWPOINT *pt)
void lwgeom_free(LWGEOM *geom)
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
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)
uint16_t rt_raster_get_height(rt_raster raster)
LWPOINT * rt_raster_pixel_as_centroid_point(rt_raster rast, int x, int y)
Get a raster pixel centroid point.
uint16_t rt_raster_get_width(rt_raster raster)
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
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): ...
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)