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

Definition at line 14281 of file rt_pg.c.

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

14282 {
14283  const int set_count = 2;
14284  rt_pgraster *pgrast[2];
14285  int pgrastpos[2] = {-1, -1};
14286  rt_raster rast[2] = {NULL};
14287  uint32_t bandindex[2] = {0};
14288  uint32_t hasbandindex[2] = {0};
14289  double distance = 0;
14290 
14291  uint32_t i;
14292  uint32_t j;
14293  uint32_t k;
14294  uint32_t numBands;
14295  int rtn;
14296  int result;
14297 
14298  for (i = 0, j = 0; i < set_count; i++) {
14299  /* pgrast is null, return null */
14300  if (PG_ARGISNULL(j)) {
14301  for (k = 0; k < i; k++) {
14302  rt_raster_destroy(rast[k]);
14303  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14304  }
14305  PG_RETURN_NULL();
14306  }
14307  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
14308  pgrastpos[i] = j;
14309  j++;
14310 
14311  /* raster */
14312  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
14313  if (!rast[i]) {
14314  for (k = 0; k <= i; k++) {
14315  if (k < i)
14316  rt_raster_destroy(rast[k]);
14317  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14318  }
14319  elog(ERROR, "RASTER_dfullywithin: Could not deserialize the %s raster", i < 1 ? "first" : "second");
14320  PG_RETURN_NULL();
14321  }
14322 
14323  /* numbands */
14324  numBands = rt_raster_get_num_bands(rast[i]);
14325  if (numBands < 1) {
14326  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
14327  if (i > 0) i++;
14328  for (k = 0; k < i; k++) {
14329  rt_raster_destroy(rast[k]);
14330  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14331  }
14332  PG_RETURN_NULL();
14333  }
14334 
14335  /* band index */
14336  if (!PG_ARGISNULL(j)) {
14337  bandindex[i] = PG_GETARG_INT32(j);
14338  if (bandindex[i] < 1 || bandindex[i] > numBands) {
14339  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
14340  if (i > 0) i++;
14341  for (k = 0; k < i; k++) {
14342  rt_raster_destroy(rast[k]);
14343  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14344  }
14345  PG_RETURN_NULL();
14346  }
14347  hasbandindex[i] = 1;
14348  }
14349  else
14350  hasbandindex[i] = 0;
14351  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
14352  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
14353  j++;
14354  }
14355 
14356  /* distance */
14357  if (PG_ARGISNULL(4)) {
14358  elog(NOTICE, "Distance cannot be NULL. Returning NULL");
14359  for (k = 0; k < set_count; k++) {
14360  rt_raster_destroy(rast[k]);
14361  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14362  }
14363  PG_RETURN_NULL();
14364  }
14365 
14366  distance = PG_GETARG_FLOAT8(4);
14367  if (distance < 0) {
14368  elog(NOTICE, "Distance cannot be less than zero. Returning NULL");
14369  for (k = 0; k < set_count; k++) {
14370  rt_raster_destroy(rast[k]);
14371  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14372  }
14373  PG_RETURN_NULL();
14374  }
14375 
14376  /* hasbandindex must be balanced */
14377  if (
14378  (hasbandindex[0] && !hasbandindex[1]) ||
14379  (!hasbandindex[0] && hasbandindex[1])
14380  ) {
14381  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
14382  for (k = 0; k < set_count; k++) {
14383  rt_raster_destroy(rast[k]);
14384  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14385  }
14386  PG_RETURN_NULL();
14387  }
14388 
14389  /* SRID must match */
14390  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
14391  for (k = 0; k < set_count; k++) {
14392  rt_raster_destroy(rast[k]);
14393  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14394  }
14395  elog(ERROR, "The two rasters provided have different SRIDs");
14396  PG_RETURN_NULL();
14397  }
14398 
14400  rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1),
14401  rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1),
14402  distance,
14403  &result
14404  );
14405  for (k = 0; k < set_count; k++) {
14406  rt_raster_destroy(rast[k]);
14407  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14408  }
14409 
14410  if (rtn != ES_NONE) {
14411  elog(ERROR, "RASTER_dfullywithin: Could not test that the two rasters are fully within the specified distance of each other");
14412  PG_RETURN_NULL();
14413  }
14414 
14415  PG_RETURN_BOOL(result);
14416 }
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
rt_errorstate rt_raster_fully_within_distance(rt_raster rast1, int nband1, rt_raster rast2, int nband2, double distance, int *dfwithin)
Return ES_ERROR if error occurred in function.
Definition: rt_api.c:12685
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
Datum distance(PG_FUNCTION_ARGS)
#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: