PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_intersects()

Datum RASTER_intersects ( PG_FUNCTION_ARGS  )

Definition at line 73 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_overlaps(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_num_bands(), rt_raster_get_srid(), and rt_raster_intersects().

74 {
75  const int set_count = 2;
76  rt_pgraster *pgrast[2];
77  int pgrastpos[2] = {-1, -1};
78  rt_raster rast[2] = {NULL};
79  uint32_t bandindex[2] = {0};
80  uint32_t hasbandindex[2] = {0};
81 
82  uint32_t i;
83  uint32_t j;
84  uint32_t k;
86  int rtn;
87  int result;
88 
89  for (i = 0, j = 0; i < set_count; i++) {
90  /* pgrast is null, return null */
91  if (PG_ARGISNULL(j)) {
92  for (k = 0; k < i; k++) {
93  rt_raster_destroy(rast[k]);
94  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
95  }
96  PG_RETURN_NULL();
97  }
98  pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j));
99  pgrastpos[i] = j;
100  j++;
101 
102  /* raster */
103  rast[i] = rt_raster_deserialize(pgrast[i], FALSE);
104  if (!rast[i]) {
105  for (k = 0; k <= i; k++) {
106  if (k < i)
107  rt_raster_destroy(rast[k]);
108  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
109  }
110  elog(ERROR, "RASTER_intersects: Could not deserialize the %s raster", i < 1 ? "first" : "second");
111  PG_RETURN_NULL();
112  }
113 
114  /* numbands */
115  numBands = rt_raster_get_num_bands(rast[i]);
116  if (numBands < 1) {
117  elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second");
118  if (i > 0) i++;
119  for (k = 0; k < i; k++) {
120  rt_raster_destroy(rast[k]);
121  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
122  }
123  PG_RETURN_NULL();
124  }
125 
126  /* band index */
127  if (!PG_ARGISNULL(j)) {
128  bandindex[i] = PG_GETARG_INT32(j);
129  if (bandindex[i] < 1 || bandindex[i] > numBands) {
130  elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second");
131  if (i > 0) i++;
132  for (k = 0; k < i; k++) {
133  rt_raster_destroy(rast[k]);
134  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
135  }
136  PG_RETURN_NULL();
137  }
138  hasbandindex[i] = 1;
139  }
140  else
141  hasbandindex[i] = 0;
142  POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]);
143  POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]);
144  j++;
145  }
146 
147  /* hasbandindex must be balanced */
148  if (
149  (hasbandindex[0] && !hasbandindex[1]) ||
150  (!hasbandindex[0] && hasbandindex[1])
151  ) {
152  elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided");
153  for (k = 0; k < set_count; k++) {
154  rt_raster_destroy(rast[k]);
155  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
156  }
157  PG_RETURN_NULL();
158  }
159 
160  /* SRID must match */
161  if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) {
162  for (k = 0; k < set_count; k++) {
163  rt_raster_destroy(rast[k]);
164  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
165  }
166  elog(ERROR, "The two rasters provided have different SRIDs");
167  PG_RETURN_NULL();
168  }
169 
170  rtn = rt_raster_intersects(
171  rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1),
172  rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1),
173  &result
174  );
175  for (k = 0; k < set_count; k++) {
176  rt_raster_destroy(rast[k]);
177  PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]);
178  }
179 
180  if (rtn != ES_NONE) {
181  elog(ERROR, "RASTER_intersects: Could not test for intersection on the two rasters");
182  PG_RETURN_NULL();
183  }
184 
185  PG_RETURN_BOOL(result);
186 }
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
rt_errorstate rt_raster_intersects(rt_raster rast1, int nband1, rt_raster rast2, int nband2, int *intersects)
Return ES_ERROR if error occurred in function.
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: