PostGIS  2.1.10dev-r@@SVN_REVISION@@
cu_out_geojson.c
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id: cu_out_geojson.c 13242 2015-02-19 00:22:22Z pramsey $
3  *
4  * PostGIS - Spatial Types for PostgreSQL
5  * http://postgis.net
6  * Copyright 2010 Olivier Courtin <olivier.courtin@oslandia.com>
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 static void do_geojson_test(char * in, char * out, char * srs, int precision, int has_bbox)
22 {
23  LWGEOM *g;
24  char * h;
25 
27  h = lwgeom_to_geojson(g, srs, precision, has_bbox);
28 
29  if (strcmp(h, out))
30  fprintf(stderr, "\nIn: %s\nOut: %s\nTheo: %s\n", in, h, out);
31 
32  CU_ASSERT_STRING_EQUAL(h, out);
33 
34  lwgeom_free(g);
35  lwfree(h);
36 }
37 
38 
39 static void do_geojson_unsupported(char * in, char * out)
40 {
41  LWGEOM *g;
42  char *h;
43 
45  h = lwgeom_to_geojson(g, NULL, 0, 0);
46 
47  if (strcmp(cu_error_msg, out))
48  fprintf(stderr, "\nIn: %s\nOut: %s\nTheo: %s\n",
49  in, cu_error_msg, out);
50 
51  CU_ASSERT_STRING_EQUAL(out, cu_error_msg);
53 
54  lwfree(h);
55  lwgeom_free(g);
56 }
57 
58 
59 static void out_geojson_test_precision(void)
60 {
61  /* 0 precision, i.e a round */
63  "POINT(1.1111111111111 1.1111111111111)",
64  "{\"type\":\"Point\",\"coordinates\":[1,1]}",
65  NULL, 0, 0);
66 
67  /* 3 digits precision */
69  "POINT(1.1111111111111 1.1111111111111)",
70  "{\"type\":\"Point\",\"coordinates\":[1.111,1.111]}",
71  NULL, 3, 0);
72 
73  /* 9 digits precision */
75  "POINT(1.2345678901234 1.2345678901234)",
76  "{\"type\":\"Point\",\"coordinates\":[1.23456789,1.23456789]}",
77  NULL, 9, 0);
78 
79  /* huge data */
81  "POINT(1E300 -1E300)",
82  "{\"type\":\"Point\",\"coordinates\":[1e+300,-1e+300]}",
83  NULL, 0, 0);
84 
85  /* huge precision, see http://trac.osgeo.org/postgis/ticket/2052 */
87  "POINT(1 2)",
88  "{\"type\":\"Point\",\"coordinates\":[1,2]}",
89  NULL, 100, 0);
90 
91  /* double precision, see http://trac.osgeo.org/postgis/ticket/2051 */
93  "POINT(59.99 -59.99)",
94  "{\"type\":\"Point\",\"coordinates\":[59.99,-59.99]}",
95  NULL, 15, 0);
96 
97  /* small numbers */
98  /* NOTE: precision of 300 will be converted to max precision (15)
99  * and being there no significant digit within that range
100  * only zeroes will be returned
101  * See http://trac.osgeo.org/postgis/ticket/2051#comment:11
102  */
104  "POINT(1E-300 -2E-200)",
105  "{\"type\":\"Point\",\"coordinates\":[0,-0]}",
106  NULL, 300, 0);
107 
108 }
109 
110 
111 static void out_geojson_test_dims(void)
112 {
113  /* 3D */
115  "POINT(0 1 2)",
116  "{\"type\":\"Point\",\"coordinates\":[0,1,2]}",
117  NULL, 0, 0);
118 
119  /* 3DM */
121  "POINTM(0 1 2)",
122  "{\"type\":\"Point\",\"coordinates\":[0,1]}",
123  NULL, 0, 0);
124 
125  /* 4D */
127  "POINT(0 1 2 3)",
128  "{\"type\":\"Point\",\"coordinates\":[0,1,2]}",
129  NULL, 0, 0);
130 }
131 
132 
133 static void out_geojson_test_srid(void)
134 {
135  /* Linestring */
137  "LINESTRING(0 1,2 3,4 5)",
138  "{\"type\":\"LineString\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[0,1],[2,3],[4,5]]}",
139  "EPSG:4326", 0, 0);
140 
141  /* Polygon */
143  "POLYGON((0 1,2 3,4 5,0 1))",
144  "{\"type\":\"Polygon\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
145  "EPSG:4326", 0, 0);
146 
147  /* Polygon - with internal ring */
149  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
150  "{\"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]]]}",
151  "EPSG:4326", 0, 0);
152 
153  /* Multiline */
155  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
156  "{\"type\":\"MultiLineString\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
157  "EPSG:4326", 0, 0);
158 
159  /* MultiPolygon */
161  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
162  "{\"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]]]]}",
163  "EPSG:4326", 0, 0);
164 
165  /* GeometryCollection */
167  "GEOMETRYCOLLECTION(POINT(0 1),LINESTRING(2 3,4 5))",
168  "{\"type\":\"GeometryCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"geometries\":[{\"type\":\"Point\",\"coordinates\":[0,1]},{\"type\":\"LineString\",\"coordinates\":[[2,3],[4,5]]}]}",
169  "EPSG:4326", 0, 0);
170 
171  /* Empty GeometryCollection */
173  "GEOMETRYCOLLECTION EMPTY",
174  "{\"type\":\"GeometryCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"geometries\":[]}",
175  "EPSG:4326", 0, 0);
176 }
177 
178 static void out_geojson_test_bbox(void)
179 {
180  /* Linestring */
182  "LINESTRING(0 1,2 3,4 5)",
183  "{\"type\":\"LineString\",\"bbox\":[0,1,4,5],\"coordinates\":[[0,1],[2,3],[4,5]]}",
184  NULL, 0, 1);
185 
186  /* Polygon */
188  "POLYGON((0 1,2 3,4 5,0 1))",
189  "{\"type\":\"Polygon\",\"bbox\":[0,1,4,5],\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
190  NULL, 0, 1);
191 
192  /* Polygon - with internal ring */
194  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
195  "{\"type\":\"Polygon\",\"bbox\":[0,1,4,5],\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]],[[6,7],[8,9],[10,11],[6,7]]]}",
196  NULL, 0, 1);
197 
198  /* Multiline */
200  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
201  "{\"type\":\"MultiLineString\",\"bbox\":[0,1,10,11],\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
202  NULL, 0, 1);
203 
204  /* MultiPolygon */
206  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
207  "{\"type\":\"MultiPolygon\",\"bbox\":[0,1,10,11],\"coordinates\":[[[[0,1],[2,3],[4,5],[0,1]]],[[[6,7],[8,9],[10,11],[6,7]]]]}",
208  NULL, 0, 1);
209 
210  /* GeometryCollection */
212  "GEOMETRYCOLLECTION(LINESTRING(0 1,-1 3),LINESTRING(2 3,4 5))",
213  "{\"type\":\"GeometryCollection\",\"bbox\":[-1,1,4,5],\"geometries\":[{\"type\":\"LineString\",\"coordinates\":[[0,1],[-1,3]]},{\"type\":\"LineString\",\"coordinates\":[[2,3],[4,5]]}]}",
214  NULL, 0, 1);
215 
216  /* Empty GeometryCollection */
218  "GEOMETRYCOLLECTION EMPTY",
219  "{\"type\":\"GeometryCollection\",\"geometries\":[]}",
220  NULL, 0, 1);
221 }
222 
223 static void out_geojson_test_geoms(void)
224 {
225  /* Linestring */
227  "LINESTRING(0 1,2 3,4 5)",
228  "{\"type\":\"LineString\",\"coordinates\":[[0,1],[2,3],[4,5]]}",
229  NULL, 0, 0);
230 
231  /* Polygon */
233  "POLYGON((0 1,2 3,4 5,0 1))",
234  "{\"type\":\"Polygon\",\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
235  NULL, 0, 0);
236 
237  /* Polygon - with internal ring */
239  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
240  "{\"type\":\"Polygon\",\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]],[[6,7],[8,9],[10,11],[6,7]]]}",
241  NULL, 0, 0);
242 
243  /* Multiline */
245  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
246  "{\"type\":\"MultiLineString\",\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
247  NULL, 0, 0);
248 
249  /* MultiPolygon */
251  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
252  "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0,1],[2,3],[4,5],[0,1]]],[[[6,7],[8,9],[10,11],[6,7]]]]}",
253  NULL, 0, 0);
254 
255  /* GeometryCollection */
257  "GEOMETRYCOLLECTION(POINT(0 1),LINESTRING(2 3,4 5))",
258  "{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"Point\",\"coordinates\":[0,1]},{\"type\":\"LineString\",\"coordinates\":[[2,3],[4,5]]}]}",
259  NULL, 0, 0);
260 
261  /* Empty GeometryCollection */
263  "GEOMETRYCOLLECTION EMPTY",
264  "{\"type\":\"GeometryCollection\",\"geometries\":[]}",
265  NULL, 0, 0);
266 
267  /* Nested GeometryCollection */
269  "GEOMETRYCOLLECTION(POINT(0 1),GEOMETRYCOLLECTION(LINESTRING(2 3,4 5)))",
270  "GeoJson: geometry not supported.");
271 
272  /* CircularString */
274  "CIRCULARSTRING(-2 0,0 2,2 0,0 2,2 4)",
275  "lwgeom_to_geojson: 'CircularString' geometry type not supported");
276 
277  /* CompoundCurve */
279  "COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1))",
280  "lwgeom_to_geojson: 'CompoundCurve' geometry type not supported");
281 
282  /* CurvePolygon */
284  "CURVEPOLYGON(CIRCULARSTRING(-2 0,-1 -1,0 0,1 -1,2 0,0 2,-2 0),(-1 0,0 0.5,1 0,0 1,-1 0))",
285  "lwgeom_to_geojson: 'CurvePolygon' geometry type not supported");
286 
287  /* MultiCurve */
289  "MULTICURVE((5 5,3 5,3 3,0 3),CIRCULARSTRING(0 0,2 1,2 2))",
290  "lwgeom_to_geojson: 'MultiCurve' geometry type not supported");
291 
292  /* MultiSurface */
294  "MULTISURFACE(CURVEPOLYGON(CIRCULARSTRING(-2 0,-1 -1,0 0,1 -1,2 0,0 2,-2 0),(-1 0,0 0.5,1 0,0 1,-1 0)),((7 8,10 10,6 14,4 11,7 8)))",
295  "lwgeom_to_geojson: 'MultiSurface' geometry type not supported");
296 }
297 
298 /*
299 ** Used by test harness to register the tests in this file.
300 */
301 void out_geojson_suite_setup(void);
303 {
304  CU_pSuite suite = CU_add_suite("GeoJson Output", NULL, NULL);
310 }
void out_geojson_suite_setup(void)
static void do_geojson_test(char *in, char *out, char *srs, int precision, int has_bbox)
static void out_geojson_test_srid(void)
void lwfree(void *mem)
Definition: lwutil.c:190
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:844
static void out_geojson_test_bbox(void)
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:1706
void cu_error_msg_reset()
static void out_geojson_test_dims(void)
#define PG_ADD_TEST(suite, testfunc)
char * lwgeom_to_geojson(const LWGEOM *geo, char *srs, int precision, int has_bbox)
Takes a GEOMETRY and returns a GeoJson representation.
Definition: lwout_geojson.c:35
static void do_geojson_unsupported(char *in, char *out)
static void out_geojson_test_geoms(void)
static void out_geojson_test_precision(void)
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]