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

◆ test_gdal_polygonize()

static void test_gdal_polygonize ( )
static

Definition at line 155 of file cu_gdal.c.

155 {
156 int i;
158 int nPols = 0;
159 double total_area = 0;
160 double total_val = 0;
161 rt_geomval gv = NULL;
162 LWGEOM *gobserved;
163 //char *wkt = NULL;
164
165 rt = fillRasterToPolygonize(1, -1.0);
166 CU_ASSERT(rt_raster_has_band(rt, 0));
167
168 nPols = 0;
169 gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
170 CU_ASSERT_DOUBLE_EQUAL(nPols, 4, FLT_EPSILON);
171 total_area = 0; total_val = 0;
172 for (i = 0; i < nPols; i++) {
173 total_val += gv[i].val;
174 gobserved = (LWGEOM *) gv[i].geom;
175 total_area += lwgeom_area(gobserved);
176 lwgeom_free((LWGEOM *) gv[i].geom);
177 }
178 printf("total area, total val, nPols = %f, %f, %i\n", total_area, total_val, nPols);
179 CU_ASSERT_DOUBLE_EQUAL(total_val, 1.8 + 0.0 + 2.8 + 0, FLT_EPSILON);
180 CU_ASSERT_DOUBLE_EQUAL(total_area, 81, FLT_EPSILON);
181
182 rtdealloc(gv);
183 cu_free_raster(rt);
184
185 /* Second test: NODATA value = 1.8 */
186 rt = fillRasterToPolygonize(1, 1.8);
187
188 /* We can check rt_raster_has_band here too */
189 CU_ASSERT(rt_raster_has_band(rt, 0));
190
191 nPols = 0;
192 gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
193 CU_ASSERT_DOUBLE_EQUAL(nPols, 4, FLT_EPSILON);
194 total_area = 0; total_val = 0;
195 for (i = 0; i < nPols; i++) {
196 total_val += gv[i].val;
197 gobserved = (LWGEOM *) gv[i].geom;
198 total_area += lwgeom_area(gobserved);
199 lwgeom_free((LWGEOM *) gv[i].geom);
200 }
201 printf("total area, total_val, polys = %f, %f, %i\n", total_area, total_val, nPols);
202 CU_ASSERT_DOUBLE_EQUAL(total_val, 4.6, FLT_EPSILON);
203 CU_ASSERT_DOUBLE_EQUAL(total_area, 81, FLT_EPSILON);
204
205
206 rtdealloc(gv);
207 cu_free_raster(rt);
208
209 /* Third test: NODATA value = 2.8 */
210 rt = fillRasterToPolygonize(1, 2.8);
211
212 /* We can check rt_raster_has_band here too */
213 CU_ASSERT(rt_raster_has_band(rt, 0));
214
215 nPols = 0;
216 gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
217 CU_ASSERT_DOUBLE_EQUAL(nPols, 4, FLT_EPSILON);
218 total_area = 0; total_val = 0;
219 for (i = 0; i < nPols; i++) {
220 total_val += gv[i].val;
221 gobserved = (LWGEOM *) gv[i].geom;
222 total_area += lwgeom_area(gobserved);
223 lwgeom_free((LWGEOM *) gv[i].geom);
224 }
225
226 printf("total area, total_val, polys = %f, %f, %i\n", total_area, total_val, nPols);
227 CU_ASSERT_DOUBLE_EQUAL(total_val, 4.6, FLT_EPSILON);
228 CU_ASSERT_DOUBLE_EQUAL(total_area, 81, FLT_EPSILON);
229
230 rtdealloc(gv);
231 cu_free_raster(rt);
232
233 /* Fourth test: NODATA value = 0 */
234 rt = fillRasterToPolygonize(1, 0.0);
235 /* We can check rt_raster_has_band here too */
236 CU_ASSERT(rt_raster_has_band(rt, 0));
237
238 nPols = 0;
239 gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
240
241 CU_ASSERT_DOUBLE_EQUAL(nPols, 2, FLT_EPSILON);
242 total_area = 0; total_val = 0;
243 for (i = 0; i < nPols; i++) {
244 total_val += gv[i].val;
245 gobserved = (LWGEOM *) gv[i].geom;
246 total_area += lwgeom_area(gobserved);
247 lwgeom_free((LWGEOM *) gv[i].geom);
248 }
249
250 printf("total area, total_val, polys = %f, %f, %i\n", total_area, total_val, nPols);
251 CU_ASSERT_DOUBLE_EQUAL(total_val, 4.6, FLT_EPSILON);
252 CU_ASSERT_DOUBLE_EQUAL(total_area, 28, FLT_EPSILON);
253
254 rtdealloc(gv);
255 cu_free_raster(rt);
256
257 /* Last test: There is no NODATA value (all values are valid) */
258 rt = fillRasterToPolygonize(0, 0.0);
259 /* We can check rt_raster_has_band here too */
260 CU_ASSERT(rt_raster_has_band(rt, 0));
261
262 nPols = 0;
263 gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
264
265 CU_ASSERT_DOUBLE_EQUAL(nPols, 4, FLT_EPSILON);
266 total_area = 0; total_val = 0;
267 for (i = 0; i < nPols; i++) {
268 total_val += gv[i].val;
269 gobserved = (LWGEOM *) gv[i].geom;
270 total_area += lwgeom_area(gobserved);
271 lwgeom_free((LWGEOM *) gv[i].geom);
272 }
273
274 printf("total area, total_val, polys = %f, %f, %i\n", total_area, total_val, nPols);
275 CU_ASSERT_DOUBLE_EQUAL(total_val, 1.8 + 0.0 + 2.8 + 0.0, FLT_EPSILON);
276 CU_ASSERT_DOUBLE_EQUAL(total_area, 81, FLT_EPSILON);
277 rtdealloc(gv);
278 cu_free_raster(rt);
279}
static rt_raster fillRasterToPolygonize(int hasnodata, double nodataval)
Definition cu_gdal.c:102
#define TRUE
Definition dbfopen.c:73
void lwgeom_free(LWGEOM *geom)
Definition lwgeom.c:1246
double lwgeom_area(const LWGEOM *geom)
Definition lwgeom.c:1999
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
Definition rt_raster.c:1253
rt_geomval rt_raster_gdal_polygonize(rt_raster raster, int nband, int exclude_nodata_value, int *pnElements)
Returns a set of "geomval" value, one for each group of pixel sharing the same value for the provided...
void rtdealloc(void *mem)
Definition rt_context.c:206
void cu_free_raster(rt_raster raster)

References cu_free_raster(), fillRasterToPolygonize(), lwgeom_area(), lwgeom_free(), rt_raster_gdal_polygonize(), rt_raster_has_band(), rtdealloc(), TRUE, and rt_geomval_t::val.

Referenced by gdal_suite_setup().

Here is the call graph for this function:
Here is the caller graph for this function: