PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ RASTER_touches()

Datum RASTER_touches ( PG_FUNCTION_ARGS  )

Definition at line 316 of file rtpg_spatial_relationship.c.

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

References ES_NONE, FALSE, POSTGIS_RT_DEBUGF, rtpixdump::rast, result, 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: