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

◆ RASTER_contains()

Datum RASTER_contains ( PG_FUNCTION_ARGS  )

Definition at line 435 of file rtpg_spatial_relationship.c.

436{
437 const uint32_t set_count = 2;
438 rt_pgraster *pgrast[2];
439 int pgrastpos[2] = {-1, -1};
440 rt_raster rast[2] = {NULL};
441 uint32_t bandindex[2] = {0};
442 uint32_t hasbandindex[2] = {0};
443
444 uint32_t i;
445 uint32_t j;
446 uint32_t k;
447 uint32_t numBands;
448 int rtn;
449 int result;
450
451 for (i = 0, j = 0; i < set_count; i++) {
452 /* pgrast is null, return null */
453 if (PG_ARGISNULL(j)) {
454 for (k = 0; k < i; k++) {
455 rt_raster_destroy(rast[k]);
456 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
457 }
458 PG_RETURN_NULL();
459 }
460 pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
461 pgrastpos[i] = j;
462 j++;
463
464 /* raster */
465 rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
466 if (!rast[i]) {
467 for (k = 0; k <= i; k++) {
468 if (k < i)
469 rt_raster_destroy(rast[k]);
470 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
471 }
472 elog(ERROR, "RASTER_contains: Could not deserialize the %s raster", i < 1 ? "first" : "second");
473 PG_RETURN_NULL();
474 }
475
476 /* numbands */
477 numBands = rt_raster_get_num_bands(rast[i]);
478 if (numBands < 1) {
479 elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
480 if (i > 0) i++;
481 for (k = 0; k < i; k++) {
482 rt_raster_destroy(rast[k]);
483 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
484 }
485 PG_RETURN_NULL();
486 }
487
488 /* band index */
489 if (!PG_ARGISNULL(j)) {
490 bandindex[i] = PG_GETARG_INT32(j);
491 if (bandindex[i] < 1 || bandindex[i] > numBands) {
492 elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
493 if (i > 0) i++;
494 for (k = 0; k < i; k++) {
495 rt_raster_destroy(rast[k]);
496 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
497 }
498 PG_RETURN_NULL();
499 }
500 hasbandindex[i] = 1;
501 }
502 else
503 hasbandindex[i] = 0;
504 POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
505 POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
506 j++;
507 }
508
509 /* hasbandindex must be balanced */
510 if (
511 (hasbandindex[0] && !hasbandindex[1]) ||
512 (!hasbandindex[0] && hasbandindex[1])
513 ) {
514 elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
515 for (k = 0; k < set_count; k++) {
516 rt_raster_destroy(rast[k]);
517 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
518 }
519 PG_RETURN_NULL();
520 }
521
522 /* SRID must match */
523 if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
524 for (k = 0; k < set_count; k++) {
525 rt_raster_destroy(rast[k]);
526 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
527 }
528 elog(ERROR, "The two rasters provided have different SRIDs");
529 PG_RETURN_NULL();
530 }
531
532 rtn = rt_raster_contains(
533 rast[0], (hasbandindex[0] ? (int)bandindex[0] - 1 : -1),
534 rast[1], (hasbandindex[1] ? (int)bandindex[1] - 1 : -1),
535 &result
536 );
537 for (k = 0; k < set_count; k++) {
538 rt_raster_destroy(rast[k]);
539 PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
540 }
541
542 if (rtn != ES_NONE) {
543 elog(ERROR, "RASTER_contains: Could not test that the first raster contains the second raster");
544 PG_RETURN_NULL();
545 }
546
547 PG_RETURN_BOOL(result);
548}
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition cu_print.c:267
#define FALSE
Definition dbfopen.c:72
rt_errorstate rt_raster_contains(rt_raster rast1, int nband1, rt_raster rast2, int nband2, int *contains)
Return ES_ERROR if error occurred in function.
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
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_contains(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_num_bands(), and rt_raster_get_srid().

Here is the call graph for this function: