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

Definition at line 13307 of file rt_pg.c.

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

13308 {
13309  const int set_count = 2;
13310  rt_pgraster *pgrast[2];
13311  int pgrastpos[2] = {-1, -1};
13312  rt_raster rast[2] = {NULL};
13313  uint32_t bandindex[2] = {0};
13314  uint32_t hasbandindex[2] = {0};
13315 
13316  uint32_t i;
13317  uint32_t j;
13318  uint32_t k;
13319  uint32_t numBands;
13320  int rtn;
13321  int result;
13322 
13323  for (i = 0, j = 0; i < set_count; i++) {
13324  /* pgrast is null, return null */
13325  if (PG_ARGISNULL(j)) {
13326  for (k = 0; k < i; k++) {
13327  rt_raster_destroy(rast[k]);
13328  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13329  }
13330  PG_RETURN_NULL();
13331  }
13332  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
13333  pgrastpos[i] = j;
13334  j++;
13335 
13336  /* raster */
13337  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
13338  if (!rast[i]) {
13339  for (k = 0; k <= i; k++) {
13340  if (k < i)
13341  rt_raster_destroy(rast[k]);
13342  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13343  }
13344  elog(ERROR, "RASTER_intersects: Could not deserialize the %s raster", i < 1 ? "first" : "second");
13345  PG_RETURN_NULL();
13346  }
13347 
13348  /* numbands */
13349  numBands = rt_raster_get_num_bands(rast[i]);
13350  if (numBands < 1) {
13351  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
13352  if (i > 0) i++;
13353  for (k = 0; k < i; k++) {
13354  rt_raster_destroy(rast[k]);
13355  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13356  }
13357  PG_RETURN_NULL();
13358  }
13359 
13360  /* band index */
13361  if (!PG_ARGISNULL(j)) {
13362  bandindex[i] = PG_GETARG_INT32(j);
13363  if (bandindex[i] < 1 || bandindex[i] > numBands) {
13364  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
13365  if (i > 0) i++;
13366  for (k = 0; k < i; k++) {
13367  rt_raster_destroy(rast[k]);
13368  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13369  }
13370  PG_RETURN_NULL();
13371  }
13372  hasbandindex[i] = 1;
13373  }
13374  else
13375  hasbandindex[i] = 0;
13376  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
13377  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
13378  j++;
13379  }
13380 
13381  /* hasbandindex must be balanced */
13382  if (
13383  (hasbandindex[0] && !hasbandindex[1]) ||
13384  (!hasbandindex[0] && hasbandindex[1])
13385  ) {
13386  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
13387  for (k = 0; k < set_count; k++) {
13388  rt_raster_destroy(rast[k]);
13389  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13390  }
13391  PG_RETURN_NULL();
13392  }
13393 
13394  /* SRID must match */
13395  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
13396  for (k = 0; k < set_count; k++) {
13397  rt_raster_destroy(rast[k]);
13398  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13399  }
13400  elog(ERROR, "The two rasters provided have different SRIDs");
13401  PG_RETURN_NULL();
13402  }
13403 
13404  rtn = rt_raster_intersects(
13405  rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1),
13406  rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1),
13407  &result
13408  );
13409  for (k = 0; k < set_count; k++) {
13410  rt_raster_destroy(rast[k]);
13411  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13412  }
13413 
13414  if (rtn != ES_NONE) {
13415  elog(ERROR, "RASTER_intersects: Could not test for intersection on the two rasters");
13416  PG_RETURN_NULL();
13417  }
13418 
13419  PG_RETURN_BOOL(result);
13420 }
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
rt_errorstate rt_raster_intersects(rt_raster rast1, int nband1, rt_raster rast2, int nband2, int *intersects)
Return zero if error occurred in function.
Definition: rt_api.c:11953
#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_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: