PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_touches()

Datum RASTER_touches ( PG_FUNCTION_ARGS  )

Definition at line 311 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_contains(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_num_bands(), rt_raster_get_srid(), and rt_raster_touches().

Referenced by RASTER_overlaps().

312 {
313  const int set_count = 2;
314  rt_pgraster *pgrast[2];
315  int pgrastpos[2] = {-1, -1};
316  rt_raster rast[2] = {NULL};
317  uint32_t bandindex[2] = {0};
318  uint32_t hasbandindex[2] = {0};
319 
320  uint32_t i;
321  uint32_t j;
322  uint32_t k;
324  int rtn;
325  int result;
326 
327  for (i = 0, j = 0; i < set_count; i++) {
328  /* pgrast is null, return null */
329  if (PG_ARGISNULL(j)) {
330  for (k = 0; k < i; k++) {
331  rt_raster_destroy(rast[k]);
332  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
333  }
334  PG_RETURN_NULL();
335  }
336  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
337  pgrastpos[i] = j;
338  j++;
339 
340  /* raster */
341  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
342  if (!rast[i]) {
343  for (k = 0; k <= i; k++) {
344  if (k < i)
345  rt_raster_destroy(rast[k]);
346  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
347  }
348  elog(ERROR, "RASTER_touches: Could not deserialize the %s raster", i < 1 ? "first" : "second");
349  PG_RETURN_NULL();
350  }
351 
352  /* numbands */
353  numBands = rt_raster_get_num_bands(rast[i]);
354  if (numBands < 1) {
355  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
356  if (i > 0) i++;
357  for (k = 0; k < i; k++) {
358  rt_raster_destroy(rast[k]);
359  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
360  }
361  PG_RETURN_NULL();
362  }
363 
364  /* band index */
365  if (!PG_ARGISNULL(j)) {
366  bandindex[i] = PG_GETARG_INT32(j);
367  if (bandindex[i] < 1 || bandindex[i] > numBands) {
368  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
369  if (i > 0) i++;
370  for (k = 0; k < i; k++) {
371  rt_raster_destroy(rast[k]);
372  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
373  }
374  PG_RETURN_NULL();
375  }
376  hasbandindex[i] = 1;
377  }
378  else
379  hasbandindex[i] = 0;
380  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
381  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
382  j++;
383  }
384 
385  /* hasbandindex must be balanced */
386  if (
387  (hasbandindex[0] && !hasbandindex[1]) ||
388  (!hasbandindex[0] && hasbandindex[1])
389  ) {
390  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
391  for (k = 0; k < set_count; k++) {
392  rt_raster_destroy(rast[k]);
393  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
394  }
395  PG_RETURN_NULL();
396  }
397 
398  /* SRID must match */
399  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
400  for (k = 0; k < set_count; k++) {
401  rt_raster_destroy(rast[k]);
402  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
403  }
404  elog(ERROR, "The two rasters provided have different SRIDs");
405  PG_RETURN_NULL();
406  }
407 
408  rtn = rt_raster_touches(
409  rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1),
410  rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1),
411  &result
412  );
413  for (k = 0; k < set_count; k++) {
414  rt_raster_destroy(rast[k]);
415  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
416  }
417 
418  if (rtn != ES_NONE) {
419  elog(ERROR, "RASTER_touches: Could not test for touch on the two rasters");
420  PG_RETURN_NULL();
421  }
422 
423  PG_RETURN_BOOL(result);
424 }
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_errorstate rt_raster_touches(rt_raster rast1, int nband1, rt_raster rast2, int nband2, int *touches)
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: