PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ RASTER_dfullywithin()

Datum RASTER_dfullywithin ( PG_FUNCTION_ARGS  )

Definition at line 1052 of file rtpg_spatial_relationship.c.

1053 {
1054  const uint32_t set_count = 2;
1055  rt_pgraster *pgrast[2];
1056  int pgrastpos[2] = {-1, -1};
1057  rt_raster rast[2] = {NULL};
1058  uint32_t bandindex[2] = {0};
1059  uint32_t hasbandindex[2] = {0};
1060  double distance = 0;
1061 
1062  uint32_t i;
1063  uint32_t j;
1064  uint32_t k;
1065  uint32_t numBands;
1066  int rtn;
1067  int result;
1068 
1069  for (i = 0, j = 0; i < set_count; i++) {
1070  /* pgrast is null, return null */
1071  if (PG_ARGISNULL(j)) {
1072  for (k = 0; k < i; k++) {
1073  rt_raster_destroy(rast[k]);
1074  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1075  }
1076  PG_RETURN_NULL();
1077  }
1078  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
1079  pgrastpos[i] = j;
1080  j++;
1081 
1082  /* raster */
1083  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
1084  if (!rast[i]) {
1085  for (k = 0; k <= i; k++) {
1086  if (k < i)
1087  rt_raster_destroy(rast[k]);
1088  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1089  }
1090  elog(ERROR, "RASTER_dfullywithin: Could not deserialize the %s raster", i < 1 ? "first" : "second");
1091  PG_RETURN_NULL();
1092  }
1093 
1094  /* numbands */
1095  numBands = rt_raster_get_num_bands(rast[i]);
1096  if (numBands < 1) {
1097  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
1098  if (i > 0) i++;
1099  for (k = 0; k < i; k++) {
1100  rt_raster_destroy(rast[k]);
1101  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1102  }
1103  PG_RETURN_NULL();
1104  }
1105 
1106  /* band index */
1107  if (!PG_ARGISNULL(j)) {
1108  bandindex[i] = PG_GETARG_INT32(j);
1109  if (bandindex[i] < 1 || bandindex[i] > numBands) {
1110  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
1111  if (i > 0) i++;
1112  for (k = 0; k < i; k++) {
1113  rt_raster_destroy(rast[k]);
1114  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1115  }
1116  PG_RETURN_NULL();
1117  }
1118  hasbandindex[i] = 1;
1119  }
1120  else
1121  hasbandindex[i] = 0;
1122  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
1123  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
1124  j++;
1125  }
1126 
1127  /* distance */
1128  if (PG_ARGISNULL(4)) {
1129  elog(NOTICE, "Distance cannot be NULL. Returning NULL");
1130  for (k = 0; k < set_count; k++) {
1131  rt_raster_destroy(rast[k]);
1132  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1133  }
1134  PG_RETURN_NULL();
1135  }
1136 
1137  distance = PG_GETARG_FLOAT8(4);
1138  if (distance < 0) {
1139  elog(NOTICE, "Distance cannot be less than zero. Returning NULL");
1140  for (k = 0; k < set_count; k++) {
1141  rt_raster_destroy(rast[k]);
1142  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1143  }
1144  PG_RETURN_NULL();
1145  }
1146 
1147  /* hasbandindex must be balanced */
1148  if (
1149  (hasbandindex[0] && !hasbandindex[1]) ||
1150  (!hasbandindex[0] && hasbandindex[1])
1151  ) {
1152  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
1153  for (k = 0; k < set_count; k++) {
1154  rt_raster_destroy(rast[k]);
1155  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1156  }
1157  PG_RETURN_NULL();
1158  }
1159 
1160  /* SRID must match */
1162  for (k = 0; k < set_count; k++) {
1163  rt_raster_destroy(rast[k]);
1164  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1165  }
1166  elog(ERROR, "The two rasters provided have different SRIDs");
1167  PG_RETURN_NULL();
1168  }
1169 
1171  rast[0], (hasbandindex[0] ? (int)bandindex[0] - 1 : -1),
1172  rast[1], (hasbandindex[1] ? (int)bandindex[1] - 1 : -1),
1173  distance,
1174  &result
1175  );
1176  for (k = 0; k < set_count; k++) {
1177  rt_raster_destroy(rast[k]);
1178  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1179  }
1180 
1181  if (rtn != ES_NONE) {
1182  elog(ERROR, "RASTER_dfullywithin: Could not test that the two rasters are fully within the specified distance of each other");
1183  PG_RETURN_NULL();
1184  }
1185 
1186  PG_RETURN_BOOL(result);
1187 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
#define FALSE
Definition: dbfopen.c:72
rt_errorstate rt_raster_fully_within_distance(rt_raster rast1, int nband1, rt_raster rast2, int nband2, double distance, int *dfwithin)
Return ES_ERROR if error occurred in function.
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_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:725
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:2440

References distance(), ES_NONE, FALSE, POSTGIS_RT_DEBUGF, rtpixdump::rast, result, rt_raster_deserialize(), rt_raster_destroy(), rt_raster_fully_within_distance(), rt_raster_get_num_bands(), and rt_raster_get_srid().

Here is the call graph for this function: