PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ test_gdal_polygonize()

static void test_gdal_polygonize ( )
static

Definition at line 154 of file cu_gdal.c.

154  {
155  int i;
156  rt_raster rt;
157  int nPols = 0;
158  rt_geomval gv = NULL;
159  LWGEOM *gexpected, *gobserved;
160  gexpected = lwgeom_from_wkt("POLYGON((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))",
162 
163  rt = fillRasterToPolygonize(1, -1.0);
164  CU_ASSERT(rt_raster_has_band(rt, 0));
165 
166  nPols = 0;
167  gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
168 
169  CU_ASSERT_DOUBLE_EQUAL(gv[0].val, 1.8, FLT_EPSILON);
170 
171  gobserved = (LWGEOM *)gv[0].geom;
172 
173  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
174 
175  CU_ASSERT_DOUBLE_EQUAL(gv[1].val, 0.0, FLT_EPSILON);
176  gobserved = (LWGEOM *)gv[1].geom;
177  gexpected = lwgeom_from_wkt("POLYGON((3 3,3 6,6 6,6 3,3 3))", LW_PARSER_CHECK_NONE);
178  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON );
179 
180  CU_ASSERT_DOUBLE_EQUAL(gv[2].val, 2.8, FLT_EPSILON);
181  gobserved = (LWGEOM *)gv[2].geom;
182  gexpected = lwgeom_from_wkt("POLYGON((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))",
184  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
185 
186  gobserved = (LWGEOM *)gv[3].geom;
187  gexpected = lwgeom_from_wkt(
188  "POLYGON((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))",
190  CU_ASSERT_DOUBLE_EQUAL(gv[3].val, 0.0, FLT_EPSILON);
191  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
192 
193  for (i = 0; i < nPols; i++) lwgeom_free((LWGEOM *) gv[i].geom);
194  rtdealloc(gv);
196 
197  /* Second test: NODATA value = 1.8 */
198  rt = fillRasterToPolygonize(1, 1.8);
199 
200  /* We can check rt_raster_has_band here too */
201  CU_ASSERT(rt_raster_has_band(rt, 0));
202 
203  nPols = 0;
204  gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
205 
206  /*
207  for (i = 0; i < nPols; i++) {
208  wkt = lwgeom_to_text((const LWGEOM *) gv[i].geom);
209  printf("(i, val, geom) = (%d, %f, %s)\n", i, gv[i].val, wkt);
210  rtdealloc(wkt);
211  }
212  */
213 
214  CU_ASSERT_DOUBLE_EQUAL(gv[1].val, 0.0, FLT_EPSILON);
215  gobserved = (LWGEOM *)gv[1].geom;
216  gexpected = lwgeom_from_wkt("POLYGON((3 3,3 6,6 6,6 3,3 3))", LW_PARSER_CHECK_NONE);
217  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
218  //wkt = lwgeom_to_text((const LWGEOM *) gv[1].geom);
219  //CU_ASSERT_STRING_EQUAL(wkt, "POLYGON((3 3,3 6,6 6,6 3,3 3))");
220  //rtdealloc(wkt);
221 
222  CU_ASSERT_DOUBLE_EQUAL(gv[2].val, 2.8, FLT_EPSILON);
223  gobserved = (LWGEOM *)gv[2].geom;
224  gexpected = lwgeom_from_wkt("POLYGON((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))",
226  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
227  //wkt = lwgeom_to_text((const LWGEOM *) gv[2].geom);
228  //CU_ASSERT_STRING_EQUAL(wkt, "POLYGON((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))");
229  //rtdealloc(wkt);
230 
231  CU_ASSERT_DOUBLE_EQUAL(gv[3].val, 0.0, FLT_EPSILON);
232  gobserved = (LWGEOM *)gv[3].geom;
233  gexpected = lwgeom_from_wkt(
234  "POLYGON((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))",
236  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
237 
238  for (i = 0; i < nPols; i++) lwgeom_free((LWGEOM *) gv[i].geom);
239  rtdealloc(gv);
241 
242  /* Third test: NODATA value = 2.8 */
243  rt = fillRasterToPolygonize(1, 2.8);
244 
245  /* We can check rt_raster_has_band here too */
246  CU_ASSERT(rt_raster_has_band(rt, 0));
247 
248  nPols = 0;
249  gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
250 
251  /*
252  for (i = 0; i < nPols; i++) {
253  wkt = lwgeom_to_text((const LWGEOM *) gv[i].geom);
254  printf("(i, val, geom) = (%d, %f, %s)\n", i, gv[i].val, wkt);
255  rtdealloc(wkt);
256  }
257  */
258 
259  CU_ASSERT_DOUBLE_EQUAL(gv[3].val, 0.0, FLT_EPSILON);
260  gobserved = (LWGEOM *)gv[3].geom;
261  gexpected = lwgeom_from_wkt(
262  "POLYGON((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))",
264  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
265 
266  gobserved = (LWGEOM *)gv[0].geom;
267  gexpected = lwgeom_from_wkt(
268  "POLYGON((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))", LW_PARSER_CHECK_NONE);
269  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
270 
271  CU_ASSERT_DOUBLE_EQUAL(gv[1].val, 0.0, FLT_EPSILON);
272  gobserved = (LWGEOM *)gv[1].geom;
273  gexpected = lwgeom_from_wkt("POLYGON((3 3,3 6,6 6,6 3,3 3))", LW_PARSER_CHECK_NONE);
274  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
275 
276  for (i = 0; i < nPols; i++) lwgeom_free((LWGEOM *) gv[i].geom);
277  rtdealloc(gv);
279 
280  /* Fourth test: NODATA value = 0 */
281  rt = fillRasterToPolygonize(1, 0.0);
282  /* We can check rt_raster_has_band here too */
283  CU_ASSERT(rt_raster_has_band(rt, 0));
284 
285  nPols = 0;
286  gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
287 
288  /*
289  for (i = 0; i < nPols; i++) {
290  wkt = lwgeom_to_text((const LWGEOM *) gv[i].geom);
291  printf("(i, val, geom) = (%d, %f, %s)\n", i, gv[i].val, wkt);
292  rtdealloc(wkt);
293  }
294  */
295 
296  CU_ASSERT_DOUBLE_EQUAL(gv[0].val, 1.8, FLT_EPSILON);
297  gobserved = (LWGEOM *)gv[0].geom;
298  gexpected = lwgeom_from_wkt("POLYGON((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))", LW_PARSER_CHECK_NONE);
299  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
300 
301  CU_ASSERT_DOUBLE_EQUAL(gv[1].val, 2.8, FLT_EPSILON);
302  gobserved = (LWGEOM *)gv[1].geom;
303  gexpected = lwgeom_from_wkt("POLYGON((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))",
305  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
306 
307  for (i = 0; i < nPols; i++) lwgeom_free((LWGEOM *) gv[i].geom);
308  rtdealloc(gv);
310 
311  /* Last test: There is no NODATA value (all values are valid) */
312  rt = fillRasterToPolygonize(0, 0.0);
313  /* We can check rt_raster_has_band here too */
314  CU_ASSERT(rt_raster_has_band(rt, 0));
315 
316  nPols = 0;
317  gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
318 
319  /*
320  for (i = 0; i < nPols; i++) {
321  wkt = lwgeom_to_text((const LWGEOM *) gv[i].geom);
322  printf("(i, val, geom) = (%d, %f, %s)\n", i, gv[i].val, wkt);
323  rtdealloc(wkt);
324  }
325  */
326 
327  CU_ASSERT_DOUBLE_EQUAL(gv[0].val, 1.8, FLT_EPSILON);
328  gobserved = (LWGEOM *)gv[0].geom;
329  gexpected = lwgeom_from_wkt(
330  "POLYGON((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))", LW_PARSER_CHECK_NONE);
331  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
332 
333  CU_ASSERT_DOUBLE_EQUAL(gv[1].val, 0.0, FLT_EPSILON);
334  gobserved = (LWGEOM *)gv[1].geom;
335  gexpected = lwgeom_from_wkt("POLYGON((3 3,3 6,6 6,6 3,3 3))", LW_PARSER_CHECK_NONE);
336  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
337 
338  CU_ASSERT_DOUBLE_EQUAL(gv[2].val, 2.8, FLT_EPSILON);
339  gobserved = (LWGEOM *)gv[2].geom;
340  gexpected = lwgeom_from_wkt("POLYGON((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))",
342  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
343 
344  CU_ASSERT_DOUBLE_EQUAL(gv[3].val, 0.0, FLT_EPSILON);
345  gobserved = (LWGEOM *)gv[3].geom;
346  gexpected = lwgeom_from_wkt(
347  "POLYGON((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))",
349  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
350 
351  for (i = 0; i < nPols; i++) lwgeom_free((LWGEOM *) gv[i].geom);
352  rtdealloc(gv);
354 }
static rt_raster fillRasterToPolygonize(int hasnodata, double nodataval)
Definition: cu_gdal.c:101
#define TRUE
Definition: dbfopen.c:169
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2005
double lwgeom_area(const LWGEOM *geom)
Definition: lwgeom.c:1872
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
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:1347
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...
Definition: rt_geometry.c:940
void rtdealloc(void *mem)
Definition: rt_context.c:186
void cu_free_raster(rt_raster raster)

References cu_free_raster(), fillRasterToPolygonize(), LW_PARSER_CHECK_NONE, lwgeom_area(), lwgeom_free(), lwgeom_from_wkt(), rtrowdump::rt, rt_raster_gdal_polygonize(), rt_raster_has_band(), rtdealloc(), and TRUE.

Referenced by gdal_suite_setup().

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