PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_contains()

Datum RASTER_contains ( PG_FUNCTION_ARGS  )

Definition at line 430 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().

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