PostGIS  2.5.7dev-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@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 #include "../lwgeom_geos.h"
19 
21 {
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 
77  lwline_free(line);
78 
79  line = lwgeom_as_lwline(lwgeom_from_wkt("LINESTRING EMPTY",
81  CU_ASSERT(line != NULL);
83  "POINT(0 0)", LW_PARSER_CHECK_NONE));
84  CU_ASSERT(point != NULL);
85  ret = lwline_split_by_point_to(line, point, coll);
86  CU_ASSERT_EQUAL(ret, 0); /* the point is not on the line */
87  lwpoint_free(point);
88  lwline_free(line);
89 
91 }
92 
93 static void test_lwgeom_split(void)
94 {
95  LWGEOM *geom, *blade, *ret, *tmp1, *tmp2;
96  char *wkt, *in_wkt;
97 
98  geom = lwgeom_from_wkt("MULTILINESTRING((-5 -2,0 0),(0 0,10 10))", LW_PARSER_CHECK_NONE);
99  CU_ASSERT(geom != NULL);
100  blade = lwgeom_from_wkt("POINT(0 0)", LW_PARSER_CHECK_NONE);
101  CU_ASSERT(blade != NULL);
102  ret = lwgeom_split(geom, blade);
103  CU_ASSERT(ret != NULL);
104  wkt = lwgeom_to_ewkt(ret);
105  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(-5 -2,0 0),LINESTRING(0 0,10 10))";
106  ASSERT_STRING_EQUAL(wkt, in_wkt);
107  lwfree(wkt);
108  lwgeom_free(ret);
109  lwgeom_free(geom);
110  lwgeom_free(blade);
111 
112  /* See #1311 */
113  geom = lwgeom_from_wkt("LINESTRING(0 0,10 0,20 4,0 3)", LW_PARSER_CHECK_NONE);
114  CU_ASSERT(geom != NULL);
115  blade = lwgeom_from_wkt("POINT(10 0)", LW_PARSER_CHECK_NONE);
116  ret = lwgeom_split(geom, blade);
117  CU_ASSERT(ret != NULL);
118  wkt = lwgeom_to_ewkt(ret);
119  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,10 0),LINESTRING(10 0,20 4,0 3))";
120  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  ASSERT_STRING_EQUAL(wkt, in_wkt);
135  lwfree(wkt);
136  lwgeom_free(ret);
137  lwgeom_free(geom);
138  lwgeom_free(blade);
139 
140  /* See #2528 (2) -- memory leak test, needs valgrind to check */
141  geom = lwgeom_from_wkt("SRID=1;POLYGON((0 1, 10 1, 10 10, 0 10, 0 1))", LW_PARSER_CHECK_NONE);
142  CU_ASSERT(geom != NULL);
143  blade = lwgeom_from_wkt("LINESTRING(7 0,7 20)", LW_PARSER_CHECK_NONE);
144  tmp1 = lwgeom_split(geom, blade);
145  ret = lwgeom_normalize(tmp1);
146  lwgeom_free(tmp1);
147  CU_ASSERT(ret != NULL);
148  wkt = lwgeom_to_ewkt(ret);
149  tmp1 = lwgeom_from_wkt(
150  "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)))",
152  tmp2 = lwgeom_normalize(tmp1);
153  in_wkt = lwgeom_to_ewkt(tmp2);
154  ASSERT_STRING_EQUAL(wkt, in_wkt);
155  lwfree(wkt);
156  lwfree(in_wkt);
157  lwgeom_free(tmp1);
158  lwgeom_free(tmp2);
159  lwgeom_free(ret);
160  lwgeom_free(geom);
161  lwgeom_free(blade);
162 
163  /* Split line by multiline */
164  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
165  CU_ASSERT_FATAL(geom != NULL);
166  blade = lwgeom_from_wkt("MULTILINESTRING((1 1,1 -1),(2 1,2 -1,3 -1,3 1))", LW_PARSER_CHECK_NONE);
167  ret = lwgeom_split(geom, blade);
168  if (!ret)
169  printf("%s", cu_error_msg);
170  CU_ASSERT_FATAL(ret != NULL);
171  wkt = lwgeom_to_ewkt(ret);
172  CU_ASSERT_FATAL(wkt != NULL);
173  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,1 0),LINESTRING(1 0,2 0),LINESTRING(2 0,3 0),LINESTRING(3 0,10 0))";
174  ASSERT_STRING_EQUAL(wkt, in_wkt);
175  lwfree(wkt);
176  lwgeom_free(ret);
177  lwgeom_free(geom);
178  lwgeom_free(blade);
179 
180  /* Split line by polygon (boundary) */
181  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
182  CU_ASSERT_FATAL(geom != NULL);
183  blade = lwgeom_from_wkt("POLYGON((1 -2,1 1,2 1,2 -1,3 -1,3 1,11 1,11 -2,1 -2))", LW_PARSER_CHECK_NONE);
184  ret = lwgeom_split(geom, blade);
185  if (!ret)
186  printf("%s", cu_error_msg);
187  CU_ASSERT_FATAL(ret != NULL);
188  wkt = lwgeom_to_ewkt(ret);
189  CU_ASSERT_FATAL(wkt != NULL);
190  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,1 0),LINESTRING(1 0,2 0),LINESTRING(2 0,3 0),LINESTRING(3 0,10 0))";
191  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)
203  printf("%s", cu_error_msg);
204  CU_ASSERT_FATAL(ret != NULL);
205  wkt = lwgeom_to_ewkt(ret);
206  CU_ASSERT_FATAL(wkt != NULL);
207  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,10 0))";
208  ASSERT_STRING_EQUAL(wkt, in_wkt);
209  lwfree(wkt);
210  lwgeom_free(ret);
211  lwgeom_free(geom);
212  lwgeom_free(blade);
213 
214  /* Split line by multipolygon (boundary) */
215  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
216  CU_ASSERT_FATAL(geom != NULL);
217  blade = lwgeom_from_wkt("MULTIPOLYGON(((1 -1,1 1,2 1,2 -1,1 -1)),((3 -1,3 1,11 1,11 -1,3 -1)))",
219  ret = lwgeom_split(geom, blade);
220  if (!ret)
221  printf("%s", cu_error_msg);
222  CU_ASSERT_FATAL(ret != NULL);
223  wkt = lwgeom_to_ewkt(ret);
224  CU_ASSERT_FATAL(wkt != NULL);
225  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,1 0),LINESTRING(1 0,2 0),LINESTRING(2 0,3 0),LINESTRING(3 0,10 0))";
226  ASSERT_STRING_EQUAL(wkt, in_wkt);
227  lwfree(wkt);
228  lwgeom_free(ret);
229  lwgeom_free(geom);
230  lwgeom_free(blade);
231 
232  /* Split line by multipoint */
233  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
234  CU_ASSERT_FATAL(geom != NULL);
235  blade = lwgeom_from_wkt("MULTIPOINT(2 0,8 0,4 0)", LW_PARSER_CHECK_NONE);
236  ret = lwgeom_split(geom, blade);
237  if (!ret)
238  printf("%s", cu_error_msg);
239  CU_ASSERT_FATAL(ret != NULL);
240  wkt = lwgeom_to_ewkt(ret);
241  CU_ASSERT_FATAL(wkt != NULL);
242  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(8 0,10 0),LINESTRING(0 0,2 0),LINESTRING(4 0,8 0),LINESTRING(2 0,4 0))";
243  ASSERT_STRING_EQUAL(wkt, in_wkt);
244  lwfree(wkt);
245  lwgeom_free(ret);
246  lwgeom_free(geom);
247  lwgeom_free(blade);
248 
249  /* See #3401 -- robustness issue */
250  geom = lwgeom_from_wkt("LINESTRING(-180 0,0 0)", LW_PARSER_CHECK_NONE);
251  CU_ASSERT(geom != NULL);
252  blade = lwgeom_from_wkt("POINT(-20 0)", LW_PARSER_CHECK_NONE);
253  ret = lwgeom_split(geom, blade);
254  CU_ASSERT(ret != NULL);
255  {
256  LWCOLLECTION *split = lwgeom_as_lwcollection(ret);
257  LWLINE *l1, *l2;
258  POINT2D pt;
259  CU_ASSERT(split != NULL);
260  l1 = lwgeom_as_lwline(split->geoms[0]);
261  CU_ASSERT(l1 != NULL);
262  getPoint2d_p(l1->points, 1, &pt);
263  ASSERT_DOUBLE_EQUAL(pt.x, -20);
264  ASSERT_DOUBLE_EQUAL(pt.y, 0);
265  l2 = lwgeom_as_lwline(split->geoms[1]);
266  CU_ASSERT(l2 != NULL);
267  getPoint2d_p(l2->points, 0, &pt);
268  ASSERT_DOUBLE_EQUAL(pt.x, -20);
269  ASSERT_DOUBLE_EQUAL(pt.y, 0);
270  }
271  lwgeom_free(ret);
272  lwgeom_free(geom);
273  lwgeom_free(blade);
274 }
275 
276 static int
278 {
279  finishGEOS();
280  return 0;
281 }
282 
283 /*
284 ** Used by test harness to register the tests in this file.
285 */
286 void split_suite_setup(void);
288 {
289  CU_pSuite suite = CU_add_suite("split", NULL, clean_geos_split_suite);
292 }
static int clean_geos_split_suite(void)
Definition: cu_split.c:277
void split_suite_setup(void)
Definition: cu_split.c:287
static void test_lwgeom_split(void)
Definition: cu_split.c:93
static void test_lwline_split_by_point_to(void)
Definition: cu_split.c:20
void cu_error_msg_reset()
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
#define ASSERT_DOUBLE_EQUAL(o, e)
#define PG_ADD_TEST(suite, testfunc)
#define ASSERT_STRING_EQUAL(o, e)
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:170
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2005
LWGEOM * lwgeom_split(const LWGEOM *lwgeom_in, const LWGEOM *blade_in)
LWMLINE * lwmline_construct_empty(int srid, char hasz, char hasm)
Definition: lwmline.c:38
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:348
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:556
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:161
void lwfree(void *mem)
Definition: lwutil.c:244
LWGEOM * lwgeom_normalize(const LWGEOM *geom)
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:356
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:224
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:188
void lwline_free(LWLINE *line)
Definition: lwline.c:76
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 ** geoms
Definition: liblwgeom.h:512
POINTARRAY * points
Definition: liblwgeom.h:425
uint32_t ngeoms
Definition: liblwgeom.h:484
double y
Definition: liblwgeom.h:331
double x
Definition: liblwgeom.h:331