PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_dwithin()

Datum RASTER_dwithin ( PG_FUNCTION_ARGS  )

Definition at line 906 of file rtpg_spatial_relationship.c.

References distance(), ES_NONE, FALSE, rt_raster_serialized_t::numBands, PG_FUNCTION_INFO_V1(), POSTGIS_RT_DEBUGF, rtpixdump::rast, RASTER_dfullywithin(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_num_bands(), rt_raster_get_srid(), and rt_raster_within_distance().

Referenced by RASTER_coveredby().

907 {
908  const int set_count = 2;
909  rt_pgraster *pgrast[2];
910  int pgrastpos[2] = {-1, -1};
911  rt_raster rast[2] = {NULL};
912  uint32_t bandindex[2] = {0};
913  uint32_t hasbandindex[2] = {0};
914  double distance = 0;
915 
916  uint32_t i;
917  uint32_t j;
918  uint32_t k;
920  int rtn;
921  int result;
922 
923  for (i = 0, j = 0; i < set_count; i++) {
924  /* pgrast is null, return null */
925  if (PG_ARGISNULL(j)) {
926  for (k = 0; k < i; k++) {
927  rt_raster_destroy(rast[k]);
928  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
929  }
930  PG_RETURN_NULL();
931  }
932  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
933  pgrastpos[i] = j;
934  j++;
935 
936  /* raster */
937  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
938  if (!rast[i]) {
939  for (k = 0; k <= i; k++) {
940  if (k < i)
941  rt_raster_destroy(rast[k]);
942  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
943  }
944  elog(ERROR, "RASTER_dwithin: Could not deserialize the %s raster", i < 1 ? "first" : "second");
945  PG_RETURN_NULL();
946  }
947 
948  /* numbands */
949  numBands = rt_raster_get_num_bands(rast[i]);
950  if (numBands < 1) {
951  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
952  if (i > 0) i++;
953  for (k = 0; k < i; k++) {
954  rt_raster_destroy(rast[k]);
955  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
956  }
957  PG_RETURN_NULL();
958  }
959 
960  /* band index */
961  if (!PG_ARGISNULL(j)) {
962  bandindex[i] = PG_GETARG_INT32(j);
963  if (bandindex[i] < 1 || bandindex[i] > numBands) {
964  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
965  if (i > 0) i++;
966  for (k = 0; k < i; k++) {
967  rt_raster_destroy(rast[k]);
968  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
969  }
970  PG_RETURN_NULL();
971  }
972  hasbandindex[i] = 1;
973  }
974  else
975  hasbandindex[i] = 0;
976  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
977  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
978  j++;
979  }
980 
981  /* distance */
982  if (PG_ARGISNULL(4)) {
983  elog(NOTICE, "Distance cannot be NULL. Returning NULL");
984  for (k = 0; k < set_count; k++) {
985  rt_raster_destroy(rast[k]);
986  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
987  }
988  PG_RETURN_NULL();
989  }
990 
991  distance = PG_GETARG_FLOAT8(4);
992  if (distance < 0) {
993  elog(NOTICE, "Distance cannot be less than zero. Returning NULL");
994  for (k = 0; k < set_count; k++) {
995  rt_raster_destroy(rast[k]);
996  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
997  }
998  PG_RETURN_NULL();
999  }
1000 
1001  /* hasbandindex must be balanced */
1002  if (
1003  (hasbandindex[0] && !hasbandindex[1]) ||
1004  (!hasbandindex[0] && hasbandindex[1])
1005  ) {
1006  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
1007  for (k = 0; k < set_count; k++) {
1008  rt_raster_destroy(rast[k]);
1009  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1010  }
1011  PG_RETURN_NULL();
1012  }
1013 
1014  /* SRID must match */
1015  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
1016  for (k = 0; k < set_count; k++) {
1017  rt_raster_destroy(rast[k]);
1018  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1019  }
1020  elog(ERROR, "The two rasters provided have different SRIDs");
1021  PG_RETURN_NULL();
1022  }
1023 
1025  rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1),
1026  rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1),
1027  distance,
1028  &result
1029  );
1030  for (k = 0; k < set_count; k++) {
1031  rt_raster_destroy(rast[k]);
1032  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
1033  }
1034 
1035  if (rtn != ES_NONE) {
1036  elog(ERROR, "RASTER_dwithin: Could not test that the two rasters are within the specified distance of each other");
1037  PG_RETURN_NULL();
1038  }
1039 
1040  PG_RETURN_BOOL(result);
1041 }
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
#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
Datum distance(PG_FUNCTION_ARGS)
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2201
rt_errorstate rt_raster_within_distance(rt_raster rast1, int nband1, rt_raster rast2, int nband2, double distance, int *dwithin)
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:
Here is the caller graph for this function: