PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ RASTER_overlaps()

Datum RASTER_overlaps ( PG_FUNCTION_ARGS  )

Definition at line 193 of file rtpg_spatial_relationship.c.

194 {
195  const uint32_t set_count = 2;
196  rt_pgraster *pgrast[2];
197  int pgrastpos[2] = {-1, -1};
198  rt_raster rast[2] = {NULL};
199  uint32_t bandindex[2] = {0};
200  uint32_t hasbandindex[2] = {0};
201 
202  uint32_t i;
203  uint32_t j;
204  uint32_t k;
205  uint32_t numBands;
206  int rtn;
207  int result;
208 
209  for (i = 0, j = 0; i < set_count; i++) {
210  /* pgrast is null, return null */
211  if (PG_ARGISNULL(j)) {
212  for (k = 0; k < i; k++) {
214  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
215  }
216  PG_RETURN_NULL();
217  }
218  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
219  pgrastpos[i] = j;
220  j++;
221 
222  /* raster */
223  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
224  if (!rast[i]) {
225  for (k = 0; k <= i; k++) {
226  if (k < i)
228  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
229  }
230  elog(ERROR, "RASTER_overlaps: Could not deserialize the %s raster", i < 1 ? "first" : "second");
231  PG_RETURN_NULL();
232  }
233 
234  /* numbands */
235  numBands = rt_raster_get_num_bands(rast[i]);
236  if (numBands < 1) {
237  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
238  if (i > 0) i++;
239  for (k = 0; k < i; k++) {
241  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
242  }
243  PG_RETURN_NULL();
244  }
245 
246  /* band index */
247  if (!PG_ARGISNULL(j)) {
248  bandindex[i] = PG_GETARG_INT32(j);
249  if (bandindex[i] < 1 || bandindex[i] > numBands) {
250  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
251  if (i > 0) i++;
252  for (k = 0; k < i; k++) {
254  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
255  }
256  PG_RETURN_NULL();
257  }
258  hasbandindex[i] = 1;
259  }
260  else
261  hasbandindex[i] = 0;
262  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
263  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
264  j++;
265  }
266 
267  /* hasbandindex must be balanced */
268  if (
269  (hasbandindex[0] && !hasbandindex[1]) ||
270  (!hasbandindex[0] && hasbandindex[1])
271  ) {
272  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
273  for (k = 0; k < set_count; k++) {
275  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
276  }
277  PG_RETURN_NULL();
278  }
279 
280  /* SRID must match */
282  for (k = 0; k < set_count; k++) {
284  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
285  }
286  elog(ERROR, "The two rasters provided have different SRIDs");
287  PG_RETURN_NULL();
288  }
289 
290  rtn = rt_raster_overlaps(
291  rast[0], (hasbandindex[0] ? (int)bandindex[0] - 1 : -1),
292  rast[1], (hasbandindex[1] ? (int)bandindex[1] - 1 : -1),
293  &result
294  );
295  for (k = 0; k < set_count; k++) {
297  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
298  }
299 
300  if (rtn != ES_NONE) {
301  elog(ERROR, "RASTER_overlaps: Could not test for overlap on the two rasters");
302  PG_RETURN_NULL();
303  }
304 
305  PG_RETURN_BOOL(result);
306 }
#define FALSE
Definition: dbfopen.c:168
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition: rt_raster.c:356
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
@ ES_NONE
Definition: librtcore.h:180
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.
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:725
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
Struct definitions.
Definition: librtcore.h:2250
unsigned int uint32_t
Definition: uthash.h:78

References ES_NONE, FALSE, POSTGIS_RT_DEBUGF, rtpixdump::rast, rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_num_bands(), rt_raster_get_srid(), and rt_raster_overlaps().

Here is the call graph for this function: