PostGIS  2.1.10dev-r@@SVN_REVISION@@
cu_in_geojson.c
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id$
3  *
4  * PostGIS - Spatial Types for PostgreSQL
5  * http://postgis.net
6  *
7  * Copyright 2013 Sandro Santilli <strk@keybit.net>
8  *
9  * This is free software; you can redistribute and/or modify it under
10  * the terms of the GNU General Public Licence. See the COPYING file.
11  *
12  **********************************************************************/
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "CUnit/Basic.h"
18 
19 #include "liblwgeom_internal.h"
20 #include "cu_tester.h"
21 
22 static void do_geojson_test(const char * exp, char * in, char * exp_srs, int precision, int has_bbox)
23 {
24  LWGEOM *g;
25  char * h = NULL;
26  char * srs = NULL;
27  size_t size;
28 
29  g = lwgeom_from_geojson(in, &srs);
30  if ( ! g ) {
31  fprintf(stderr, "\nIn: %s\nExp: %s\nObt: %s\n", in, exp, cu_error_msg);
32  CU_ASSERT(g != NULL);
33  return;
34  }
35 
36  h = lwgeom_to_wkt(g, WKT_EXTENDED, 15, &size);
37 
38  if (strcmp(h, exp)) {
39  fprintf(stderr, "\nIn: %s\nExp: %s\nObt: %s\n", in, exp, h);
40  CU_ASSERT_STRING_EQUAL(h, exp);
41  }
42 
43  if ( exp_srs ) {
44  if ( ! srs ) {
45  fprintf(stderr, "\nIn: %s\nExp: %s\nObt: (null)\n", in, exp_srs);
46  CU_ASSERT_EQUAL(srs, exp_srs);
47  }
48  else if (strcmp(srs, exp_srs)) {
49  fprintf(stderr, "\nIn: %s\nExp: %s\nObt: %s\n", in, exp_srs, srs);
50  CU_ASSERT_STRING_EQUAL(srs, exp_srs);
51  }
52  } else if ( srs ) {
53  fprintf(stderr, "\nIn: %s\nExp: (null)\nObt: %s\n", in, srs);
54  CU_ASSERT_EQUAL(srs, exp_srs);
55  }
56 
57  lwgeom_free(g);
58  if ( h ) lwfree(h);
59  if ( srs ) lwfree(srs);
60 }
61 
62 #if 0
63 static void do_geojson_unsupported(char * in, char * exp)
64 {
65  LWGEOM *g;
66  char * h = NULL;
67  char * srs = NULL;
68  size_t size;
69 
70  g = lwgeom_from_geojson(in, &srs);
71 
72  if ( g ) {
73  h = lwgeom_to_wkt(g, WKT_ISO, 1, &size);
74  fprintf(stderr, "\nIn: %s\nExp: %s\nObt: %s\n",
75  in, exp, h);
76  CU_ASSERT(!g);
77  } else {
78 
79  if (strcmp(cu_error_msg, exp))
80  fprintf(stderr, "\nIn: %s\nExp: %s\nObt: %s\n",
81  in, exp, cu_error_msg);
82  CU_ASSERT_STRING_EQUAL(in, cu_error_msg);
83  }
84 
86 
87  if ( srs ) lwfree(srs);
88  if ( h ) lwfree(h);
89  lwgeom_free(g);
90 }
91 #endif
92 
93 static void in_geojson_test_srid(void)
94 {
95  /* Linestring */
97  "LINESTRING(0 1,2 3,4 5)",
98  "{\"type\":\"LineString\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[0,1],[2,3],[4,5]]}",
99  "EPSG:4326", 0, 0);
100 
101  /* Polygon */
103  "POLYGON((0 1,2 3,4 5,0 1))",
104  "{\"type\":\"Polygon\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
105  "EPSG:4326", 0, 0);
106 
107  /* Polygon - with internal ring */
109  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
110  "{\"type\":\"Polygon\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]],[[6,7],[8,9],[10,11],[6,7]]]}",
111  "EPSG:4326", 0, 0);
112 
113  /* Multiline */
115  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
116  "{\"type\":\"MultiLineString\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
117  "EPSG:4326", 0, 0);
118 
119  /* MultiPolygon */
121  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
122  "{\"type\":\"MultiPolygon\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[[[0,1],[2,3],[4,5],[0,1]]],[[[6,7],[8,9],[10,11],[6,7]]]]}",
123  "EPSG:4326", 0, 0);
124 
125  /* Empty GeometryCollection */
127  "GEOMETRYCOLLECTION EMPTY",
128  "{\"type\":\"GeometryCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"geometries\":[]}",
129  "EPSG:4326", 0, 0);
130 }
131 
132 static void in_geojson_test_bbox(void)
133 {
134  /* Linestring */
136  "LINESTRING(0 1,2 3,4 5)",
137  "{\"type\":\"LineString\",\"bbox\":[0,1,4,5],\"coordinates\":[[0,1],[2,3],[4,5]]}",
138  NULL, 0, 1);
139 
140  /* Polygon */
142  "POLYGON((0 1,2 3,4 5,0 1))",
143  "{\"type\":\"Polygon\",\"bbox\":[0,1,4,5],\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
144  NULL, 0, 1);
145 
146  /* Polygon - with internal ring */
148  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
149  "{\"type\":\"Polygon\",\"bbox\":[0,1,4,5],\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]],[[6,7],[8,9],[10,11],[6,7]]]}",
150  NULL, 0, 1);
151 
152  /* Multiline */
154  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
155  "{\"type\":\"MultiLineString\",\"bbox\":[0,1,10,11],\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
156  NULL, 0, 1);
157 
158  /* MultiPolygon */
160  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
161  "{\"type\":\"MultiPolygon\",\"bbox\":[0,1,10,11],\"coordinates\":[[[[0,1],[2,3],[4,5],[0,1]]],[[[6,7],[8,9],[10,11],[6,7]]]]}",
162  NULL, 0, 1);
163 
164  /* GeometryCollection */
166  "GEOMETRYCOLLECTION(LINESTRING(0 1,-1 3),LINESTRING(2 3,4 5))",
167  "{\"type\":\"GeometryCollection\",\"bbox\":[-1,1,4,5],\"geometries\":[{\"type\":\"LineString\",\"coordinates\":[[0,1],[-1,3]]},{\"type\":\"LineString\",\"coordinates\":[[2,3],[4,5]]}]}",
168  NULL, 0, 1);
169 
170  /* Empty GeometryCollection */
172  "GEOMETRYCOLLECTION EMPTY",
173  "{\"type\":\"GeometryCollection\",\"geometries\":[]}",
174  NULL, 0, 1);
175 }
176 
177 static void in_geojson_test_geoms(void)
178 {
179  /* Linestring */
181  "LINESTRING(0 1,2 3,4 5)",
182  "{\"type\":\"LineString\",\"coordinates\":[[0,1],[2,3],[4,5]]}",
183  NULL, 0, 0);
184 
185  /* Polygon */
187  "POLYGON((0 1,2 3,4 5,0 1))",
188  "{\"type\":\"Polygon\",\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
189  NULL, 0, 0);
190 
191  /* Polygon - with internal ring */
193  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
194  "{\"type\":\"Polygon\",\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]],[[6,7],[8,9],[10,11],[6,7]]]}",
195  NULL, 0, 0);
196 
197  /* Multiline */
199  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
200  "{\"type\":\"MultiLineString\",\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
201  NULL, 0, 0);
202 
203  /* MultiPolygon */
205  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
206  "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0,1],[2,3],[4,5],[0,1]]],[[[6,7],[8,9],[10,11],[6,7]]]]}",
207  NULL, 0, 0);
208 
209  /* MultiPolygon with internal rings */
210  /* See http://trac.osgeo.org/postgis/ticket/2216 */
212  "MULTIPOLYGON(((4 0,0 -4,-4 0,0 4,4 0),(2 0,0 2,-2 0,0 -2,2 0)),((24 0,20 -4,16 0,20 4,24 0),(22 0,20 2,18 0,20 -2,22 0)),((44 0,40 -4,36 0,40 4,44 0),(42 0,40 2,38 0,40 -2,42 0)))",
213  "{'type':'MultiPolygon','coordinates':[[[[4,0],[0,-4],[-4,0],[0,4],[4,0]],[[2,0],[0,2],[-2,0],[0,-2],[2,0]]],[[[24,0],[20,-4],[16,0],[20,4],[24,0]],[[22,0],[20,2],[18,0],[20,-2],[22,0]]],[[[44,0],[40,-4],[36,0],[40,4],[44,0]],[[42,0],[40,2],[38,0],[40,-2],[42,0]]]]}",
214  NULL, 0, 0);
215 
216  /* GeometryCollection */
218  "GEOMETRYCOLLECTION(POINT(0 1),LINESTRING(2 3,4 5))",
219  "{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"Point\",\"coordinates\":[0,1]},{\"type\":\"LineString\",\"coordinates\":[[2,3],[4,5]]}]}",
220  NULL, 0, 0);
221 
222  /* Empty GeometryCollection */
224  "GEOMETRYCOLLECTION EMPTY",
225  "{\"type\":\"GeometryCollection\",\"geometries\":[]}",
226  NULL, 0, 0);
227 
228 }
229 
230 /*
231 ** Used by test harness to register the tests in this file.
232 */
233 void in_geojson_suite_setup(void);
235 {
236  CU_pSuite suite = CU_add_suite("GeoJSON Input", NULL, NULL);
240 }
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 in_geojson_test_bbox(void)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
static void in_geojson_test_srid(void)
Definition: cu_in_geojson.c:93
static void do_geojson_test(const char *exp, char *in, char *exp_srs, int precision, int has_bbox)
Definition: cu_in_geojson.c:22
#define WKT_ISO
Definition: liblwgeom.h:1776
static void in_geojson_test_geoms(void)
void cu_error_msg_reset()
#define PG_ADD_TEST(suite, testfunc)
static void do_geojson_unsupported(char *in, char *out)
#define WKT_EXTENDED
Definition: liblwgeom.h:1778
void in_geojson_suite_setup(void)
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
LWGEOM * lwgeom_from_geojson(const char *geojson, char **srs)
Create an LWGEOM object from a GeoJSON representation.
Definition: lwin_geojson.c:510