PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum RASTER_coveredby ( PG_FUNCTION_ARGS  )

Definition at line 787 of file rtpg_spatial_relationship.c.

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

788 {
789  const int set_count = 2;
790  rt_pgraster *pgrast[2];
791  int pgrastpos[2] = {-1, -1};
792  rt_raster rast[2] = {NULL};
793  uint32_t bandindex[2] = {0};
794  uint32_t hasbandindex[2] = {0};
795 
796  uint32_t i;
797  uint32_t j;
798  uint32_t k;
799  uint32_t numBands;
800  int rtn;
801  int result;
802 
803  for (i = 0, j = 0; i < set_count; i++) {
804  /* pgrast is null, return null */
805  if (PG_ARGISNULL(j)) {
806  for (k = 0; k < i; k++) {
807  rt_raster_destroy(rast[k]);
808  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
809  }
810  PG_RETURN_NULL();
811  }
812  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
813  pgrastpos[i] = j;
814  j++;
815 
816  /* raster */
817  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
818  if (!rast[i]) {
819  for (k = 0; k <= i; k++) {
820  if (k < i)
821  rt_raster_destroy(rast[k]);
822  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
823  }
824  elog(ERROR, "RASTER_coveredby: Could not deserialize the %s raster", i < 1 ? "first" : "second");
825  PG_RETURN_NULL();
826  }
827 
828  /* numbands */
829  numBands = rt_raster_get_num_bands(rast[i]);
830  if (numBands < 1) {
831  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
832  if (i > 0) i++;
833  for (k = 0; k < i; k++) {
834  rt_raster_destroy(rast[k]);
835  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
836  }
837  PG_RETURN_NULL();
838  }
839 
840  /* band index */
841  if (!PG_ARGISNULL(j)) {
842  bandindex[i] = PG_GETARG_INT32(j);
843  if (bandindex[i] < 1 || bandindex[i] > numBands) {
844  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
845  if (i > 0) i++;
846  for (k = 0; k < i; k++) {
847  rt_raster_destroy(rast[k]);
848  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
849  }
850  PG_RETURN_NULL();
851  }
852  hasbandindex[i] = 1;
853  }
854  else
855  hasbandindex[i] = 0;
856  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
857  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
858  j++;
859  }
860 
861  /* hasbandindex must be balanced */
862  if (
863  (hasbandindex[0] && !hasbandindex[1]) ||
864  (!hasbandindex[0] && hasbandindex[1])
865  ) {
866  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
867  for (k = 0; k < set_count; k++) {
868  rt_raster_destroy(rast[k]);
869  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
870  }
871  PG_RETURN_NULL();
872  }
873 
874  /* SRID must match */
875  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
876  for (k = 0; k < set_count; k++) {
877  rt_raster_destroy(rast[k]);
878  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
879  }
880  elog(ERROR, "The two rasters provided have different SRIDs");
881  PG_RETURN_NULL();
882  }
883 
884  rtn = rt_raster_coveredby(
885  rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1),
886  rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1),
887  &result
888  );
889  for (k = 0; k < set_count; k++) {
890  rt_raster_destroy(rast[k]);
891  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
892  }
893 
894  if (rtn != ES_NONE) {
895  elog(ERROR, "RASTER_coveredby: Could not test that the first raster is covered by the second raster");
896  PG_RETURN_NULL();
897  }
898 
899  PG_RETURN_BOOL(result);
900 }
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
tuple rast
Definition: rtpixdump.py:61
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:57
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
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2213
rt_errorstate rt_raster_coveredby(rt_raster rast1, int nband1, rt_raster rast2, int nband2, int *coveredby)
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:717

Here is the call graph for this function: