PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ RASTER_covers()

Datum RASTER_covers ( PG_FUNCTION_ARGS  )

Definition at line 673 of file rtpg_spatial_relationship.c.

674 {
675  const uint32_t set_count = 2;
676  rt_pgraster *pgrast[2];
677  int pgrastpos[2] = {-1, -1};
678  rt_raster rast[2] = {NULL};
679  uint32_t bandindex[2] = {0};
680  uint32_t hasbandindex[2] = {0};
681 
682  uint32_t i;
683  uint32_t j;
684  uint32_t k;
685  uint32_t numBands;
686  int rtn;
687  int result;
688 
689  for (i = 0, j = 0; i < set_count; i++) {
690  /* pgrast is null, return null */
691  if (PG_ARGISNULL(j)) {
692  for (k = 0; k < i; k++) {
694  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
695  }
696  PG_RETURN_NULL();
697  }
698  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
699  pgrastpos[i] = j;
700  j++;
701 
702  /* raster */
703  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
704  if (!rast[i]) {
705  for (k = 0; k <= i; k++) {
706  if (k < i)
708  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
709  }
710  elog(ERROR, "RASTER_covers: Could not deserialize the %s raster", i < 1 ? "first" : "second");
711  PG_RETURN_NULL();
712  }
713 
714  /* numbands */
715  numBands = rt_raster_get_num_bands(rast[i]);
716  if (numBands < 1) {
717  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
718  if (i > 0) i++;
719  for (k = 0; k < i; k++) {
721  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
722  }
723  PG_RETURN_NULL();
724  }
725 
726  /* band index */
727  if (!PG_ARGISNULL(j)) {
728  bandindex[i] = PG_GETARG_INT32(j);
729  if (bandindex[i] < 1 || bandindex[i] > numBands) {
730  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
731  if (i > 0) i++;
732  for (k = 0; k < i; k++) {
734  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
735  }
736  PG_RETURN_NULL();
737  }
738  hasbandindex[i] = 1;
739  }
740  else
741  hasbandindex[i] = 0;
742  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
743  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
744  j++;
745  }
746 
747  /* hasbandindex must be balanced */
748  if (
749  (hasbandindex[0] && !hasbandindex[1]) ||
750  (!hasbandindex[0] && hasbandindex[1])
751  ) {
752  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
753  for (k = 0; k < set_count; k++) {
755  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
756  }
757  PG_RETURN_NULL();
758  }
759 
760  /* SRID must match */
762  for (k = 0; k < set_count; k++) {
764  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
765  }
766  elog(ERROR, "The two rasters provided have different SRIDs");
767  PG_RETURN_NULL();
768  }
769 
770  rtn = rt_raster_covers(
771  rast[0], (hasbandindex[0] ? (int)bandindex[0] - 1 : -1),
772  rast[1], (hasbandindex[1] ? (int)bandindex[1] - 1 : -1),
773  &result
774  );
775  for (k = 0; k < set_count; k++) {
777  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
778  }
779 
780  if (rtn != ES_NONE) {
781  elog(ERROR, "RASTER_covers: Could not test that the first raster covers the second raster");
782  PG_RETURN_NULL();
783  }
784 
785  PG_RETURN_BOOL(result);
786 }
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
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:376
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.
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:69
Struct definitions.
Definition: librtcore.h:2440

References ES_NONE, FALSE, 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().

Here is the call graph for this function: