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

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

13546 {
13547  const int set_count = 2;
13548  rt_pgraster *pgrast[2];
13549  int pgrastpos[2] = {-1, -1};
13550  rt_raster rast[2] = {NULL};
13551  uint32_t bandindex[2] = {0};
13552  uint32_t hasbandindex[2] = {0};
13553 
13554  uint32_t i;
13555  uint32_t j;
13556  uint32_t k;
13557  uint32_t numBands;
13558  int rtn;
13559  int result;
13560 
13561  for (i = 0, j = 0; i < set_count; i++) {
13562  /* pgrast is null, return null */
13563  if (PG_ARGISNULL(j)) {
13564  for (k = 0; k < i; k++) {
13565  rt_raster_destroy(rast[k]);
13566  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13567  }
13568  PG_RETURN_NULL();
13569  }
13570  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
13571  pgrastpos[i] = j;
13572  j++;
13573 
13574  /* raster */
13575  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
13576  if (!rast[i]) {
13577  for (k = 0; k <= i; k++) {
13578  if (k < i)
13579  rt_raster_destroy(rast[k]);
13580  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13581  }
13582  elog(ERROR, "RASTER_touches: Could not deserialize the %s raster", i < 1 ? "first" : "second");
13583  PG_RETURN_NULL();
13584  }
13585 
13586  /* numbands */
13587  numBands = rt_raster_get_num_bands(rast[i]);
13588  if (numBands < 1) {
13589  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
13590  if (i > 0) i++;
13591  for (k = 0; k < i; k++) {
13592  rt_raster_destroy(rast[k]);
13593  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13594  }
13595  PG_RETURN_NULL();
13596  }
13597 
13598  /* band index */
13599  if (!PG_ARGISNULL(j)) {
13600  bandindex[i] = PG_GETARG_INT32(j);
13601  if (bandindex[i] < 1 || bandindex[i] > numBands) {
13602  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
13603  if (i > 0) i++;
13604  for (k = 0; k < i; k++) {
13605  rt_raster_destroy(rast[k]);
13606  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13607  }
13608  PG_RETURN_NULL();
13609  }
13610  hasbandindex[i] = 1;
13611  }
13612  else
13613  hasbandindex[i] = 0;
13614  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
13615  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
13616  j++;
13617  }
13618 
13619  /* hasbandindex must be balanced */
13620  if (
13621  (hasbandindex[0] && !hasbandindex[1]) ||
13622  (!hasbandindex[0] && hasbandindex[1])
13623  ) {
13624  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
13625  for (k = 0; k < set_count; k++) {
13626  rt_raster_destroy(rast[k]);
13627  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13628  }
13629  PG_RETURN_NULL();
13630  }
13631 
13632  /* SRID must match */
13633  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
13634  for (k = 0; k < set_count; k++) {
13635  rt_raster_destroy(rast[k]);
13636  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13637  }
13638  elog(ERROR, "The two rasters provided have different SRIDs");
13639  PG_RETURN_NULL();
13640  }
13641 
13642  rtn = rt_raster_touches(
13643  rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1),
13644  rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1),
13645  &result
13646  );
13647  for (k = 0; k < set_count; k++) {
13648  rt_raster_destroy(rast[k]);
13649  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13650  }
13651 
13652  if (rtn != ES_NONE) {
13653  elog(ERROR, "RASTER_touches: Could not test for touch on the two rasters");
13654  PG_RETURN_NULL();
13655  }
13656 
13657  PG_RETURN_BOOL(result);
13658 }
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
rt_errorstate rt_raster_touches(rt_raster rast1, int nband1, rt_raster rast2, int nband2, int *touches)
Return ES_ERROR if error occurred in function.
Definition: rt_api.c:12433
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: