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

Definition at line 13902 of file rt_pg.c.

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

13903 {
13904  const int set_count = 2;
13905  rt_pgraster *pgrast[2];
13906  int pgrastpos[2] = {-1, -1};
13907  rt_raster rast[2] = {NULL};
13908  uint32_t bandindex[2] = {0};
13909  uint32_t hasbandindex[2] = {0};
13910 
13911  uint32_t i;
13912  uint32_t j;
13913  uint32_t k;
13914  uint32_t numBands;
13915  int rtn;
13916  int result;
13917 
13918  for (i = 0, j = 0; i < set_count; i++) {
13919  /* pgrast is null, return null */
13920  if (PG_ARGISNULL(j)) {
13921  for (k = 0; k < i; k++) {
13922  rt_raster_destroy(rast[k]);
13923  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13924  }
13925  PG_RETURN_NULL();
13926  }
13927  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
13928  pgrastpos[i] = j;
13929  j++;
13930 
13931  /* raster */
13932  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
13933  if (!rast[i]) {
13934  for (k = 0; k <= i; k++) {
13935  if (k < i)
13936  rt_raster_destroy(rast[k]);
13937  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13938  }
13939  elog(ERROR, "RASTER_covers: Could not deserialize the %s raster", i < 1 ? "first" : "second");
13940  PG_RETURN_NULL();
13941  }
13942 
13943  /* numbands */
13944  numBands = rt_raster_get_num_bands(rast[i]);
13945  if (numBands < 1) {
13946  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
13947  if (i > 0) i++;
13948  for (k = 0; k < i; k++) {
13949  rt_raster_destroy(rast[k]);
13950  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13951  }
13952  PG_RETURN_NULL();
13953  }
13954 
13955  /* band index */
13956  if (!PG_ARGISNULL(j)) {
13957  bandindex[i] = PG_GETARG_INT32(j);
13958  if (bandindex[i] < 1 || bandindex[i] > numBands) {
13959  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
13960  if (i > 0) i++;
13961  for (k = 0; k < i; k++) {
13962  rt_raster_destroy(rast[k]);
13963  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13964  }
13965  PG_RETURN_NULL();
13966  }
13967  hasbandindex[i] = 1;
13968  }
13969  else
13970  hasbandindex[i] = 0;
13971  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
13972  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
13973  j++;
13974  }
13975 
13976  /* hasbandindex must be balanced */
13977  if (
13978  (hasbandindex[0] && !hasbandindex[1]) ||
13979  (!hasbandindex[0] && hasbandindex[1])
13980  ) {
13981  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
13982  for (k = 0; k < set_count; k++) {
13983  rt_raster_destroy(rast[k]);
13984  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13985  }
13986  PG_RETURN_NULL();
13987  }
13988 
13989  /* SRID must match */
13990  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
13991  for (k = 0; k < set_count; k++) {
13992  rt_raster_destroy(rast[k]);
13993  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
13994  }
13995  elog(ERROR, "The two rasters provided have different SRIDs");
13996  PG_RETURN_NULL();
13997  }
13998 
13999  rtn = rt_raster_covers(
14000  rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1),
14001  rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1),
14002  &result
14003  );
14004  for (k = 0; k < set_count; k++) {
14005  rt_raster_destroy(rast[k]);
14006  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14007  }
14008 
14009  if (rtn != ES_NONE) {
14010  elog(ERROR, "RASTER_covers: Could not test that the first raster covers the second raster");
14011  PG_RETURN_NULL();
14012  }
14013 
14014  PG_RETURN_BOOL(result);
14015 }
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_covers(rt_raster rast1, int nband1, rt_raster rast2, int nband2, int *covers)
Return ES_ERROR if error occurred in function.
Definition: rt_api.c:12526
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_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: