PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ RASTER_touches()

Datum RASTER_touches ( PG_FUNCTION_ARGS  )

Definition at line 312 of file rtpg_spatial_relationship.c.

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

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

Here is the call graph for this function: