PostGIS  2.2.8dev-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(1e700 0)";
71  r = cu_wkt_in(s, WKT_SFSQL);
72  CU_TEST ( ! strcmp(r, "POINT(inf 0)") || ! strcmp(r, "POINT(1.#INF 0)") || ! strcmp(r, "POINT(Infinity 0)") );
73  lwfree(r);
74 
75  s = "POINT(0 0)";
76  r = cu_wkt_in(s, WKT_SFSQL);
77  CU_ASSERT_STRING_EQUAL(r,s);
78  lwfree(r);
79 
80  s = "POINT EMPTY";
81  r = cu_wkt_in(s, WKT_SFSQL);
82  CU_ASSERT_STRING_EQUAL(r,s);
83  lwfree(r);
84 
85  s = "POINT M EMPTY";
86  r = cu_wkt_in(s, WKT_ISO);
87  CU_ASSERT_STRING_EQUAL(r,s);
88  lwfree(r);
89 
90  //printf("\nIN: %s\nOUT: %s\n",s,r);
91 }
92 
93 static void test_wkt_in_linestring(void)
94 {
95  s = "LINESTRING EMPTY";
96  r = cu_wkt_in(s, WKT_SFSQL);
97  CU_ASSERT_STRING_EQUAL(r,s);
98  lwfree(r);
99 
100  s = "LINESTRING(0 0,1 1)";
101  r = cu_wkt_in(s, WKT_SFSQL);
102  CU_ASSERT_STRING_EQUAL(r,s);
103  lwfree(r);
104 
105  s = "LINESTRING(0 0 0,1 1 1)";
107  CU_ASSERT_STRING_EQUAL(r,s);
108  lwfree(r);
109 
110  s = "LINESTRING M (0 0 0,1 1 1)";
111  r = cu_wkt_in(s, WKT_ISO);
112  CU_ASSERT_STRING_EQUAL(r,s);
113  lwfree(r);
114 
115  s = "LINESTRING ZM (0 0 0 1,1 1 1 1,2 2 2 2,0.141231 4 5 4)";
116  r = cu_wkt_in(s, WKT_ISO);
117  CU_ASSERT_STRING_EQUAL(r,s);
118  lwfree(r);
119 
120  s = "LINESTRINGM(0 0 0,1 1 1)";
122  CU_ASSERT_STRING_EQUAL(r,s);
123  lwfree(r);
124 
125  s = "LINESTRING ZM EMPTY";
126  r = cu_wkt_in(s, WKT_ISO);
127  CU_ASSERT_STRING_EQUAL(r,s);
128  lwfree(r);
129 
130  s = "LINESTRING Z (0 0 0 1, 0 1 0 1)";
132  CU_ASSERT_STRING_EQUAL(r,"can not mix dimensionality in a geometry");
133  //printf("\nIN: %s\nOUT: %s\n",s,r);
134  lwfree(r);
135 
136 }
137 
138 static void test_wkt_in_polygon(void)
139 {
140  s = "POLYGON((0 0,0 1,1 1,0 0))";
141  r = cu_wkt_in(s, WKT_SFSQL);
142  CU_ASSERT_STRING_EQUAL(r,s);
143  lwfree(r);
144 
145  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))";
146  r = cu_wkt_in(s, WKT_SFSQL);
147  CU_ASSERT_STRING_EQUAL(r,"can not mix dimensionality in a geometry");
148  lwfree(r);
149 
150  s = "POLYGON Z ((0 0,0 10,10 10,10 0,0 0),(1 1,1 2,2 2,2 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  //printf("\nIN: %s\nOUT: %s\n",s,r);
154  lwfree(r);
155 }
156 
157 static void test_wkt_in_multipoint(void)
158 {
163  // 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)";
164  // r = cu_wkt_in(s, WKT_EXTENDED);
165  // CU_ASSERT_STRING_EQUAL(r,s);
166  // printf("\nIN: %s\nOUT: %s\n",s,r);
167  // lwfree(r);
168 
169  s = "MULTIPOINT(0 0)";
170  r = cu_wkt_in(s, WKT_SFSQL);
171  CU_ASSERT_STRING_EQUAL(r,s);
172  //printf("\nIN: %s\nOUT: %s\n",s,r);
173  lwfree(r);
174 
175  s = "MULTIPOINT(0 0,1 1)";
176  r = cu_wkt_in(s, WKT_SFSQL);
177  CU_ASSERT_STRING_EQUAL(r,s);
178  //printf("\nIN: %s\nOUT: %s\n",s,r);
179  lwfree(r);
180 }
181 
183 {
184  s = "MULTILINESTRING((0 0,1 1),(1 1,2 2),(3 3,3 3,3 3,2 2,2 1))";
185  r = cu_wkt_in(s, WKT_SFSQL);
186  CU_ASSERT_STRING_EQUAL(r,s);
187  //printf("\nIN: %s\nOUT: %s\n",s,r);
188  lwfree(r);
189 
190 }
191 
192 static void test_wkt_in_multipolygon(void)
193 {
194  s = "MULTIPOLYGON(((0 0,0 1,1 1,0 0)))";
195  r = cu_wkt_in(s, WKT_SFSQL);
196  CU_ASSERT_STRING_EQUAL(r,s);
197  //printf("\nIN: %s\nOUT: %s\n",s,r);
198  lwfree(r);
199 
200  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)))";
201  r = cu_wkt_in(s, WKT_SFSQL);
202  CU_ASSERT_STRING_EQUAL(r,s);
203  //printf("\nIN: %s\nOUT: %s\n",s,r);
204  lwfree(r);
205 
206  s = "SRID=4;MULTIPOLYGON(((0 0,0 1,1 1,0 0)))";
208  CU_ASSERT_STRING_EQUAL(r,s);
209  //printf("\nIN: %s\nOUT: %s\n",s,r);
210  lwfree(r);
211 
212 }
213 
214 static void test_wkt_in_collection(void)
215 {
216  s = "SRID=5;GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(1 0,0 0),CIRCULARSTRING(0 0,0 1,1 1,0 1,2 2))";
218  //printf("\nIN: %s\nOUT: %s\n",s,r);
219  CU_ASSERT_STRING_EQUAL(r,s);
220  lwfree(r);
221 
222  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))";
223  r = cu_wkt_in(s, WKT_SFSQL);
224  //printf("\nIN: %s\nOUT: %s\n",s,r);
225  CU_ASSERT_STRING_EQUAL(r,s);
226  lwfree(r);
227 
228  s = "GEOMETRYCOLLECTION Z (POINT Z (0 0 0))";
229  r = cu_wkt_in(s, WKT_ISO);
230  //printf("\nIN: %s\nOUT: %s\n",s,r);
231  CU_ASSERT_STRING_EQUAL(r,s);
232  lwfree(r);
233 
234  s = "GEOMETRYCOLLECTION M (MULTILINESTRING M ((0 0 5,2 0 5),(1 1 5,2 2 5)))";
235  r = cu_wkt_in(s, WKT_ISO);
236  //printf("\nIN: %s\nOUT: %s\n",s,r);
237  CU_ASSERT_STRING_EQUAL(r,s);
238  lwfree(r);
239 
240  /* See http://trac.osgeo.org/postgis/ticket/1455#comment:3 */
241  s = "GEOMETRYCOLLECTION Z (MULTILINESTRING Z ((0 0 5,2 0 5),(1 1 5,2 2 5)))";
242  r = cu_wkt_in(s, WKT_ISO);
243  //printf("\nIN: %s\nOUT: %s\n",s,r);
244  CU_ASSERT_STRING_EQUAL(r,s);
245  lwfree(r);
246 }
247 
248 static void test_wkt_in_circularstring(void)
249 {
250  s = "CIRCULARSTRING(0 0,0 1,1 1,0 1,2 2)";
251  r = cu_wkt_in(s, WKT_SFSQL);
252  //printf("\nIN: %s\nOUT: %s\n",s,r);
253  CU_ASSERT_STRING_EQUAL(r,s);
254  lwfree(r);
255 }
256 
257 static void test_wkt_in_compoundcurve(void)
258 {
259  s = "SRID=4326;COMPOUNDCURVEM(CIRCULARSTRINGM(0 0 2,1 1 2,1 0 2),(1 0 2,0 1 2))";
261  CU_ASSERT_STRING_EQUAL(r,s);
262  //printf("\nIN: %s\nOUT: %s\n",s,r);
263  lwfree(r);
264 
265  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))";
266  r = cu_wkt_in(s, WKT_ISO);
267  CU_ASSERT_STRING_EQUAL(r,s);
268  //printf("\nIN: %s\nOUT: %s\n",s,r);
269  lwfree(r);
270 }
271 
272 static void test_wkt_in_curvpolygon(void)
273 {
274  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))";
275  r = cu_wkt_in(s, WKT_ISO);
276  CU_ASSERT_STRING_EQUAL(r,s);
277  //printf("\nIN: %s\nOUT: %s\n",s,r);
278  lwfree(r);
279 }
280 
281 static void test_wkt_in_multicurve(void)
282 {
283  s = "SRID=4326;MULTICURVE(COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1)))";
285  CU_ASSERT_STRING_EQUAL(r,s);
286  //printf("\nIN: %s\nOUT: %s\n",s,r);
287  lwfree(r);
288 }
289 
290 static void test_wkt_in_multisurface(void)
291 {
292  s = "SRID=4326;MULTICURVE(COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1)))";
294  CU_ASSERT_STRING_EQUAL(r,s);
295  //printf("\nIN: %s\nOUT: %s\n",s,r);
296  lwfree(r);
297 }
298 
299 static void test_wkt_in_tin(void)
300 {
301  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)))";
303  CU_ASSERT_STRING_EQUAL(r,"triangle must have exactly 4 points");
304  //printf("\nIN: %s\nOUT: %s\n",s,r);
305  lwfree(r);
306 }
307 
309 {
310  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)))";
312  CU_ASSERT_STRING_EQUAL(r,s);
313  //printf("\nIN: %s\nOUT: %s\n",s,r);
314  lwfree(r);
315 
316  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)))";
317  r = cu_wkt_in(s, WKT_ISO);
318  CU_ASSERT_STRING_EQUAL(r,s);
319  //printf("\nIN: %s\nOUT: %s\n",s,r);
320  lwfree(r);
321 
322  s = "POLYHEDRALSURFACE(((0 1 2,3 4 5,6 7,0 1 2)))";
323  r = cu_wkt_in(s, WKT_ISO);
324  CU_ASSERT_STRING_EQUAL(r,"can not mix dimensionality in a geometry");
325  //printf("\nIN: %s\nOUT: %s\n",s,r);
326  lwfree(r);
327 
328 }
329 
330 static void test_wkt_in_errlocation(void)
331 {
333  int rv = 0;
334  char *wkt = 0;
335 
336  wkt = "LINESTRING((0 0 0,1 1)";
338  rv = lwgeom_parse_wkt(&p, wkt, LW_PARSER_CHECK_ALL);
339  CU_ASSERT_EQUAL( rv, LW_FAILURE );
340  CU_ASSERT((12 - p.errlocation) < 1.5);
341 
342 // printf("errlocation %d\n", p.errlocation);
343 // printf("message %s\n", p.message);
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:214
uint8_t variant
Definition: cu_in_twkb.c:26
static void test_wkt_in_curvpolygon(void)
Definition: cu_in_wkt.c:272
char * r
Definition: cu_in_wkt.c:24
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:214
#define LW_SUCCESS
Definition: liblwgeom.h:65
static void test_wkt_in_multipoint(void)
Definition: cu_in_wkt.c:157
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:871
static void test_wkt_in_multilinestring(void)
Definition: cu_in_wkt.c:182
static int clean_wkt_in_suite(void)
Definition: cu_in_wkt.c:39
void wkt_in_suite_setup(void)
Definition: cu_in_wkt.c:353
#define LW_FAILURE
Definition: liblwgeom.h:64
static void test_wkt_in_polyhedralsurface(void)
Definition: cu_in_wkt.c:308
#define WKT_ISO
Definition: liblwgeom.h:1939
static void test_wkt_in_multicurve(void)
Definition: cu_in_wkt.c:281
static void test_wkt_in_multipolygon(void)
Definition: cu_in_wkt.c:192
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:1876
#define PG_ADD_TEST(suite, testfunc)
static void test_wkt_in_errlocation(void)
Definition: cu_in_wkt.c:330
static void test_wkt_in_linestring(void)
Definition: cu_in_wkt.c:93
char * s
Definition: cu_in_wkt.c:23
static void test_wkt_in_multisurface(void)
Definition: cu_in_wkt.c:290
#define WKT_EXTENDED
Definition: liblwgeom.h:1941
static void test_wkt_in_polygon(void)
Definition: cu_in_wkt.c:138
#define WKT_SFSQL
Definition: liblwgeom.h:1940
static void test_wkt_in_point(void)
Definition: cu_in_wkt.c:68
void lwgeom_parser_result_init(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:865
static void test_wkt_in_compoundcurve(void)
Definition: cu_in_wkt.c:257
#define LW_PARSER_CHECK_ALL
Definition: liblwgeom.h:1870
static void test_wkt_in_circularstring(void)
Definition: cu_in_wkt.c:248
static void test_wkt_in_tin(void)
Definition: cu_in_wkt.c:299
static char * cu_wkt_in(char *wkt, uint8_t variant)
Definition: cu_in_wkt.c:50
static int init_wkt_in_suite(void)
Definition: cu_in_wkt.c:30