PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ RASTER_contains()

Datum RASTER_contains ( PG_FUNCTION_ARGS  )

Definition at line 431 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_containsProperly(), rt_raster_contains(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_num_bands(), and rt_raster_get_srid().

Referenced by RASTER_touches().

432 {
433  const uint32_t set_count = 2;
434  rt_pgraster *pgrast[2];
435  int pgrastpos[2] = {-1, -1};
436  rt_raster rast[2] = {NULL};
437  uint32_t bandindex[2] = {0};
438  uint32_t hasbandindex[2] = {0};
439 
440  uint32_t i;
441  uint32_t j;
442  uint32_t k;
444  int rtn;
445  int result;
446 
447  for (i = 0, j = 0; i < set_count; i++) {
448  /* pgrast is null, return null */
449  if (PG_ARGISNULL(j)) {
450  for (k = 0; k < i; k++) {
451  rt_raster_destroy(rast[k]);
452  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
453  }
454  PG_RETURN_NULL();
455  }
456  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
457  pgrastpos[i] = j;
458  j++;
459 
460  /* raster */
461  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
462  if (!rast[i]) {
463  for (k = 0; k <= i; k++) {
464  if (k < i)
465  rt_raster_destroy(rast[k]);
466  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
467  }
468  elog(ERROR, "RASTER_contains: Could not deserialize the %s raster", i < 1 ? "first" : "second");
469  PG_RETURN_NULL();
470  }
471 
472  /* numbands */
473  numBands = rt_raster_get_num_bands(rast[i]);
474  if (numBands < 1) {
475  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
476  if (i > 0) i++;
477  for (k = 0; k < i; k++) {
478  rt_raster_destroy(rast[k]);
479  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
480  }
481  PG_RETURN_NULL();
482  }
483 
484  /* band index */
485  if (!PG_ARGISNULL(j)) {
486  bandindex[i] = PG_GETARG_INT32(j);
487  if (bandindex[i] < 1 || bandindex[i] > numBands) {
488  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
489  if (i > 0) i++;
490  for (k = 0; k < i; k++) {
491  rt_raster_destroy(rast[k]);
492  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
493  }
494  PG_RETURN_NULL();
495  }
496  hasbandindex[i] = 1;
497  }
498  else
499  hasbandindex[i] = 0;
500  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
501  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
502  j++;
503  }
504 
505  /* hasbandindex must be balanced */
506  if (
507  (hasbandindex[0] && !hasbandindex[1]) ||
508  (!hasbandindex[0] && hasbandindex[1])
509  ) {
510  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
511  for (k = 0; k < set_count; k++) {
512  rt_raster_destroy(rast[k]);
513  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
514  }
515  PG_RETURN_NULL();
516  }
517 
518  /* SRID must match */
519  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
520  for (k = 0; k < set_count; k++) {
521  rt_raster_destroy(rast[k]);
522  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
523  }
524  elog(ERROR, "The two rasters provided have different SRIDs");
525  PG_RETURN_NULL();
526  }
527 
528  rtn = rt_raster_contains(
529  rast[0], (hasbandindex[0] ? (int)bandindex[0] - 1 : -1),
530  rast[1], (hasbandindex[1] ? (int)bandindex[1] - 1 : -1),
531  &result
532  );
533  for (k = 0; k < set_count; k++) {
534  rt_raster_destroy(rast[k]);
535  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
536  }
537 
538  if (rtn != ES_NONE) {
539  elog(ERROR, "RASTER_contains: Could not test that the first raster contains the second raster");
540  PG_RETURN_NULL();
541  }
542 
543  PG_RETURN_BOOL(result);
544 }
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
rt_errorstate rt_raster_contains(rt_raster rast1, int nband1, rt_raster rast2, int nband2, int *contains)
Return ES_ERROR if error occurred in function.
#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:2230
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: