PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_overlaps()

Datum RASTER_overlaps ( PG_FUNCTION_ARGS  )

Definition at line 192 of file rtpg_spatial_relationship.c.

References ES_NONE, FALSE, rt_raster_serialized_t::numBands, PG_FUNCTION_INFO_V1(), POSTGIS_RT_DEBUGF, rtpixdump::rast, RASTER_touches(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_num_bands(), rt_raster_get_srid(), and rt_raster_overlaps().

Referenced by RASTER_intersects().

193 {
194  const int set_count = 2;
195  rt_pgraster *pgrast[2];
196  int pgrastpos[2] = {-1, -1};
197  rt_raster rast[2] = {NULL};
198  uint32_t bandindex[2] = {0};
199  uint32_t hasbandindex[2] = {0};
200 
201  uint32_t i;
202  uint32_t j;
203  uint32_t k;
205  int rtn;
206  int result;
207 
208  for (i = 0, j = 0; i < set_count; i++) {
209  /* pgrast is null, return null */
210  if (PG_ARGISNULL(j)) {
211  for (k = 0; k < i; k++) {
212  rt_raster_destroy(rast[k]);
213  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
214  }
215  PG_RETURN_NULL();
216  }
217  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
218  pgrastpos[i] = j;
219  j++;
220 
221  /* raster */
222  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
223  if (!rast[i]) {
224  for (k = 0; k <= i; k++) {
225  if (k < i)
226  rt_raster_destroy(rast[k]);
227  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
228  }
229  elog(ERROR, "RASTER_overlaps: Could not deserialize the %s raster", i < 1 ? "first" : "second");
230  PG_RETURN_NULL();
231  }
232 
233  /* numbands */
234  numBands = rt_raster_get_num_bands(rast[i]);
235  if (numBands < 1) {
236  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
237  if (i > 0) i++;
238  for (k = 0; k < i; k++) {
239  rt_raster_destroy(rast[k]);
240  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
241  }
242  PG_RETURN_NULL();
243  }
244 
245  /* band index */
246  if (!PG_ARGISNULL(j)) {
247  bandindex[i] = PG_GETARG_INT32(j);
248  if (bandindex[i] < 1 || bandindex[i] > numBands) {
249  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
250  if (i > 0) i++;
251  for (k = 0; k < i; k++) {
252  rt_raster_destroy(rast[k]);
253  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
254  }
255  PG_RETURN_NULL();
256  }
257  hasbandindex[i] = 1;
258  }
259  else
260  hasbandindex[i] = 0;
261  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
262  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
263  j++;
264  }
265 
266  /* hasbandindex must be balanced */
267  if (
268  (hasbandindex[0] && !hasbandindex[1]) ||
269  (!hasbandindex[0] && hasbandindex[1])
270  ) {
271  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
272  for (k = 0; k < set_count; k++) {
273  rt_raster_destroy(rast[k]);
274  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
275  }
276  PG_RETURN_NULL();
277  }
278 
279  /* SRID must match */
280  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
281  for (k = 0; k < set_count; k++) {
282  rt_raster_destroy(rast[k]);
283  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
284  }
285  elog(ERROR, "The two rasters provided have different SRIDs");
286  PG_RETURN_NULL();
287  }
288 
289  rtn = rt_raster_overlaps(
290  rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1),
291  rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1),
292  &result
293  );
294  for (k = 0; k < set_count; k++) {
295  rt_raster_destroy(rast[k]);
296  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
297  }
298 
299  if (rtn != ES_NONE) {
300  elog(ERROR, "RASTER_overlaps: Could not test for overlap on the two rasters");
301  PG_RETURN_NULL();
302  }
303 
304  PG_RETURN_BOOL(result);
305 }
int 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
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2201
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:717
rt_errorstate rt_raster_overlaps(rt_raster rast1, int nband1, rt_raster rast2, int nband2, int *overlaps)
Return ES_ERROR if error occurred in function.
Here is the call graph for this function:
Here is the caller graph for this function: