PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ RASTER_dfullywithin()

Datum RASTER_dfullywithin ( PG_FUNCTION_ARGS  )

Definition at line 1048 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().

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