PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ test_lwgeom_split()

static void test_lwgeom_split ( void  )
static

Definition at line 80 of file cu_split.c.

References ASSERT_DOUBLE_EQUAL, cu_error_msg, LWCOLLECTION::geoms, getPoint2d_p(), LW_PARSER_CHECK_NONE, lwfree(), lwgeom_as_lwcollection(), lwgeom_as_lwline(), lwgeom_free(), lwgeom_from_wkt(), lwgeom_split(), lwgeom_to_ewkt(), LWLINE::points, split_suite_setup(), POINT2D::x, and POINT2D::y.

Referenced by split_suite_setup().

81 {
82  LWGEOM *geom, *blade, *ret;
83  char *wkt, *in_wkt;
84 
85  geom = lwgeom_from_wkt(
86 "MULTILINESTRING((-5 -2,0 0),(0 0,10 10))",
88  CU_ASSERT(geom != NULL);
89  blade = lwgeom_from_wkt(
90  "POINT(0 0)",
92  CU_ASSERT(blade != NULL);
93  ret = lwgeom_split(geom, blade);
94  CU_ASSERT(ret != NULL);
95  wkt = lwgeom_to_ewkt(ret);
96  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(-5 -2,0 0),LINESTRING(0 0,10 10))";
97  if (strcmp(in_wkt, wkt))
98  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
99  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
100  lwfree(wkt);
101  lwgeom_free(ret);
102  lwgeom_free(geom);
103  lwgeom_free(blade);
104 
105  /* See #1311 */
106  geom = lwgeom_from_wkt(
107  "LINESTRING(0 0,10 0,20 4,0 3)",
109  CU_ASSERT(geom != NULL);
110  blade = lwgeom_from_wkt("POINT(10 0)", LW_PARSER_CHECK_NONE);
111  ret = lwgeom_split(geom, blade);
112  CU_ASSERT(ret != NULL);
113  wkt = lwgeom_to_ewkt(ret);
114  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,10 0),LINESTRING(10 0,20 4,0 3))";
115  if (strcmp(in_wkt, wkt))
116  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
117  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
118  lwfree(wkt);
119  lwgeom_free(ret);
120  lwgeom_free(geom);
121  lwgeom_free(blade);
122 
123  /* See #2528 (1) -- memory leak test, needs valgrind to check */
124  geom = lwgeom_from_wkt("SRID=1;LINESTRING(0 1,10 1)", LW_PARSER_CHECK_NONE);
125  CU_ASSERT(geom != NULL);
126  blade = lwgeom_from_wkt("LINESTRING(7 0,7 3)", LW_PARSER_CHECK_NONE);
127  ret = lwgeom_split(geom, blade);
128  CU_ASSERT(ret != NULL);
129  wkt = lwgeom_to_ewkt(ret);
130  in_wkt = "SRID=1;GEOMETRYCOLLECTION(LINESTRING(0 1,7 1),LINESTRING(7 1,10 1))";
131  if (strcmp(in_wkt, wkt))
132  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
133  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
134  lwfree(wkt);
135  lwgeom_free(ret);
136  lwgeom_free(geom);
137  lwgeom_free(blade);
138 
139  /* See #2528 (2) -- memory leak test, needs valgrind to check */
140  geom = lwgeom_from_wkt("SRID=1;POLYGON((0 1, 10 1, 10 10, 0 10, 0 1))", LW_PARSER_CHECK_NONE);
141  CU_ASSERT(geom != NULL);
142  blade = lwgeom_from_wkt("LINESTRING(7 0,7 20)", LW_PARSER_CHECK_NONE);
143  ret = lwgeom_split(geom, blade);
144  CU_ASSERT(ret != NULL);
145  wkt = lwgeom_to_ewkt(ret);
146  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)))";
147  if (strcmp(in_wkt, wkt))
148  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
149  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
150  lwfree(wkt);
151  lwgeom_free(ret);
152  lwgeom_free(geom);
153  lwgeom_free(blade);
154 
155  /* Split line by multiline */
156  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
157  CU_ASSERT_FATAL(geom != NULL);
158  blade = lwgeom_from_wkt("MULTILINESTRING((1 1,1 -1),(2 1,2 -1,3 -1,3 1))",
160  ret = lwgeom_split(geom, blade);
161  if ( ! ret ) printf("%s", cu_error_msg);
162  CU_ASSERT_FATAL(ret != NULL);
163  wkt = lwgeom_to_ewkt(ret);
164  CU_ASSERT_FATAL(wkt != NULL);
165  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,1 0),LINESTRING(1 0,2 0),LINESTRING(2 0,3 0),LINESTRING(3 0,10 0))";
166  if (strcmp(in_wkt, wkt))
167  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
168  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
169  lwfree(wkt);
170  lwgeom_free(ret);
171  lwgeom_free(geom);
172  lwgeom_free(blade);
173 
174  /* Split line by polygon (boundary) */
175  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
176  CU_ASSERT_FATAL(geom != NULL);
177  blade = lwgeom_from_wkt(
178 "POLYGON((1 -2,1 1,2 1,2 -1,3 -1,3 1,11 1,11 -2,1 -2))",
180  ret = lwgeom_split(geom, blade);
181  if ( ! ret ) printf("%s", cu_error_msg);
182  CU_ASSERT_FATAL(ret != NULL);
183  wkt = lwgeom_to_ewkt(ret);
184  CU_ASSERT_FATAL(wkt != NULL);
185  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,1 0),LINESTRING(1 0,2 0),LINESTRING(2 0,3 0),LINESTRING(3 0,10 0))";
186  if (strcmp(in_wkt, wkt))
187  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
188  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
189  lwfree(wkt);
190  lwgeom_free(ret);
191  lwgeom_free(geom);
192  lwgeom_free(blade);
193 
194  /* Split line by EMPTY polygon (boundary) */
195  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
196  CU_ASSERT_FATAL(geom != NULL);
197  blade = lwgeom_from_wkt("POLYGON EMPTY", LW_PARSER_CHECK_NONE);
198  ret = lwgeom_split(geom, blade);
199  if ( ! ret ) printf("%s", cu_error_msg);
200  CU_ASSERT_FATAL(ret != NULL);
201  wkt = lwgeom_to_ewkt(ret);
202  CU_ASSERT_FATAL(wkt != NULL);
203  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,10 0))";
204  if (strcmp(in_wkt, wkt))
205  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
206  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
207  lwfree(wkt);
208  lwgeom_free(ret);
209  lwgeom_free(geom);
210  lwgeom_free(blade);
211 
212  /* Split line by multipolygon (boundary) */
213  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
214  CU_ASSERT_FATAL(geom != NULL);
215  blade = lwgeom_from_wkt(
216 "MULTIPOLYGON(((1 -1,1 1,2 1,2 -1,1 -1)),((3 -1,3 1,11 1,11 -1,3 -1)))",
218  ret = lwgeom_split(geom, blade);
219  if ( ! ret ) printf("%s", cu_error_msg);
220  CU_ASSERT_FATAL(ret != NULL);
221  wkt = lwgeom_to_ewkt(ret);
222  CU_ASSERT_FATAL(wkt != NULL);
223  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(0 0,1 0),LINESTRING(1 0,2 0),LINESTRING(2 0,3 0),LINESTRING(3 0,10 0))";
224  if (strcmp(in_wkt, wkt))
225  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
226  CU_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 ) printf("%s", cu_error_msg);
238  CU_ASSERT_FATAL(ret != NULL);
239  wkt = lwgeom_to_ewkt(ret);
240  CU_ASSERT_FATAL(wkt != NULL);
241  in_wkt = "GEOMETRYCOLLECTION(LINESTRING(8 0,10 0),LINESTRING(0 0,2 0),LINESTRING(4 0,8 0),LINESTRING(2 0,4 0))";
242  if (strcmp(in_wkt, wkt))
243  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_wkt, wkt);
244  CU_ASSERT_STRING_EQUAL(wkt, in_wkt);
245  lwfree(wkt);
246  lwgeom_free(ret);
247  lwgeom_free(geom);
248  lwgeom_free(blade);
249 
250  /* See #3401 -- robustness issue */
251  geom = lwgeom_from_wkt("LINESTRING(-180 0,0 0)", LW_PARSER_CHECK_NONE);
252  CU_ASSERT(geom != NULL);
253  blade = lwgeom_from_wkt("POINT(-20 0)", LW_PARSER_CHECK_NONE);
254  ret = lwgeom_split(geom, blade);
255  CU_ASSERT(ret != NULL);
256  {
257  LWCOLLECTION *split = lwgeom_as_lwcollection(ret);
258  LWLINE *l1, *l2;
259  POINT2D pt;
260  CU_ASSERT(split != NULL);
261  l1 = lwgeom_as_lwline(split->geoms[0]);
262  CU_ASSERT(l1 != NULL);
263  getPoint2d_p(l1->points, 1, &pt);
264  ASSERT_DOUBLE_EQUAL(pt.x, -20);
265  ASSERT_DOUBLE_EQUAL(pt.y, 0);
266  l2 = lwgeom_as_lwline(split->geoms[1]);
267  CU_ASSERT(l2 != NULL);
268  getPoint2d_p(l2->points, 0, &pt);
269  ASSERT_DOUBLE_EQUAL(pt.x, -20);
270  ASSERT_DOUBLE_EQUAL(pt.y, 0);
271  }
272  lwgeom_free(ret);
273  lwgeom_free(geom);
274  lwgeom_free(blade);
275 }
void lwfree(void *mem)
Definition: lwutil.c:244
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:518
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2013
double x
Definition: liblwgeom.h:328
LWGEOM ** geoms
Definition: liblwgeom.h:509
double y
Definition: liblwgeom.h:328
int getPoint2d_p(const POINTARRAY *pa, int n, POINT2D *point)
Definition: lwgeom_api.c:347
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:138
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:192
#define ASSERT_DOUBLE_EQUAL(o, e)
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
LWGEOM * lwgeom_split(const LWGEOM *lwgeom_in, const LWGEOM *blade_in)
POINTARRAY * points
Definition: liblwgeom.h:422
Here is the call graph for this function:
Here is the caller graph for this function: