PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum RASTER_contains ( PG_FUNCTION_ARGS  )

Definition at line 13664 of file rt_pg.c.

References ES_NONE, FALSE, rt_raster_serialized_t::numBands, POSTGIS_RT_DEBUGF, rtpixdump::rast, result, rt_raster_contains(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_num_bands(), and rt_raster_get_srid().

13665 {
13666  const int set_count = 2;
13667  rt_pgraster *pgrast[2];
13668  int pgrastpos[2] = {-1, -1};
13669  rt_raster rast[2] = {NULL};
13670  uint32_t bandindex[2] = {0};
13671  uint32_t hasbandindex[2] = {0};
13672 
13673  uint32_t i;
13674  uint32_t j;
13675  uint32_t k;
13676  uint32_t numBands;
13677  int rtn;
13678  int result;
13679 
13680  for (i = 0, j = 0; i < set_count; i++) {
13681  /* pgrast is null, return null */
13682  if (PG_ARGISNULL(j)) {
13683  for (k = 0; k < i; k++) {
13684  rt_raster_destroy(rast[k]);
13685  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13686  }
13687  PG_RETURN_NULL();
13688  }
13689  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
13690  pgrastpos[i] = j;
13691  j++;
13692 
13693  /* raster */
13694  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
13695  if (!rast[i]) {
13696  for (k = 0; k <= i; k++) {
13697  if (k < i)
13698  rt_raster_destroy(rast[k]);
13699  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13700  }
13701  elog(ERROR, "RASTER_contains: Could not deserialize the %s raster", i < 1 ? "first" : "second");
13702  PG_RETURN_NULL();
13703  }
13704 
13705  /* numbands */
13706  numBands = rt_raster_get_num_bands(rast[i]);
13707  if (numBands < 1) {
13708  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
13709  if (i > 0) i++;
13710  for (k = 0; k < i; k++) {
13711  rt_raster_destroy(rast[k]);
13712  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13713  }
13714  PG_RETURN_NULL();
13715  }
13716 
13717  /* band index */
13718  if (!PG_ARGISNULL(j)) {
13719  bandindex[i] = PG_GETARG_INT32(j);
13720  if (bandindex[i] < 1 || bandindex[i] > numBands) {
13721  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
13722  if (i > 0) i++;
13723  for (k = 0; k < i; k++) {
13724  rt_raster_destroy(rast[k]);
13725  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13726  }
13727  PG_RETURN_NULL();
13728  }
13729  hasbandindex[i] = 1;
13730  }
13731  else
13732  hasbandindex[i] = 0;
13733  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
13734  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
13735  j++;
13736  }
13737 
13738  /* hasbandindex must be balanced */
13739  if (
13740  (hasbandindex[0] && !hasbandindex[1]) ||
13741  (!hasbandindex[0] && hasbandindex[1])
13742  ) {
13743  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
13744  for (k = 0; k < set_count; k++) {
13745  rt_raster_destroy(rast[k]);
13746  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13747  }
13748  PG_RETURN_NULL();
13749  }
13750 
13751  /* SRID must match */
13752  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
13753  for (k = 0; k < set_count; k++) {
13754  rt_raster_destroy(rast[k]);
13755  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13756  }
13757  elog(ERROR, "The two rasters provided have different SRIDs");
13758  PG_RETURN_NULL();
13759  }
13760 
13761  rtn = rt_raster_contains(
13762  rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1),
13763  rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1),
13764  &result
13765  );
13766  for (k = 0; k < set_count; k++) {
13767  rt_raster_destroy(rast[k]);
13768  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13769  }
13770 
13771  if (rtn != ES_NONE) {
13772  elog(ERROR, "RASTER_contains: Could not test that the first raster contains the second raster");
13773  PG_RETURN_NULL();
13774  }
13775 
13776  PG_RETURN_BOOL(result);
13777 }
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_api.c:5677
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_api.c:5387
tuple rast
Definition: rtpixdump.py:62
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition: rt_api.c:5661
char ** result
Definition: liblwgeom.h:218
#define FALSE
Definition: dbfopen.c:169
Struct definitions.
Definition: rt_api.h:2175
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rt_pg.h:62
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.
Definition: rt_api.c:12464
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_api.c:8350

Here is the call graph for this function: