PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ RASTER_intersects()

Datum RASTER_intersects ( PG_FUNCTION_ARGS  )

Definition at line 74 of file rtpg_spatial_relationship.c.

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

Here is the call graph for this function: