PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ RASTER_dfullywithin()

Datum RASTER_dfullywithin ( PG_FUNCTION_ARGS  )

Definition at line 1047 of file rtpg_spatial_relationship.c.

References distance(), ES_NONE, FALSE, rt_raster_serialized_t::numBands, PG_FUNCTION_INFO_V1(), POSTGIS_RT_DEBUGF, rtpixdump::rast, RASTER_sameAlignment(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_fully_within_distance(), rt_raster_get_num_bands(), and rt_raster_get_srid().

Referenced by RASTER_dwithin().

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