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

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

13427 {
13428  const int set_count = 2;
13429  rt_pgraster *pgrast[2];
13430  int pgrastpos[2] = {-1, -1};
13431  rt_raster rast[2] = {NULL};
13432  uint32_t bandindex[2] = {0};
13433  uint32_t hasbandindex[2] = {0};
13434 
13435  uint32_t i;
13436  uint32_t j;
13437  uint32_t k;
13438  uint32_t numBands;
13439  int rtn;
13440  int result;
13441 
13442  for (i = 0, j = 0; i < set_count; i++) {
13443  /* pgrast is null, return null */
13444  if (PG_ARGISNULL(j)) {
13445  for (k = 0; k < i; k++) {
13446  rt_raster_destroy(rast[k]);
13447  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13448  }
13449  PG_RETURN_NULL();
13450  }
13451  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
13452  pgrastpos[i] = j;
13453  j++;
13454 
13455  /* raster */
13456  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
13457  if (!rast[i]) {
13458  for (k = 0; k <= i; k++) {
13459  if (k < i)
13460  rt_raster_destroy(rast[k]);
13461  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13462  }
13463  elog(ERROR, "RASTER_overlaps: Could not deserialize the %s raster", i < 1 ? "first" : "second");
13464  PG_RETURN_NULL();
13465  }
13466 
13467  /* numbands */
13468  numBands = rt_raster_get_num_bands(rast[i]);
13469  if (numBands < 1) {
13470  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
13471  if (i > 0) i++;
13472  for (k = 0; k < i; k++) {
13473  rt_raster_destroy(rast[k]);
13474  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13475  }
13476  PG_RETURN_NULL();
13477  }
13478 
13479  /* band index */
13480  if (!PG_ARGISNULL(j)) {
13481  bandindex[i] = PG_GETARG_INT32(j);
13482  if (bandindex[i] < 1 || bandindex[i] > numBands) {
13483  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
13484  if (i > 0) i++;
13485  for (k = 0; k < i; k++) {
13486  rt_raster_destroy(rast[k]);
13487  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13488  }
13489  PG_RETURN_NULL();
13490  }
13491  hasbandindex[i] = 1;
13492  }
13493  else
13494  hasbandindex[i] = 0;
13495  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
13496  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
13497  j++;
13498  }
13499 
13500  /* hasbandindex must be balanced */
13501  if (
13502  (hasbandindex[0] && !hasbandindex[1]) ||
13503  (!hasbandindex[0] && hasbandindex[1])
13504  ) {
13505  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
13506  for (k = 0; k < set_count; k++) {
13507  rt_raster_destroy(rast[k]);
13508  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13509  }
13510  PG_RETURN_NULL();
13511  }
13512 
13513  /* SRID must match */
13514  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
13515  for (k = 0; k < set_count; k++) {
13516  rt_raster_destroy(rast[k]);
13517  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13518  }
13519  elog(ERROR, "The two rasters provided have different SRIDs");
13520  PG_RETURN_NULL();
13521  }
13522 
13523  rtn = rt_raster_overlaps(
13524  rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1),
13525  rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1),
13526  &result
13527  );
13528  for (k = 0; k < set_count; k++) {
13529  rt_raster_destroy(rast[k]);
13530  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13531  }
13532 
13533  if (rtn != ES_NONE) {
13534  elog(ERROR, "RASTER_overlaps: Could not test for overlap on the two rasters");
13535  PG_RETURN_NULL();
13536  }
13537 
13538  PG_RETURN_BOOL(result);
13539 }
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
rt_errorstate rt_raster_overlaps(rt_raster rast1, int nband1, rt_raster rast2, int nband2, int *overlaps)
Return ES_ERROR if error occurred in function.
Definition: rt_api.c:12402
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_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: