PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ RASTER_dwithin()

Datum RASTER_dwithin ( PG_FUNCTION_ARGS  )

Definition at line 907 of file rtpg_spatial_relationship.c.

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

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

Here is the call graph for this function: