PostGIS  2.2.8dev-r@@SVN_REVISION@@
cu_split.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * Copyright (C) 2011-2015 Sandro Santilli <strk@keybit.net>
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 
20 {
21 #if POSTGIS_GEOS_VERSION >= 33
22  LWLINE *line;
23  LWPOINT *point;
24  LWMLINE *coll;
25  int ret;
26 
27  /* Because i don't trust that much prior tests... ;) */
29 
31  CU_ASSERT_EQUAL(coll->ngeoms, 0);
32 
33  line = lwgeom_as_lwline(lwgeom_from_wkt("LINESTRING(0 0,5 5, 10 0)",
35  CU_ASSERT(line != NULL);
36 
38  "POINT(0 0)",
40  ret = lwline_split_by_point_to(line, point, coll);
41  CU_ASSERT_EQUAL(ret, 1);
42  CU_ASSERT_EQUAL(coll->ngeoms, 0);
43  lwpoint_free(point);
44 
46  "POINT(10 0)",
48  ret = lwline_split_by_point_to(line, point, coll);
49  CU_ASSERT_EQUAL(ret, 1);
50  CU_ASSERT_EQUAL(coll->ngeoms, 0);
51  lwpoint_free(point);
52 
54  "POINT(5 0)",
56  ret = lwline_split_by_point_to(line, point, coll);
57  CU_ASSERT_EQUAL(ret, 0);
58  CU_ASSERT_EQUAL(coll->ngeoms, 0);
59  lwpoint_free(point);
60 
62  "POINT(5 5)",
64  ret = lwline_split_by_point_to(line, point, coll);
65  CU_ASSERT_EQUAL(ret, 2);
66  CU_ASSERT_EQUAL(coll->ngeoms, 2);
67  lwpoint_free(point);
68 
70  "POINT(2 2)",
72  ret = lwline_split_by_point_to(line, point, coll);
73  CU_ASSERT_EQUAL(ret, 2);
74  CU_ASSERT_EQUAL(coll->ngeoms, 4);
75  lwpoint_free(point);
76 
78  lwline_free(line);
79 
80 #endif /* POSTGIS_GEOS_VERSION >= 33 */
81 }
82 
83 static void test_lwgeom_split(void)
84 {
85  LWGEOM *geom, *blade, *ret;
86  char *wkt, *in_wkt;
87 
88  geom = lwgeom_from_wkt(
89 "MULTILINESTRING((-5 -2,0 0),(0 0,10 10))",
91  CU_ASSERT(geom != NULL);
92  blade = lwgeom_from_wkt(
93  "POINT(0 0)",
95  CU_ASSERT(blade != NULL);
96  ret = lwgeom_split(geom, blade);
97  CU_ASSERT(ret != NULL);
98  wkt = lwgeom_to_ewkt(ret);
99  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(-5 -2,0 0),LINESTRING(0 0,10 10))";
100  if (strcmp(in_wkt, wkt))
101  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
102  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
103  lwfree(wkt);
104  lwgeom_free(ret);
105  lwgeom_free(geom);
106  lwgeom_free(blade);
107 
108  /* See #1311 */
109  geom = lwgeom_from_wkt(
110  "LINESTRING(0 0,10 0,20 4,0 3)",
112  CU_ASSERT(geom != NULL);
113  blade = lwgeom_from_wkt("POINT(10 0)", LW_PARSER_CHECK_NONE);
114  ret = lwgeom_split(geom, blade);
115  CU_ASSERT(ret != NULL);
116  wkt = lwgeom_to_ewkt(ret);
117  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,10 0),LINESTRING(10 0,20 4,0 3))";
118  if (strcmp(in_wkt, wkt))
119  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
120  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
121  lwfree(wkt);
122  lwgeom_free(ret);
123  lwgeom_free(geom);
124  lwgeom_free(blade);
125 
126  /* See #2528 (1) -- memory leak test, needs valgrind to check */
127  geom = lwgeom_from_wkt("SRID=1;LINESTRING(0 1,10 1)", LW_PARSER_CHECK_NONE);
128  CU_ASSERT(geom != NULL);
129  blade = lwgeom_from_wkt("LINESTRING(7 0,7 3)", LW_PARSER_CHECK_NONE);
130  ret = lwgeom_split(geom, blade);
131  CU_ASSERT(ret != NULL);
132  wkt = lwgeom_to_ewkt(ret);
133  in_wkt = "SRID=1;GEOMETRYCOLLECTION(LINESTRING(0 1,7 1),LINESTRING(7 1,10 1))";
134  if (strcmp(in_wkt, wkt))
135  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
136  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
137  lwfree(wkt);
138  lwgeom_free(ret);
139  lwgeom_free(geom);
140  lwgeom_free(blade);
141 
142  /* See #2528 (2) -- memory leak test, needs valgrind to check */
143  geom = lwgeom_from_wkt("SRID=1;POLYGON((0 1, 10 1, 10 10, 0 10, 0 1))", LW_PARSER_CHECK_NONE);
144  CU_ASSERT(geom != NULL);
145  blade = lwgeom_from_wkt("LINESTRING(7 0,7 20)", LW_PARSER_CHECK_NONE);
146  ret = lwgeom_split(geom, blade);
147  CU_ASSERT(ret != NULL);
148  wkt = lwgeom_to_ewkt(ret);
149  in_wkt = "SRID=1;GEOMETRYCOLLECTION(POLYGON((7 1,0 1,0 10,7 10,7 1)),POLYGON((7 10,10 10,10 1,7 1,7 10)))";
150  if (strcmp(in_wkt, wkt))
151  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
152  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
153  lwfree(wkt);
154  lwgeom_free(ret);
155  lwgeom_free(geom);
156  lwgeom_free(blade);
157 
158  /* Split line by multiline */
159  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
160  CU_ASSERT_FATAL(geom != NULL);
161  blade = lwgeom_from_wkt("MULTILINESTRING((1 1,1 -1),(2 1,2 -1,3 -1,3 1))",
163  ret = lwgeom_split(geom, blade);
164  if ( ! ret ) printf("%s", cu_error_msg);
165  CU_ASSERT_FATAL(ret != NULL);
166  wkt = lwgeom_to_ewkt(ret);
167  CU_ASSERT_FATAL(wkt != NULL);
168  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,1 0),LINESTRING(1 0,2 0),LINESTRING(2 0,3 0),LINESTRING(3 0,10 0))";
169  if (strcmp(in_wkt, wkt))
170  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
171  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
172  lwfree(wkt);
173  lwgeom_free(ret);
174  lwgeom_free(geom);
175  lwgeom_free(blade);
176 
177  /* Split line by polygon (boundary) */
178  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
179  CU_ASSERT_FATAL(geom != NULL);
180  blade = lwgeom_from_wkt(
181 "POLYGON((1 -2,1 1,2 1,2 -1,3 -1,3 1,11 1,11 -2,1 -2))",
183  ret = lwgeom_split(geom, blade);
184  if ( ! ret ) printf("%s", cu_error_msg);
185  CU_ASSERT_FATAL(ret != NULL);
186  wkt = lwgeom_to_ewkt(ret);
187  CU_ASSERT_FATAL(wkt != NULL);
188  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,1 0),LINESTRING(1 0,2 0),LINESTRING(2 0,3 0),LINESTRING(3 0,10 0))";
189  if (strcmp(in_wkt, wkt))
190  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
191  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
192  lwfree(wkt);
193  lwgeom_free(ret);
194  lwgeom_free(geom);
195  lwgeom_free(blade);
196 
197  /* Split line by EMPTY polygon (boundary) */
198  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
199  CU_ASSERT_FATAL(geom != NULL);
200  blade = lwgeom_from_wkt("POLYGON EMPTY", LW_PARSER_CHECK_NONE);
201  ret = lwgeom_split(geom, blade);
202  if ( ! ret ) printf("%s", cu_error_msg);
203  CU_ASSERT_FATAL(ret != NULL);
204  wkt = lwgeom_to_ewkt(ret);
205  CU_ASSERT_FATAL(wkt != NULL);
206  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,10 0))";
207  if (strcmp(in_wkt, wkt))
208  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
209  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
210  lwfree(wkt);
211  lwgeom_free(ret);
212  lwgeom_free(geom);
213  lwgeom_free(blade);
214 
215  /* Split line by multipolygon (boundary) */
216  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
217  CU_ASSERT_FATAL(geom != NULL);
218  blade = lwgeom_from_wkt(
219 "MULTIPOLYGON(((1 -1,1 1,2 1,2 -1,1 -1)),((3 -1,3 1,11 1,11 -1,3 -1)))",
221  ret = lwgeom_split(geom, blade);
222  if ( ! ret ) printf("%s", cu_error_msg);
223  CU_ASSERT_FATAL(ret != NULL);
224  wkt = lwgeom_to_ewkt(ret);
225  CU_ASSERT_FATAL(wkt != NULL);
226  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,1 0),LINESTRING(1 0,2 0),LINESTRING(2 0,3 0),LINESTRING(3 0,10 0))";
227  if (strcmp(in_wkt, wkt))
228  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
229  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
230  lwfree(wkt);
231  lwgeom_free(ret);
232  lwgeom_free(geom);
233  lwgeom_free(blade);
234 
235  /* Split line by multipoint */
236  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
237  CU_ASSERT_FATAL(geom != NULL);
238  blade = lwgeom_from_wkt("MULTIPOINT(2 0,8 0,4 0)", LW_PARSER_CHECK_NONE);
239  ret = lwgeom_split(geom, blade);
240  if ( ! ret ) printf("%s", cu_error_msg);
241  CU_ASSERT_FATAL(ret != NULL);
242  wkt = lwgeom_to_ewkt(ret);
243  CU_ASSERT_FATAL(wkt != NULL);
244  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(8 0,10 0),LINESTRING(0 0,2 0),LINESTRING(4 0,8 0),LINESTRING(2 0,4 0))";
245  if (strcmp(in_wkt, wkt))
246  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
247  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
248  lwfree(wkt);
249  lwgeom_free(ret);
250  lwgeom_free(geom);
251  lwgeom_free(blade);
252 
253  /* See #3401 -- robustness issue */
254  geom = lwgeom_from_wkt("LINESTRING(-180 0,0 0)", LW_PARSER_CHECK_NONE);
255  CU_ASSERT(geom != NULL);
256  blade = lwgeom_from_wkt("POINT(-20 0)", LW_PARSER_CHECK_NONE);
257  ret = lwgeom_split(geom, blade);
258  CU_ASSERT(ret != NULL);
259  {
260  LWCOLLECTION *split = lwgeom_as_lwcollection(ret);
261  LWLINE *l1, *l2;
262  POINT2D pt;
263  CU_ASSERT(split != NULL);
264  l1 = lwgeom_as_lwline(split->geoms[0]);
265  CU_ASSERT(l1 != NULL);
266  getPoint2d_p(l1->points, 1, &pt);
267  ASSERT_DOUBLE_EQUAL(pt.x, -20);
268  ASSERT_DOUBLE_EQUAL(pt.y, 0);
269  l2 = lwgeom_as_lwline(split->geoms[1]);
270  CU_ASSERT(l2 != NULL);
271  getPoint2d_p(l2->points, 0, &pt);
272  ASSERT_DOUBLE_EQUAL(pt.x, -20);
273  ASSERT_DOUBLE_EQUAL(pt.y, 0);
274  }
275  lwgeom_free(ret);
276  lwgeom_free(geom);
277  lwgeom_free(blade);
278 }
279 
280 
281 /*
282 ** Used by test harness to register the tests in this file.
283 */
284 void split_suite_setup(void);
286 {
287  CU_pSuite suite = CU_add_suite("split", NULL, NULL);
290 }
void lwfree(void *mem)
Definition: lwutil.c:214
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:469
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:182
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
void lwline_free(LWLINE *line)
Definition: lwline.c:63
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:890
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:80
LWMLINE * lwmline_construct_empty(int srid, char hasz, char hasm)
Definition: lwmline.c:24
int ngeoms
Definition: liblwgeom.h:465
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:1869
double x
Definition: liblwgeom.h:312
void cu_error_msg_reset()
LWGEOM ** geoms
Definition: liblwgeom.h:493
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:172
#define PG_ADD_TEST(suite, testfunc)
void split_suite_setup(void)
Definition: cu_split.c:285
static void test_lwline_split_by_point_to(void)
Definition: cu_split.c:19
double y
Definition: liblwgeom.h:312
int getPoint2d_p(const POINTARRAY *pa, int n, POINT2D *point)
Definition: lwgeom_api.c:448
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:89
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:143
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:326
#define ASSERT_DOUBLE_EQUAL(o, e)
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
static void test_lwgeom_split(void)
Definition: cu_split.c:83
This library is the generic geometry handling section of PostGIS.
int lwline_split_by_point_to(const LWLINE *ln, const LWPOINT *pt, LWMLINE *to)
Split a line by a point and push components to the provided multiline.
LWGEOM * lwgeom_split(const LWGEOM *lwgeom_in, const LWGEOM *blade_in)
POINTARRAY * points
Definition: liblwgeom.h:406