PostGIS  2.5.1dev-r@@SVN_REVISION@@
cu_buildarea.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * Copyright (C) 2012 Sandro Santilli <strk@kbt.io>
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU General Public Licence. See the COPYING file.
10  *
11  **********************************************************************/
12 
13 #include "CUnit/Basic.h"
14 #include "cu_tester.h"
15 
16 #include "liblwgeom.h"
17 #include "liblwgeom_internal.h"
18 
19 /*
20  * TODO: change lwgeom_same to lwgeom_equals
21  * (requires porting predicates to liblwgeom)
22  */
23 #define check_geom_equal(gobt, gexp) do { \
24  char *obt, *exp; \
25  LWGEOM *ngobt, *ngexp; \
26  ngobt = lwgeom_normalize(gobt); \
27  ngexp = lwgeom_normalize(gexp); \
28  if ( ! lwgeom_same((ngobt), (ngexp)) ) { \
29  obt = lwgeom_to_wkt((ngobt), WKT_ISO, 8, NULL); \
30  exp = lwgeom_to_wkt((ngexp), WKT_ISO, 8, NULL); \
31  printf(" Failure at %s:%d\n", __FILE__, __LINE__); \
32  printf(" Exp: %s\n", exp); \
33  printf(" Obt: %s\n", obt); \
34  free(obt); free(exp); \
35  lwgeom_free(ngobt); lwgeom_free(ngexp); \
36  CU_ASSERT(0); \
37  } else { \
38  lwgeom_free(ngobt); lwgeom_free(ngexp); \
39  CU_ASSERT(1); \
40  } \
41 } while (0)
42 
43 /*
44  +-----+
45  | |
46  +-----+-----+
47  | |
48  +-----+
49 */
50 static void buildarea1(void)
51 {
52  LWGEOM *gin, *gout, *gexp;
53 
55 
56  gin = lwgeom_from_wkt(
57 "MULTILINESTRING((0 0, 10 0, 10 10, 0 10, 0 0),(10 10, 20 10, 20 20, 10 20, 10 10))",
59  CU_ASSERT( gin != NULL );
60 
61  gexp = lwgeom_from_wkt(
62 "MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0)),((10 10,10 20,20 20,20 10,10 10)))",
64  CU_ASSERT( gexp != NULL );
65 
66  gout = lwgeom_buildarea(gin);
67  CU_ASSERT(gout != NULL);
68 
69  check_geom_equal(gout, gexp);
70 
71  lwgeom_free(gout);
72  lwgeom_free(gexp);
73  lwgeom_free(gin);
74 }
75 
76 /*
77  +-----+-----+
78  | | |
79  +-----+-----+
80 */
81 static void buildarea2(void)
82 {
83  LWGEOM *gin, *gout, *gexp;
84 
85  /* because i don't trust that much prior tests... ;) */
87 
88  gin = lwgeom_from_wkt(
89 "MULTILINESTRING((0 0, 10 0, 10 10, 0 10, 0 0),(10 10, 20 10, 20 0, 10 0, 10 10))",
91  CU_ASSERT(gin != NULL);
92 
93  gexp = lwgeom_from_wkt(
94 "POLYGON((0 0,0 10,10 10,20 10,20 0,10 0,0 0))",
96  CU_ASSERT(gexp != NULL);
97 
98  gout = lwgeom_buildarea(gin);
99  CU_ASSERT(gout != NULL);
100 
101  check_geom_equal(gout, gexp);
102 
103  lwgeom_free(gout);
104  lwgeom_free(gexp);
105  lwgeom_free(gin);
106 
107 }
108 
109 /*
110  +-----------+
111  | +-----+ |
112  | | | |
113  | +-----+ |
114  +-----------+
115 */
116 static void buildarea3(void)
117 {
118  LWGEOM *gin, *gout, *gexp;
119 
121 
122  gin = lwgeom_from_wkt(
123 "MULTILINESTRING((0 0, 20 0, 20 20, 0 20, 0 0),(2 2, 18 2, 18 18, 2 18, 2 2))",
125  CU_ASSERT(gin != NULL);
126 
127  gexp = lwgeom_from_wkt(
128 "POLYGON((0 0,0 20,20 20,20 0,0 0),(2 2,18 2,18 18,2 18,2 2))",
130  CU_ASSERT(gexp != NULL);
131 
132  gout = lwgeom_buildarea(gin);
133  CU_ASSERT(gout != NULL);
134 
135  check_geom_equal(gout, gexp);
136 
137  lwgeom_free(gout);
138  lwgeom_free(gexp);
139  lwgeom_free(gin);
140 
141 }
142 
143 /*
144  +-----------+
145  | +-----+ |
146  | | +-+ | |
147  | | | | | |
148  | | +-+ | |
149  | +-----+ |
150  +-----------+
151 */
152 static void buildarea4(void)
153 {
154  LWGEOM *gin, *gout, *gexp;
155 
157 
158  gin = lwgeom_from_wkt(
159 "MULTILINESTRING((0 0, 20 0, 20 20, 0 20, 0 0),(2 2, 18 2, 18 18, 2 18, 2 2),(8 8, 8 12, 12 12, 12 8, 8 8))",
161  CU_ASSERT(gin != NULL);
162 
163  gexp = lwgeom_from_wkt(
164 "MULTIPOLYGON(((0 0,0 20,20 20,20 0,0 0),(2 2,18 2,18 18,2 18,2 2)),((8 8,8 12,12 12,12 8,8 8)))",
166  CU_ASSERT(gexp != NULL);
167 
168  gout = lwgeom_buildarea(gin);
169  CU_ASSERT(gout != NULL);
170 
171  check_geom_equal(gout, gexp);
172 
173  lwgeom_free(gout);
174  lwgeom_free(gexp);
175  lwgeom_free(gin);
176 
177 }
178 
179 /*
180  +-----------+
181  | +-----+ | This time the innermost ring has
182  | | +-+ | | more points than the other (outer) two.
183  | | | | | |
184  | | +-+ | |
185  | +-----+ |
186  +-----------+
187 */
188 static void buildarea4b(void)
189 {
190  LWGEOM *gin, *gout, *gexp;
191 
193 
194  gin = lwgeom_from_wkt(
195 "MULTILINESTRING((0 0, 20 0, 20 20, 0 20, 0 0),(2 2, 18 2, 18 18, 2 18, 2 2), (8 8, 8 9, 8 10, 8 11, 8 12, 9 12, 10 12, 11 12, 12 12, 12 11, 12 10, 12 9, 12 8, 11 8, 10 8, 9 8, 8 8))",
197  CU_ASSERT(gin != NULL);
198 
199  gexp = lwgeom_from_wkt(
200 "MULTIPOLYGON(((0 0,0 20,20 20,20 0,0 0),(2 2,18 2,18 18,2 18,2 2)),((8 8,8 9,8 10,8 11,8 12,9 12,10 12,11 12,12 12,12 11,12 10,12 9,12 8,11 8,10 8,9 8,8 8)))",
202  CU_ASSERT(gexp != NULL);
203 
204  gout = lwgeom_buildarea(gin);
205  CU_ASSERT(gout != NULL);
206 
207  check_geom_equal(gout, gexp);
208 
209  lwgeom_free(gout);
210  lwgeom_free(gexp);
211  lwgeom_free(gin);
212 
213 }
214 
215 /*
216  +---------------+
217  | +---------+ |
218  | | +--+--+ | |
219  | | | | | | |
220  | | +--+--+ | |
221  | +---------+ |
222  +---------------+
223 */
224 static void buildarea5(void)
225 {
226  LWGEOM *gin, *gout, *gexp;
227 
229 
230  gin = lwgeom_from_wkt(
231 "MULTILINESTRING((0 0, 20 0, 20 20, 0 20, 0 0),(2 2, 18 2, 18 18, 2 18, 2 2),(8 8, 8 12, 12 12, 12 8, 8 8),(10 8, 10 12))",
233  CU_ASSERT(gin != NULL);
234 
235  gexp = lwgeom_from_wkt(
236 "MULTIPOLYGON(((0 0,0 20,20 20,20 0,0 0),(2 2,18 2,18 18,2 18,2 2)),((8 8,8 12,12 12,12 8,8 8)))",
238  CU_ASSERT(gexp != NULL);
239 
240  gout = lwgeom_buildarea(gin);
241  CU_ASSERT(gout != NULL);
242 
243  check_geom_equal(gout, gexp);
244 
245  lwgeom_free(gout);
246  lwgeom_free(gexp);
247  lwgeom_free(gin);
248 
249 }
250 
251 /*
252  +---------------+
253  | +----+----+ |
254  | | | | |
255  | | | | |
256  | | | | |
257  | +----+----+ |
258  +---------------+
259 */
260 static void buildarea6(void)
261 {
262  LWGEOM *gin, *gout, *gexp;
263 
265 
266  gin = lwgeom_from_wkt(
267 "MULTILINESTRING((0 0, 20 0, 20 20, 0 20, 0 0),(2 2, 18 2, 18 18, 2 18, 2 2),(10 2, 10 18))",
269  CU_ASSERT(gin != NULL);
270 
271  gexp = lwgeom_from_wkt(
272 "POLYGON((0 0,0 20,20 20,20 0,0 0),(2 2,18 2,18 18,2 18,2 2))",
274  CU_ASSERT(gexp != NULL);
275 
276  gout = lwgeom_buildarea(gin);
277  CU_ASSERT(gout != NULL);
278 
279  check_geom_equal(gout, gexp);
280 
281  lwgeom_free(gout);
282  lwgeom_free(gexp);
283  lwgeom_free(gin);
284 
285 }
286 
287 /*
288  +--------------------+ +-------+
289  | +-----+ +----+ | | +---+ |
290  | | +-+ | | | | | | | |
291  | | | | | +----+ | | +---+ |
292  | | +-+ | | | | | |
293  | | | | | | | | | |
294  | | +-+ | | | | | |
295  | +-----+ +----+ | | |
296  +--------------------+ +-------+
297 */
298 static void buildarea7(void)
299 {
300  LWGEOM *gin, *gout, *gexp;
301 
303 
304  gin = lwgeom_from_wkt(
305 "MULTILINESTRING( (0 0, 70 0, 70 70, 0 70, 0 0), (10 10, 10 60, 40 60, 40 10, 10 10), (20 20, 20 30, 30 30, 30 20, 20 20), (20 30, 30 30, 30 50, 20 50, 20 30), (50 20, 60 20, 60 40, 50 40, 50 20), (50 40, 60 40, 60 60, 50 60, 50 40), (80 0, 110 0, 110 70, 80 70, 80 0), (90 60, 100 60, 100 50, 90 50, 90 60))",
307  CU_ASSERT(gin != NULL);
308 
309  gexp = lwgeom_from_wkt(
310 "MULTIPOLYGON(((80 0,80 70,110 70,110 0,80 0),(90 60,90 50,100 50,100 60,90 60)),((20 20,20 30,20 50,30 50,30 30,30 20,20 20)),((0 0,0 70,70 70,70 0,0 0),(10 10,40 10,40 60,10 60,10 10),(50 20,60 20,60 40,60 60,50 60,50 40,50 20)))",
312  CU_ASSERT(gexp != NULL);
313 
314  gout = lwgeom_buildarea(gin);
315  CU_ASSERT(gout != NULL);
316 
317  check_geom_equal(gout, gexp);
318 
319  lwgeom_free(gout);
320  lwgeom_free(gexp);
321  lwgeom_free(gin);
322 
323 }
324 
325 
326 void buildarea_suite_setup(void);
328 {
329  CU_pSuite suite = CU_add_suite("buildarea", NULL, NULL);
330  PG_ADD_TEST(suite,buildarea1);
331  PG_ADD_TEST(suite,buildarea2);
332  PG_ADD_TEST(suite,buildarea3);
333  PG_ADD_TEST(suite,buildarea4);
334  PG_ADD_TEST(suite,buildarea4b);
335  PG_ADD_TEST(suite,buildarea5);
336  PG_ADD_TEST(suite,buildarea6);
337  PG_ADD_TEST(suite,buildarea7);
338 }
static void buildarea4(void)
Definition: cu_buildarea.c:152
static void buildarea6(void)
Definition: cu_buildarea.c:260
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
static void buildarea4b(void)
Definition: cu_buildarea.c:188
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
static void buildarea3(void)
Definition: cu_buildarea.c:116
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2004
void cu_error_msg_reset()
#define PG_ADD_TEST(suite, testfunc)
static void buildarea1(void)
Definition: cu_buildarea.c:50
void buildarea_suite_setup(void)
Definition: cu_buildarea.c:327
LWGEOM * lwgeom_buildarea(const LWGEOM *geom)
Take a geometry and return an areal geometry (Polygon or MultiPolygon).
static void buildarea2(void)
Definition: cu_buildarea.c:81
static void buildarea5(void)
Definition: cu_buildarea.c:224
#define check_geom_equal(gobt, gexp)
Definition: cu_buildarea.c:23
This library is the generic geometry handling section of PostGIS.
static void buildarea7(void)
Definition: cu_buildarea.c:298