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

Definition at line 14021 of file rt_pg.c.

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

14022 {
14023  const int set_count = 2;
14024  rt_pgraster *pgrast[2];
14025  int pgrastpos[2] = {-1, -1};
14026  rt_raster rast[2] = {NULL};
14027  uint32_t bandindex[2] = {0};
14028  uint32_t hasbandindex[2] = {0};
14029 
14030  uint32_t i;
14031  uint32_t j;
14032  uint32_t k;
14033  uint32_t numBands;
14034  int rtn;
14035  int result;
14036 
14037  for (i = 0, j = 0; i < set_count; i++) {
14038  /* pgrast is null, return null */
14039  if (PG_ARGISNULL(j)) {
14040  for (k = 0; k < i; k++) {
14041  rt_raster_destroy(rast[k]);
14042  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14043  }
14044  PG_RETURN_NULL();
14045  }
14046  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
14047  pgrastpos[i] = j;
14048  j++;
14049 
14050  /* raster */
14051  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
14052  if (!rast[i]) {
14053  for (k = 0; k <= i; k++) {
14054  if (k < i)
14055  rt_raster_destroy(rast[k]);
14056  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14057  }
14058  elog(ERROR, "RASTER_coveredby: Could not deserialize the %s raster", i < 1 ? "first" : "second");
14059  PG_RETURN_NULL();
14060  }
14061 
14062  /* numbands */
14063  numBands = rt_raster_get_num_bands(rast[i]);
14064  if (numBands < 1) {
14065  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
14066  if (i > 0) i++;
14067  for (k = 0; k < i; k++) {
14068  rt_raster_destroy(rast[k]);
14069  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14070  }
14071  PG_RETURN_NULL();
14072  }
14073 
14074  /* band index */
14075  if (!PG_ARGISNULL(j)) {
14076  bandindex[i] = PG_GETARG_INT32(j);
14077  if (bandindex[i] < 1 || bandindex[i] > numBands) {
14078  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
14079  if (i > 0) i++;
14080  for (k = 0; k < i; k++) {
14081  rt_raster_destroy(rast[k]);
14082  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14083  }
14084  PG_RETURN_NULL();
14085  }
14086  hasbandindex[i] = 1;
14087  }
14088  else
14089  hasbandindex[i] = 0;
14090  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
14091  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
14092  j++;
14093  }
14094 
14095  /* hasbandindex must be balanced */
14096  if (
14097  (hasbandindex[0] && !hasbandindex[1]) ||
14098  (!hasbandindex[0] && hasbandindex[1])
14099  ) {
14100  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
14101  for (k = 0; k < set_count; k++) {
14102  rt_raster_destroy(rast[k]);
14103  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14104  }
14105  PG_RETURN_NULL();
14106  }
14107 
14108  /* SRID must match */
14109  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
14110  for (k = 0; k < set_count; k++) {
14111  rt_raster_destroy(rast[k]);
14112  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14113  }
14114  elog(ERROR, "The two rasters provided have different SRIDs");
14115  PG_RETURN_NULL();
14116  }
14117 
14118  rtn = rt_raster_coveredby(
14119  rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1),
14120  rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1),
14121  &result
14122  );
14123  for (k = 0; k < set_count; k++) {
14124  rt_raster_destroy(rast[k]);
14125  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
14126  }
14127 
14128  if (rtn != ES_NONE) {
14129  elog(ERROR, "RASTER_coveredby: Could not test that the first raster is covered by the second raster");
14130  PG_RETURN_NULL();
14131  }
14132 
14133  PG_RETURN_BOOL(result);
14134 }
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_coveredby(rt_raster rast1, int nband1, rt_raster rast2, int nband2, int *coveredby)
Return ES_ERROR if error occurred in function.
Definition: rt_api.c:12557
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: