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

◆ RASTER_dwithin()

Datum RASTER_dwithin ( PG_FUNCTION_ARGS  )

Definition at line 911 of file rtpg_spatial_relationship.c.

912{
913 const uint32_t set_count = 2;
914 rt_pgraster *pgrast[2];
915 int pgrastpos[2] = {-1, -1};
916 rt_raster rast[2] = {NULL};
917 uint32_t bandindex[2] = {0};
918 uint32_t hasbandindex[2] = {0};
919 double distance = 0;
920
921 uint32_t i;
922 uint32_t j;
923 uint32_t k;
924 uint32_t numBands;
925 int rtn;
926 int result;
927
928 for (i = 0, j = 0; i < set_count; i++) {
929 /* pgrast is null, return null */
930 if (PG_ARGISNULL(j)) {
931 for (k = 0; k < i; k++) {
932 rt_raster_destroy(rast[k]);
933 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
934 }
935 PG_RETURN_NULL();
936 }
937 pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
938 pgrastpos[i] = j;
939 j++;
940
941 /* raster */
942 rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
943 if (!rast[i]) {
944 for (k = 0; k <= i; k++) {
945 if (k < i)
946 rt_raster_destroy(rast[k]);
947 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
948 }
949 elog(ERROR, "RASTER_dwithin: Could not deserialize the %s raster", i < 1 ? "first" : "second");
950 PG_RETURN_NULL();
951 }
952
953 /* numbands */
954 numBands = rt_raster_get_num_bands(rast[i]);
955 if (numBands < 1) {
956 elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
957 if (i > 0) i++;
958 for (k = 0; k < i; k++) {
959 rt_raster_destroy(rast[k]);
960 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
961 }
962 PG_RETURN_NULL();
963 }
964
965 /* band index */
966 if (!PG_ARGISNULL(j)) {
967 bandindex[i] = PG_GETARG_INT32(j);
968 if (bandindex[i] < 1 || bandindex[i] > numBands) {
969 elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
970 if (i > 0) i++;
971 for (k = 0; k < i; k++) {
972 rt_raster_destroy(rast[k]);
973 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
974 }
975 PG_RETURN_NULL();
976 }
977 hasbandindex[i] = 1;
978 }
979 else
980 hasbandindex[i] = 0;
981 POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
982 POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
983 j++;
984 }
985
986 /* distance */
987 if (PG_ARGISNULL(4)) {
988 elog(NOTICE, "Distance cannot be NULL. Returning NULL");
989 for (k = 0; k < set_count; k++) {
990 rt_raster_destroy(rast[k]);
991 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
992 }
993 PG_RETURN_NULL();
994 }
995
996 distance = PG_GETARG_FLOAT8(4);
997 if (distance < 0) {
998 elog(NOTICE, "Distance cannot be less than zero. Returning NULL");
999 for (k = 0; k < set_count; k++) {
1000 rt_raster_destroy(rast[k]);
1001 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1002 }
1003 PG_RETURN_NULL();
1004 }
1005
1006 /* hasbandindex must be balanced */
1007 if (
1008 (hasbandindex[0] && !hasbandindex[1]) ||
1009 (!hasbandindex[0] && hasbandindex[1])
1010 ) {
1011 elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
1012 for (k = 0; k < set_count; k++) {
1013 rt_raster_destroy(rast[k]);
1014 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1015 }
1016 PG_RETURN_NULL();
1017 }
1018
1019 /* SRID must match */
1020 if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
1021 for (k = 0; k < set_count; k++) {
1022 rt_raster_destroy(rast[k]);
1023 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1024 }
1025 elog(ERROR, "The two rasters provided have different SRIDs");
1026 PG_RETURN_NULL();
1027 }
1028
1030 rast[0], (hasbandindex[0] ? (int)bandindex[0] - 1 : -1),
1031 rast[1], (hasbandindex[1] ? (int)bandindex[1] - 1 : -1),
1032 distance,
1033 &result
1034 );
1035 for (k = 0; k < set_count; k++) {
1036 rt_raster_destroy(rast[k]);
1037 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1038 }
1039
1040 if (rtn != ES_NONE) {
1041 elog(ERROR, "RASTER_dwithin: Could not test that the two rasters are within the specified distance of each other");
1042 PG_RETURN_NULL();
1043 }
1044
1045 PG_RETURN_BOOL(result);
1046}
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition cu_print.c:267
#define FALSE
Definition dbfopen.c:72
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition rt_raster.c:360
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition rt_raster.c:86
@ ES_NONE
Definition librtcore.h:182
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition rt_raster.c:376
rt_errorstate rt_raster_within_distance(rt_raster rast1, int nband1, rt_raster rast2, int nband2, double distance, int *dwithin)
Return ES_ERROR if error occurred in function.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
static double distance(double x1, double y1, double x2, double y2)
Definition lwtree.c:1032
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition rtpostgis.h:69
Struct definitions.
Definition librtcore.h:2452

References distance(), ES_NONE, FALSE, POSTGIS_RT_DEBUGF, result, rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_num_bands(), rt_raster_get_srid(), and rt_raster_within_distance().

Here is the call graph for this function: