PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ RASTER_overlaps()

Datum RASTER_overlaps ( PG_FUNCTION_ARGS  )

Definition at line 197 of file rtpg_spatial_relationship.c.

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

References ES_NONE, FALSE, POSTGIS_RT_DEBUGF, result, 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: