PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_covers()

Datum RASTER_covers ( PG_FUNCTION_ARGS  )

Definition at line 668 of file rtpg_spatial_relationship.c.

References ES_NONE, FALSE, rt_raster_serialized_t::numBands, PG_FUNCTION_INFO_V1(), POSTGIS_RT_DEBUGF, rtpixdump::rast, RASTER_coveredby(), rt_raster_covers(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_num_bands(), and rt_raster_get_srid().

Referenced by RASTER_containsProperly().

669 {
670  const int set_count = 2;
671  rt_pgraster *pgrast[2];
672  int pgrastpos[2] = {-1, -1};
673  rt_raster rast[2] = {NULL};
674  uint32_t bandindex[2] = {0};
675  uint32_t hasbandindex[2] = {0};
676 
677  uint32_t i;
678  uint32_t j;
679  uint32_t k;
681  int rtn;
682  int result;
683 
684  for (i = 0, j = 0; i < set_count; i++) {
685  /* pgrast is null, return null */
686  if (PG_ARGISNULL(j)) {
687  for (k = 0; k < i; k++) {
688  rt_raster_destroy(rast[k]);
689  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
690  }
691  PG_RETURN_NULL();
692  }
693  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
694  pgrastpos[i] = j;
695  j++;
696 
697  /* raster */
698  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
699  if (!rast[i]) {
700  for (k = 0; k <= i; k++) {
701  if (k < i)
702  rt_raster_destroy(rast[k]);
703  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
704  }
705  elog(ERROR, "RASTER_covers: Could not deserialize the %s raster", i < 1 ? "first" : "second");
706  PG_RETURN_NULL();
707  }
708 
709  /* numbands */
710  numBands = rt_raster_get_num_bands(rast[i]);
711  if (numBands < 1) {
712  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
713  if (i > 0) i++;
714  for (k = 0; k < i; k++) {
715  rt_raster_destroy(rast[k]);
716  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
717  }
718  PG_RETURN_NULL();
719  }
720 
721  /* band index */
722  if (!PG_ARGISNULL(j)) {
723  bandindex[i] = PG_GETARG_INT32(j);
724  if (bandindex[i] < 1 || bandindex[i] > numBands) {
725  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
726  if (i > 0) i++;
727  for (k = 0; k < i; k++) {
728  rt_raster_destroy(rast[k]);
729  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
730  }
731  PG_RETURN_NULL();
732  }
733  hasbandindex[i] = 1;
734  }
735  else
736  hasbandindex[i] = 0;
737  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
738  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
739  j++;
740  }
741 
742  /* hasbandindex must be balanced */
743  if (
744  (hasbandindex[0] && !hasbandindex[1]) ||
745  (!hasbandindex[0] && hasbandindex[1])
746  ) {
747  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
748  for (k = 0; k < set_count; k++) {
749  rt_raster_destroy(rast[k]);
750  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
751  }
752  PG_RETURN_NULL();
753  }
754 
755  /* SRID must match */
756  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
757  for (k = 0; k < set_count; k++) {
758  rt_raster_destroy(rast[k]);
759  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
760  }
761  elog(ERROR, "The two rasters provided have different SRIDs");
762  PG_RETURN_NULL();
763  }
764 
765  rtn = rt_raster_covers(
766  rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1),
767  rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1),
768  &result
769  );
770  for (k = 0; k < set_count; k++) {
771  rt_raster_destroy(rast[k]);
772  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
773  }
774 
775  if (rtn != ES_NONE) {
776  elog(ERROR, "RASTER_covers: Could not test that the first raster covers the second raster");
777  PG_RETURN_NULL();
778  }
779 
780  PG_RETURN_BOOL(result);
781 }
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
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.
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
unsigned int uint32_t
Definition: uthash.h:78
int32_t rt_raster_get_srid(rt_raster raster)
Get raster&#39;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
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2201
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:717
Here is the call graph for this function:
Here is the caller graph for this function: