PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ RASTER_intersects()

Datum RASTER_intersects ( PG_FUNCTION_ARGS  )

Definition at line 78 of file rtpg_spatial_relationship.c.

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

References ES_NONE, FALSE, POSTGIS_RT_DEBUGF, result, 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: