PostGIS  2.1.10dev-r@@SVN_REVISION@@
cu_in_wkt.c
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id: cu_out_wkt.c 6036 2010-10-03 18:14:35Z pramsey $
3  *
4  * PostGIS - Spatial Types for PostgreSQL
5  * http://postgis.net
6  * Copyright 2010 Paul Ramsey <pramsey@cleverelephant.ca>
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 <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include "CUnit/Basic.h"
17 
18 #include "liblwgeom_internal.h"
19 #include "cu_tester.h"
20 
21 /*
22 ** Globals used by tests
23 */
24 char *s;
25 char *r;
26 
27 /*
28 ** The suite initialization function.
29 ** Create any re-used objects.
30 */
31 static int init_wkt_in_suite(void)
32 {
33  return 0;
34 }
35 
36 /*
37 ** The suite cleanup function.
38 ** Frees any global objects.
39 */
40 static int clean_wkt_in_suite(void)
41 {
42  return 0;
43 }
44 
45 /*
46 * Return a char* that results from taking the input
47 * WKT, creating an LWGEOM, then writing it back out
48 * as an output WKT using the supplied variant.
49 * If there is an error, return that.
50 */
51 static char* cu_wkt_in(char *wkt, uint8_t variant)
52 {
54  int rv = 0;
55  char *s = 0;
56 
57  rv = lwgeom_parse_wkt(&p, wkt, 0);
58  if( p.errcode ) {
59  return strdup(p.message);
60  }
61  s = lwgeom_to_wkt(p.geom, variant, 8, NULL);
63  return s;
64 }
65 
66 
67 static void test_wkt_in_point(void)
68 {
69  s = "POINT(1e700 0)";
70  r = cu_wkt_in(s, WKT_SFSQL);
71  CU_TEST ( ! strcmp(r, "POINT(inf 0)") || ! strcmp(r, "POINT(1.#INF 0)") );
72  lwfree(r);
73 
74  s = "POINT(0 0)";
75  r = cu_wkt_in(s, WKT_SFSQL);
76  CU_ASSERT_STRING_EQUAL(r,s);
77  lwfree(r);
78 
79  s = "POINT EMPTY";
80  r = cu_wkt_in(s, WKT_SFSQL);
81  CU_ASSERT_STRING_EQUAL(r,s);
82  lwfree(r);
83 
84  s = "POINT M EMPTY";
85  r = cu_wkt_in(s, WKT_ISO);
86  CU_ASSERT_STRING_EQUAL(r,s);
87  lwfree(r);
88 
89  //printf("\nIN: %s\nOUT: %s\n",s,r);
90 }
91 
92 static void test_wkt_in_linestring(void)
93 {
94  s = "LINESTRING EMPTY";
95  r = cu_wkt_in(s, WKT_SFSQL);
96  CU_ASSERT_STRING_EQUAL(r,s);
97  lwfree(r);
98 
99  s = "LINESTRING(0 0,1 1)";
100  r = cu_wkt_in(s, WKT_SFSQL);
101  CU_ASSERT_STRING_EQUAL(r,s);
102  lwfree(r);
103 
104  s = "LINESTRING(0 0 0,1 1 1)";
106  CU_ASSERT_STRING_EQUAL(r,s);
107  lwfree(r);
108 
109  s = "LINESTRING M (0 0 0,1 1 1)";
110  r = cu_wkt_in(s, WKT_ISO);
111  CU_ASSERT_STRING_EQUAL(r,s);
112  lwfree(r);
113 
114  s = "LINESTRING ZM (0 0 0 1,1 1 1 1,2 2 2 2,0.141231 4 5 4)";
115  r = cu_wkt_in(s, WKT_ISO);
116  CU_ASSERT_STRING_EQUAL(r,s);
117  lwfree(r);
118 
119  s = "LINESTRINGM(0 0 0,1 1 1)";
121  CU_ASSERT_STRING_EQUAL(r,s);
122  lwfree(r);
123 
124  s = "LINESTRING ZM EMPTY";
125  r = cu_wkt_in(s, WKT_ISO);
126  CU_ASSERT_STRING_EQUAL(r,s);
127  lwfree(r);
128 
129  s = "LINESTRING Z (0 0 0 1, 0 1 0 1)";
131  CU_ASSERT_STRING_EQUAL(r,"can not mix dimensionality in a geometry");
132  //printf("\nIN: %s\nOUT: %s\n",s,r);
133  lwfree(r);
134 
135 }
136 
137 static void test_wkt_in_polygon(void)
138 {
139  s = "POLYGON((0 0,0 1,1 1,0 0))";
140  r = cu_wkt_in(s, WKT_SFSQL);
141  CU_ASSERT_STRING_EQUAL(r,s);
142  lwfree(r);
143 
144  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))";
145  r = cu_wkt_in(s, WKT_SFSQL);
146  CU_ASSERT_STRING_EQUAL(r,"can not mix dimensionality in a geometry");
147  lwfree(r);
148 
149  s = "POLYGON Z ((0 0,0 10,10 10,10 0,0 0),(1 1,1 2,2 2,2 1,1 1))";
150  r = cu_wkt_in(s, WKT_SFSQL);
151  CU_ASSERT_STRING_EQUAL(r,"can not mix dimensionality in a geometry");
152  //printf("\nIN: %s\nOUT: %s\n",s,r);
153  lwfree(r);
154 }
155 
156 static void test_wkt_in_multipoint(void)
157 {
162  // 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)";
163  // r = cu_wkt_in(s, WKT_EXTENDED);
164  // CU_ASSERT_STRING_EQUAL(r,s);
165  // printf("\nIN: %s\nOUT: %s\n",s,r);
166  // lwfree(r);
167 
168  s = "MULTIPOINT(0 0)";
169  r = cu_wkt_in(s, WKT_SFSQL);
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,1 1)";
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 
182 {
183  s = "MULTILINESTRING((0 0,1 1),(1 1,2 2),(3 3,3 3,3 3,2 2,2 1))";
184  r = cu_wkt_in(s, WKT_SFSQL);
185  CU_ASSERT_STRING_EQUAL(r,s);
186  //printf("\nIN: %s\nOUT: %s\n",s,r);
187  lwfree(r);
188 
189 }
190 
191 static void test_wkt_in_multipolygon(void)
192 {
193  s = "MULTIPOLYGON(((0 0,0 1,1 1,0 0)))";
194  r = cu_wkt_in(s, WKT_SFSQL);
195  CU_ASSERT_STRING_EQUAL(r,s);
196  //printf("\nIN: %s\nOUT: %s\n",s,r);
197  lwfree(r);
198 
199  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)))";
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 = "SRID=4;MULTIPOLYGON(((0 0,0 1,1 1,0 0)))";
207  CU_ASSERT_STRING_EQUAL(r,s);
208  //printf("\nIN: %s\nOUT: %s\n",s,r);
209  lwfree(r);
210 
211 }
212 
213 static void test_wkt_in_collection(void)
214 {
215  s = "SRID=5;GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(1 0,0 0),CIRCULARSTRING(0 0,0 1,1 1,0 1,2 2))";
217  //printf("\nIN: %s\nOUT: %s\n",s,r);
218  CU_ASSERT_STRING_EQUAL(r,s);
219  lwfree(r);
220 
221  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))";
222  r = cu_wkt_in(s, WKT_SFSQL);
223  //printf("\nIN: %s\nOUT: %s\n",s,r);
224  CU_ASSERT_STRING_EQUAL(r,s);
225  lwfree(r);
226 
227  s = "GEOMETRYCOLLECTION Z (POINT Z (0 0 0))";
228  r = cu_wkt_in(s, WKT_ISO);
229  //printf("\nIN: %s\nOUT: %s\n",s,r);
230  CU_ASSERT_STRING_EQUAL(r,s);
231  lwfree(r);
232 
233  s = "GEOMETRYCOLLECTION M (MULTILINESTRING M ((0 0 5,2 0 5),(1 1 5,2 2 5)))";
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  /* See http://trac.osgeo.org/postgis/ticket/1455#comment:3 */
240  s = "GEOMETRYCOLLECTION Z (MULTILINESTRING Z ((0 0 5,2 0 5),(1 1 5,2 2 5)))";
241  r = cu_wkt_in(s, WKT_ISO);
242  //printf("\nIN: %s\nOUT: %s\n",s,r);
243  CU_ASSERT_STRING_EQUAL(r,s);
244  lwfree(r);
245 }
246 
247 static void test_wkt_in_circularstring(void)
248 {
249  s = "CIRCULARSTRING(0 0,0 1,1 1,0 1,2 2)";
250  r = cu_wkt_in(s, WKT_SFSQL);
251  //printf("\nIN: %s\nOUT: %s\n",s,r);
252  CU_ASSERT_STRING_EQUAL(r,s);
253  lwfree(r);
254 }
255 
256 static void test_wkt_in_compoundcurve(void)
257 {
258  s = "SRID=4326;COMPOUNDCURVEM(CIRCULARSTRINGM(0 0 2,1 1 2,1 0 2),(1 0 2,0 1 2))";
260  CU_ASSERT_STRING_EQUAL(r,s);
261  //printf("\nIN: %s\nOUT: %s\n",s,r);
262  lwfree(r);
263 
264  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))";
265  r = cu_wkt_in(s, WKT_ISO);
266  CU_ASSERT_STRING_EQUAL(r,s);
267  //printf("\nIN: %s\nOUT: %s\n",s,r);
268  lwfree(r);
269 }
270 
271 static void test_wkt_in_curvpolygon(void)
272 {
273  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))";
274  r = cu_wkt_in(s, WKT_ISO);
275  CU_ASSERT_STRING_EQUAL(r,s);
276  //printf("\nIN: %s\nOUT: %s\n",s,r);
277  lwfree(r);
278 }
279 
280 static void test_wkt_in_multicurve(void)
281 {
282  s = "SRID=4326;MULTICURVE(COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1)))";
284  CU_ASSERT_STRING_EQUAL(r,s);
285  //printf("\nIN: %s\nOUT: %s\n",s,r);
286  lwfree(r);
287 }
288 
289 static void test_wkt_in_multisurface(void)
290 {
291  s = "SRID=4326;MULTICURVE(COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1)))";
293  CU_ASSERT_STRING_EQUAL(r,s);
294  //printf("\nIN: %s\nOUT: %s\n",s,r);
295  lwfree(r);
296 }
297 
298 static void test_wkt_in_tin(void)
299 {
300  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)))";
302  CU_ASSERT_STRING_EQUAL(r,"triangle must have exactly 4 points");
303  //printf("\nIN: %s\nOUT: %s\n",s,r);
304  lwfree(r);
305 }
306 
308 {
309  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)))";
311  CU_ASSERT_STRING_EQUAL(r,s);
312  //printf("\nIN: %s\nOUT: %s\n",s,r);
313  lwfree(r);
314 
315  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)))";
316  r = cu_wkt_in(s, WKT_ISO);
317  CU_ASSERT_STRING_EQUAL(r,s);
318  //printf("\nIN: %s\nOUT: %s\n",s,r);
319  lwfree(r);
320 
321  s = "POLYHEDRALSURFACE(((0 1 2,3 4 5,6 7,0 1 2)))";
322  r = cu_wkt_in(s, WKT_ISO);
323  CU_ASSERT_STRING_EQUAL(r,"can not mix dimensionality in a geometry");
324  //printf("\nIN: %s\nOUT: %s\n",s,r);
325  lwfree(r);
326 
327 }
328 
329 static void test_wkt_in_errlocation(void)
330 {
332  int rv = 0;
333  char *wkt = 0;
334 
335  wkt = "LINESTRING((0 0 0,1 1)";
337  rv = lwgeom_parse_wkt(&p, wkt, LW_PARSER_CHECK_ALL);
338  if ( fabs(12 - p.errlocation) >= 1.5 ) {
339  printf("Unexpected errlocation: %d\n", p.errlocation);
340  printf(" message: %s\n", p.message);
341  }
342  CU_ASSERT(fabs(12 - p.errlocation) < 1.5);
343 
344 
346 
347 }
348 
349 /*
350 ** Used by test harness to register the tests in this file.
351 */
352 void wkt_in_suite_setup(void);
354 {
355  CU_pSuite suite = CU_add_suite("WKT Input", init_wkt_in_suite, clean_wkt_in_suite);
371 }
static void test_wkt_in_collection(void)
Definition: cu_in_wkt.c:213
static void test_wkt_in_curvpolygon(void)
Definition: cu_in_wkt.c:271
char * r
Definition: cu_in_wkt.c:25
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:655
void lwfree(void *mem)
Definition: lwutil.c:190
static void test_wkt_in_multipoint(void)
Definition: cu_in_wkt.c:156
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:825
static void test_wkt_in_multilinestring(void)
Definition: cu_in_wkt.c:181
static int clean_wkt_in_suite(void)
Definition: cu_in_wkt.c:40
void wkt_in_suite_setup(void)
Definition: cu_in_wkt.c:353
static void test_wkt_in_polyhedralsurface(void)
Definition: cu_in_wkt.c:307
#define WKT_ISO
Definition: liblwgeom.h:1776
static void test_wkt_in_multicurve(void)
Definition: cu_in_wkt.c:280
static void test_wkt_in_multipolygon(void)
Definition: cu_in_wkt.c:191
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM...
Definition: liblwgeom.h:1713
#define PG_ADD_TEST(suite, testfunc)
static void test_wkt_in_errlocation(void)
Definition: cu_in_wkt.c:329
static void test_wkt_in_linestring(void)
Definition: cu_in_wkt.c:92
char * s
Definition: cu_in_wkt.c:24
static void test_wkt_in_multisurface(void)
Definition: cu_in_wkt.c:289
#define WKT_EXTENDED
Definition: liblwgeom.h:1778
static void test_wkt_in_polygon(void)
Definition: cu_in_wkt.c:137
#define WKT_SFSQL
Definition: liblwgeom.h:1777
static void test_wkt_in_point(void)
Definition: cu_in_wkt.c:67
void lwgeom_parser_result_init(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:819
static void test_wkt_in_compoundcurve(void)
Definition: cu_in_wkt.c:256
#define LW_PARSER_CHECK_ALL
Definition: liblwgeom.h:1707
static void test_wkt_in_circularstring(void)
Definition: cu_in_wkt.c:247
static void test_wkt_in_tin(void)
Definition: cu_in_wkt.c:298
static char * cu_wkt_in(char *wkt, uint8_t variant)
Definition: cu_in_wkt.c:51
static int init_wkt_in_suite(void)
Definition: cu_in_wkt.c:31