PostGIS  2.5.7dev-r@@SVN_REVISION@@
cu_in_wkt.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  * Copyright 2010 Paul Ramsey <pramsey@cleverelephant.ca>
6  *
7  * This is free software; you can redistribute and/or modify it under
8  * the terms of the GNU General Public Licence. See the COPYING file.
9  *
10  **********************************************************************/
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "CUnit/Basic.h"
16 
17 #include "liblwgeom_internal.h"
18 #include "cu_tester.h"
19 
20 /*
21 ** Globals used by tests
22 */
23 char *s;
24 char *r;
25 
26 /*
27 ** The suite initialization function.
28 ** Create any re-used objects.
29 */
30 static int init_wkt_in_suite(void)
31 {
32  return 0;
33 }
34 
35 /*
36 ** The suite cleanup function.
37 ** Frees any global objects.
38 */
39 static int clean_wkt_in_suite(void)
40 {
41  return 0;
42 }
43 
44 /*
45 * Return a char* that results from taking the input
46 * WKT, creating an LWGEOM, then writing it back out
47 * as an output WKT using the supplied variant.
48 * If there is an error, return that.
49 */
50 static char* cu_wkt_in(char *wkt, uint8_t variant)
51 {
53  int rv = 0;
54  char *s = 0;
55 
56  rv = lwgeom_parse_wkt(&p, wkt, 0);
57  if( p.errcode ) {
58  CU_ASSERT_EQUAL( rv, LW_FAILURE );
59  return strdup(p.message);
60  }
61  CU_ASSERT_EQUAL( rv, LW_SUCCESS );
62  s = lwgeom_to_wkt(p.geom, variant, 8, NULL);
64  return s;
65 }
66 
67 
68 static void test_wkt_in_point(void)
69 {
70  s = "POINT(1 2) foobar";
71  r = cu_wkt_in(s, WKT_SFSQL);
72  CU_ASSERT_STRING_EQUAL("parse error - invalid geometry", r);
73  lwfree(r);
74 
75  s = "POINT(1e700 0)";
76  r = cu_wkt_in(s, WKT_SFSQL);
77  CU_TEST ( ! strcmp(r, "POINT(inf 0)") || ! strcmp(r, "POINT(1.#INF 0)") || ! strcmp(r, "POINT(Infinity 0)") );
78  lwfree(r);
79 
80  s = "POINT(0 0)";
81  r = cu_wkt_in(s, WKT_SFSQL);
82  CU_ASSERT_STRING_EQUAL(r,s);
83  lwfree(r);
84 
85  s = "POINT EMPTY";
86  r = cu_wkt_in(s, WKT_SFSQL);
87  CU_ASSERT_STRING_EQUAL(r,s);
88  lwfree(r);
89 
90  s = "POINT M EMPTY";
91  r = cu_wkt_in(s, WKT_ISO);
92  CU_ASSERT_STRING_EQUAL(r,s);
93  lwfree(r);
94 
95  //printf("\nIN: %s\nOUT: %s\n",s,r);
96 }
97 
98 static void test_wkt_in_linestring(void)
99 {
100  s = "LINESTRING EMPTY";
101  r = cu_wkt_in(s, WKT_SFSQL);
102  CU_ASSERT_STRING_EQUAL(r,s);
103  lwfree(r);
104 
105  s = "LINESTRING(0 0,1 1)";
106  r = cu_wkt_in(s, WKT_SFSQL);
107  CU_ASSERT_STRING_EQUAL(r,s);
108  lwfree(r);
109 
110  s = "LINESTRING(0 0 0,1 1 1)";
112  CU_ASSERT_STRING_EQUAL(r,s);
113  lwfree(r);
114 
115  s = "LINESTRING M (0 0 0,1 1 1)";
116  r = cu_wkt_in(s, WKT_ISO);
117  CU_ASSERT_STRING_EQUAL(r,s);
118  lwfree(r);
119 
120  s = "LINESTRING ZM (0 0 0 1,1 1 1 1,2 2 2 2,0.141231 4 5 4)";
121  r = cu_wkt_in(s, WKT_ISO);
122  CU_ASSERT_STRING_EQUAL(r,s);
123  lwfree(r);
124 
125  s = "LINESTRINGM(0 0 0,1 1 1)";
127  CU_ASSERT_STRING_EQUAL(r,s);
128  lwfree(r);
129 
130  s = "LINESTRING ZM EMPTY";
131  r = cu_wkt_in(s, WKT_ISO);
132  CU_ASSERT_STRING_EQUAL(r,s);
133  lwfree(r);
134 
135  s = "LINESTRING Z (0 0 0 1, 0 1 0 1)";
137  CU_ASSERT_STRING_EQUAL(r,"can not mix dimensionality in a geometry");
138  //printf("\nIN: %s\nOUT: %s\n",s,r);
139  lwfree(r);
140 
141 }
142 
143 static void test_wkt_in_polygon(void)
144 {
145  s = "POLYGON((0 0,0 1,1 1,0 0))";
146  r = cu_wkt_in(s, WKT_SFSQL);
147  CU_ASSERT_STRING_EQUAL(r,s);
148  lwfree(r);
149 
150  s = "POLYGON Z ((0 0,0 10,10 10,10 0,0 0),(1 1 1,1 2 1,2 2 1,2 1 1,1 1 1))";
151  r = cu_wkt_in(s, WKT_SFSQL);
152  CU_ASSERT_STRING_EQUAL(r,"can not mix dimensionality in a geometry");
153  lwfree(r);
154 
155  s = "POLYGON Z ((0 0,0 10,10 10,10 0,0 0),(1 1,1 2,2 2,2 1,1 1))";
156  r = cu_wkt_in(s, WKT_SFSQL);
157  CU_ASSERT_STRING_EQUAL(r,"can not mix dimensionality in a geometry");
158  //printf("\nIN: %s\nOUT: %s\n",s,r);
159  lwfree(r);
160 }
161 
162 static void test_wkt_in_multipoint(void)
163 {
168  // s = "MULTIPOINT(-1 -2 -3,5.4 6.6 7.77,-5.4 -6.6 -7.77,1000000 1e-06 -1000000,-1.3e-06 -1.4e-05 0)";
169  // r = cu_wkt_in(s, WKT_EXTENDED);
170  // CU_ASSERT_STRING_EQUAL(r,s);
171  // printf("\nIN: %s\nOUT: %s\n",s,r);
172  // lwfree(r);
173 
174  s = "MULTIPOINT(0 0)";
175  r = cu_wkt_in(s, WKT_SFSQL);
176  CU_ASSERT_STRING_EQUAL(r,s);
177  //printf("\nIN: %s\nOUT: %s\n",s,r);
178  lwfree(r);
179 
180  s = "MULTIPOINT(0 0,1 1)";
181  r = cu_wkt_in(s, WKT_SFSQL);
182  CU_ASSERT_STRING_EQUAL(r,s);
183  //printf("\nIN: %s\nOUT: %s\n",s,r);
184  lwfree(r);
185 }
186 
188 {
189  s = "MULTILINESTRING((0 0,1 1),(1 1,2 2),(3 3,3 3,3 3,2 2,2 1))";
190  r = cu_wkt_in(s, WKT_SFSQL);
191  CU_ASSERT_STRING_EQUAL(r,s);
192  //printf("\nIN: %s\nOUT: %s\n",s,r);
193  lwfree(r);
194 
195 }
196 
197 static void test_wkt_in_multipolygon(void)
198 {
199  s = "MULTIPOLYGON(((0 0,0 1,1 1,0 0)))";
200  r = cu_wkt_in(s, WKT_SFSQL);
201  CU_ASSERT_STRING_EQUAL(r,s);
202  //printf("\nIN: %s\nOUT: %s\n",s,r);
203  lwfree(r);
204 
205  s = "MULTIPOLYGON(((0 0,0 10,10 10,0 0),(1 1,1 2,2 2,1 1)),((-10 -10,-10 -5,-5 -5,-10 -10)))";
206  r = cu_wkt_in(s, WKT_SFSQL);
207  CU_ASSERT_STRING_EQUAL(r,s);
208  //printf("\nIN: %s\nOUT: %s\n",s,r);
209  lwfree(r);
210 
211  s = "SRID=4;MULTIPOLYGON(((0 0,0 1,1 1,0 0)))";
213  CU_ASSERT_STRING_EQUAL(r,s);
214  //printf("\nIN: %s\nOUT: %s\n",s,r);
215  lwfree(r);
216 
217 }
218 
219 static void test_wkt_in_collection(void)
220 {
221  s = "SRID=5;GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(1 0,0 0),CIRCULARSTRING(0 0,0 1,1 1,0 1,2 2))";
223  //printf("\nIN: %s\nOUT: %s\n",s,r);
224  CU_ASSERT_STRING_EQUAL(r,s);
225  lwfree(r);
226 
227  s = "GEOMETRYCOLLECTION(POINT(0 0),POINT EMPTY,LINESTRING(1 0,0 0),POLYGON EMPTY,CIRCULARSTRING(0 0,0 1,1 1,0 1,2 2))";
228  r = cu_wkt_in(s, WKT_SFSQL);
229  //printf("\nIN: %s\nOUT: %s\n",s,r);
230  CU_ASSERT_STRING_EQUAL(r,s);
231  lwfree(r);
232 
233  s = "GEOMETRYCOLLECTION Z (POINT Z (0 0 0))";
234  r = cu_wkt_in(s, WKT_ISO);
235  //printf("\nIN: %s\nOUT: %s\n",s,r);
236  CU_ASSERT_STRING_EQUAL(r,s);
237  lwfree(r);
238 
239  s = "GEOMETRYCOLLECTION M (MULTILINESTRING M ((0 0 5,2 0 5),(1 1 5,2 2 5)))";
240  r = cu_wkt_in(s, WKT_ISO);
241  //printf("\nIN: %s\nOUT: %s\n",s,r);
242  CU_ASSERT_STRING_EQUAL(r,s);
243  lwfree(r);
244 
245  /* See http://trac.osgeo.org/postgis/ticket/1455#comment:3 */
246  s = "GEOMETRYCOLLECTION Z (MULTILINESTRING Z ((0 0 5,2 0 5),(1 1 5,2 2 5)))";
247  r = cu_wkt_in(s, WKT_ISO);
248  //printf("\nIN: %s\nOUT: %s\n",s,r);
249  CU_ASSERT_STRING_EQUAL(r,s);
250  lwfree(r);
251 }
252 
253 static void test_wkt_in_circularstring(void)
254 {
255  s = "CIRCULARSTRING(0 0,0 1,1 1,0 1,2 2)";
256  r = cu_wkt_in(s, WKT_SFSQL);
257  //printf("\nIN: %s\nOUT: %s\n",s,r);
258  CU_ASSERT_STRING_EQUAL(r,s);
259  lwfree(r);
260 }
261 
262 static void test_wkt_in_compoundcurve(void)
263 {
264  s = "SRID=4326;COMPOUNDCURVEM(CIRCULARSTRINGM(0 0 2,1 1 2,1 0 2),(1 0 2,0 1 2))";
266  CU_ASSERT_STRING_EQUAL(r,s);
267  //printf("\nIN: %s\nOUT: %s\n",s,r);
268  lwfree(r);
269 
270  s = "COMPOUNDCURVE Z (CIRCULARSTRING Z (0 0 0,0 1 0,1 1 0,0 0 0,2 2 0),(2 2 0,0 0 1,1 1 1,2 2 1))";
271  r = cu_wkt_in(s, WKT_ISO);
272  CU_ASSERT_STRING_EQUAL(r,s);
273  //printf("\nIN: %s\nOUT: %s\n",s,r);
274  lwfree(r);
275 }
276 
277 static void test_wkt_in_curvpolygon(void)
278 {
279  s = "CURVEPOLYGON(COMPOUNDCURVE(CIRCULARSTRING(0 0,0 1,1 1,2 2,0 0),(0 0,1 1,2 2)),CIRCULARSTRING(0 0,0 1,1 1,0 0,2 2),(0 0,1 1,2 1))";
280  r = cu_wkt_in(s, WKT_ISO);
281  CU_ASSERT_STRING_EQUAL(r,s);
282  //printf("\nIN: %s\nOUT: %s\n",s,r);
283  lwfree(r);
284 }
285 
286 static void test_wkt_in_multicurve(void)
287 {
288  s = "SRID=4326;MULTICURVE(COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1)))";
290  CU_ASSERT_STRING_EQUAL(r,s);
291  //printf("\nIN: %s\nOUT: %s\n",s,r);
292  lwfree(r);
293 }
294 
295 static void test_wkt_in_multisurface(void)
296 {
297  s = "SRID=4326;MULTICURVE(COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1)))";
299  CU_ASSERT_STRING_EQUAL(r,s);
300  //printf("\nIN: %s\nOUT: %s\n",s,r);
301  lwfree(r);
302 }
303 
304 static void test_wkt_in_tin(void)
305 {
306  s = "TIN(((0 1 2,3 4 5,6 7 8,0 1 2)),((0 1 2,3 4 5,6 7 8,9 10 11,0 1 2)))";
308  CU_ASSERT_STRING_EQUAL(r,"triangle must have exactly 4 points");
309  //printf("\nIN: %s\nOUT: %s\n",s,r);
310  lwfree(r);
311 }
312 
314 {
315  s = "POLYHEDRALSURFACE(((0 0 0,0 0 1,0 1 0,0 0 0)),((0 0 0,0 1 0,1 0 0,0 0 0)),((0 0 0,1 0 0,0 0 1,0 0 0)),((1 0 0,0 1 0,0 0 1,1 0 0)))";
317  CU_ASSERT_STRING_EQUAL(r,s);
318  //printf("\nIN: %s\nOUT: %s\n",s,r);
319  lwfree(r);
320 
321  s = "POLYHEDRALSURFACE Z (((0 0 0,0 0 1,0 1 0,0 0 0)),((0 0 0,0 1 0,1 0 0,0 0 0)),((0 0 0,1 0 0,0 0 1,0 0 0)),((1 0 0,0 1 0,0 0 1,1 0 0)))";
322  r = cu_wkt_in(s, WKT_ISO);
323  CU_ASSERT_STRING_EQUAL(r,s);
324  //printf("\nIN: %s\nOUT: %s\n",s,r);
325  lwfree(r);
326 
327  s = "POLYHEDRALSURFACE(((0 1 2,3 4 5,6 7,0 1 2)))";
328  r = cu_wkt_in(s, WKT_ISO);
329  CU_ASSERT_STRING_EQUAL(r,"can not mix dimensionality in a geometry");
330  //printf("\nIN: %s\nOUT: %s\n",s,r);
331  lwfree(r);
332 
333 }
334 
335 static void test_wkt_in_errlocation(void)
336 {
338  int rv = 0;
339  char *wkt = 0;
340 
341  wkt = "LINESTRING((0 0 0,1 1)";
343  rv = lwgeom_parse_wkt(&p, wkt, LW_PARSER_CHECK_ALL);
344  CU_ASSERT_EQUAL( rv, LW_FAILURE );
345  CU_ASSERT((12 - p.errlocation) < 1.5);
346 
347 // printf("errlocation %d\n", p.errlocation);
348 // printf("message %s\n", p.message);
349 
351 
352 }
353 
354 static void test_wkt_double(void)
355 {
357  int rv = 0;
358  char *wkt = 0;
359 
360  wkt = "LINESTRING(1.1.1, 2.2.2)";
362  rv = lwgeom_parse_wkt(&p, wkt, LW_PARSER_CHECK_ALL);
363  CU_ASSERT_EQUAL( rv, LW_FAILURE );
365 
366  wkt = "LINESTRING(1.1 .1, 2.2 .2)";
368  rv = lwgeom_parse_wkt(&p, wkt, LW_PARSER_CHECK_ALL);
369  CU_ASSERT_EQUAL( rv, LW_SUCCESS );
371 
372  wkt = "LINESTRING( 1.1 .1 , 2.2 .2 )";
374  rv = lwgeom_parse_wkt(&p, wkt, LW_PARSER_CHECK_ALL);
375  CU_ASSERT_EQUAL( rv, LW_SUCCESS );
377 
378  wkt = "LINESTRING(\n1.1\n.1,\n2.2\n.2\n)";
380  rv = lwgeom_parse_wkt(&p, wkt, LW_PARSER_CHECK_ALL);
381  CU_ASSERT_EQUAL( rv, LW_SUCCESS );
383 
384  wkt = "LINESTRING(1.1\t.1\t,\t2.2\t.2\t)";
386  rv = lwgeom_parse_wkt(&p, wkt, LW_PARSER_CHECK_ALL);
387  CU_ASSERT_EQUAL( rv, LW_SUCCESS );
389 }
390 
391 /*
392 ** Used by test harness to register the tests in this file.
393 */
394 void wkt_in_suite_setup(void);
396 {
397  CU_pSuite suite = CU_add_suite("wkt_input", init_wkt_in_suite, clean_wkt_in_suite);
414 }
static uint8_t variant
Definition: cu_in_twkb.c:26
static void test_wkt_in_tin(void)
Definition: cu_in_wkt.c:304
static void test_wkt_in_linestring(void)
Definition: cu_in_wkt.c:98
static void test_wkt_in_point(void)
Definition: cu_in_wkt.c:68
static void test_wkt_in_multipoint(void)
Definition: cu_in_wkt.c:162
static int clean_wkt_in_suite(void)
Definition: cu_in_wkt.c:39
static void test_wkt_in_multisurface(void)
Definition: cu_in_wkt.c:295
static void test_wkt_in_polygon(void)
Definition: cu_in_wkt.c:143
static void test_wkt_in_multipolygon(void)
Definition: cu_in_wkt.c:197
static void test_wkt_in_collection(void)
Definition: cu_in_wkt.c:219
static void test_wkt_in_curvpolygon(void)
Definition: cu_in_wkt.c:277
static void test_wkt_double(void)
Definition: cu_in_wkt.c:354
static void test_wkt_in_multicurve(void)
Definition: cu_in_wkt.c:286
static void test_wkt_in_errlocation(void)
Definition: cu_in_wkt.c:335
static void test_wkt_in_polyhedralsurface(void)
Definition: cu_in_wkt.c:313
char * s
Definition: cu_in_wkt.c:23
char * r
Definition: cu_in_wkt.c:24
static void test_wkt_in_multilinestring(void)
Definition: cu_in_wkt.c:187
void wkt_in_suite_setup(void)
Definition: cu_in_wkt.c:395
static void test_wkt_in_circularstring(void)
Definition: cu_in_wkt.c:253
static char * cu_wkt_in(char *wkt, uint8_t variant)
Definition: cu_in_wkt.c:50
static void test_wkt_in_compoundcurve(void)
Definition: cu_in_wkt.c:262
static int init_wkt_in_suite(void)
Definition: cu_in_wkt.c:30
#define PG_ADD_TEST(suite, testfunc)
#define LW_PARSER_CHECK_ALL
Definition: liblwgeom.h:2006
#define WKT_SFSQL
Definition: liblwgeom.h:2076
#define LW_FAILURE
Definition: liblwgeom.h:79
#define WKT_EXTENDED
Definition: liblwgeom.h:2077
#define LW_SUCCESS
Definition: liblwgeom.h:80
void lwgeom_parser_result_init(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:879
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
void lwfree(void *mem)
Definition: lwutil.c:244
#define WKT_ISO
Definition: liblwgeom.h:2075
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:676
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:885
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM.
Definition: liblwgeom.h:2013
unsigned char uint8_t
Definition: uthash.h:79