PostGIS  3.4.0dev-r@@SVN_REVISION@@
cu_out_geojson.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  * Copyright 2010 Olivier Courtin <olivier.courtin@oslandia.com>
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 static void do_geojson_test(char * in, char * out, char * srs, int precision, int has_bbox)
21 {
23  lwvarlena_t *v = lwgeom_to_geojson(g, srs, precision, has_bbox);
24 
25  ASSERT_VARLENA_EQUAL(v, out);
26 
27  lwgeom_free(g);
28  lwfree(v);
29 }
30 
31 
32 static void do_geojson_unsupported(char * in, char * out)
33 {
35  lwvarlena_t *v = lwgeom_to_geojson(g, NULL, 0, 0);
36 
39 
40  lwfree(v);
41  lwgeom_free(g);
42 }
43 
44 
45 static void out_geojson_test_precision(void)
46 {
47  /* 0 precision, i.e a round */
49  "POINT(1.1111111111111 1.1111111111111)",
50  "{\"type\":\"Point\",\"coordinates\":[1,1]}",
51  NULL, 0, 0);
52 
53  /* 3 digits precision */
55  "POINT(1.1111111111111 1.1111111111111)",
56  "{\"type\":\"Point\",\"coordinates\":[1.111,1.111]}",
57  NULL, 3, 0);
58 
59  /* 9 digits precision */
61  "POINT(1.2345678901234 1.2345678901234)",
62  "{\"type\":\"Point\",\"coordinates\":[1.23456789,1.23456789]}",
63  NULL, 9, 0);
64 
65  /* huge data */
67  "POINT(1E300 -1E300)",
68  "{\"type\":\"Point\",\"coordinates\":[1e+300,-1e+300]}",
69  NULL, 0, 0);
70 
71  /* huge precision, see http://trac.osgeo.org/postgis/ticket/2052 */
73  "POINT(1 2)",
74  "{\"type\":\"Point\",\"coordinates\":[1,2]}",
75  NULL, 100, 0);
76 
77  /* double precision, see http://trac.osgeo.org/postgis/ticket/2051 */
79  "POINT(59.99 -59.99)",
80  "{\"type\":\"Point\",\"coordinates\":[59.99,-59.99]}",
81  NULL, 15, 0);
82 
83  /* small numbers */
84  do_geojson_test("POINT(1E-300 -2E-200)", "{\"type\":\"Point\",\"coordinates\":[1e-300,-2e-200]}", NULL, 300, 0);
85 }
86 
87 
88 static void out_geojson_test_dims(void)
89 {
90  /* 3D */
92  "POINT(0 1 2)",
93  "{\"type\":\"Point\",\"coordinates\":[0,1,2]}",
94  NULL, 0, 0);
95 
96  /* 3DM */
98  "POINTM(0 1 2)",
99  "{\"type\":\"Point\",\"coordinates\":[0,1]}",
100  NULL, 0, 0);
101 
102  /* 4D */
104  "POINT(0 1 2 3)",
105  "{\"type\":\"Point\",\"coordinates\":[0,1,2]}",
106  NULL, 0, 0);
107 }
108 
109 
110 static void out_geojson_test_srid(void)
111 {
112  /* Linestring */
114  "LINESTRING(0 1,2 3,4 5)",
115  "{\"type\":\"LineString\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[0,1],[2,3],[4,5]]}",
116  "EPSG:4326", 0, 0);
117 
118  /* Polygon */
120  "POLYGON((0 1,2 3,4 5,0 1))",
121  "{\"type\":\"Polygon\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
122  "EPSG:4326", 0, 0);
123 
124  /* Polygon - with internal ring */
126  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
127  "{\"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]]]}",
128  "EPSG:4326", 0, 0);
129 
130  /* Multiline */
132  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
133  "{\"type\":\"MultiLineString\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
134  "EPSG:4326", 0, 0);
135 
136  /* MultiPolygon */
138  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
139  "{\"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]]]]}",
140  "EPSG:4326", 0, 0);
141 
142  /* GeometryCollection */
144  "GEOMETRYCOLLECTION(POINT(0 1),LINESTRING(2 3,4 5))",
145  "{\"type\":\"GeometryCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"geometries\":[{\"type\":\"Point\",\"coordinates\":[0,1]},{\"type\":\"LineString\",\"coordinates\":[[2,3],[4,5]]}]}",
146  "EPSG:4326", 0, 0);
147 
148  /* Empty GeometryCollection */
150  "GEOMETRYCOLLECTION EMPTY",
151  "{\"type\":\"GeometryCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"geometries\":[]}",
152  "EPSG:4326", 0, 0);
153 }
154 
155 static void out_geojson_test_bbox(void)
156 {
157  /* Linestring */
159  "LINESTRING(0 1,2 3,4 5)",
160  "{\"type\":\"LineString\",\"bbox\":[0,1,4,5],\"coordinates\":[[0,1],[2,3],[4,5]]}",
161  NULL, 0, 1);
162 
163  /* Polygon */
165  "POLYGON((0 1,2 3,4 5,0 1))",
166  "{\"type\":\"Polygon\",\"bbox\":[0,1,4,5],\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
167  NULL, 0, 1);
168 
169  /* Polygon - with internal ring */
171  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
172  "{\"type\":\"Polygon\",\"bbox\":[0,1,4,5],\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]],[[6,7],[8,9],[10,11],[6,7]]]}",
173  NULL, 0, 1);
174 
175  /* Multiline */
177  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
178  "{\"type\":\"MultiLineString\",\"bbox\":[0,1,10,11],\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
179  NULL, 0, 1);
180 
181  /* MultiPolygon */
183  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
184  "{\"type\":\"MultiPolygon\",\"bbox\":[0,1,10,11],\"coordinates\":[[[[0,1],[2,3],[4,5],[0,1]]],[[[6,7],[8,9],[10,11],[6,7]]]]}",
185  NULL, 0, 1);
186 
187  /* GeometryCollection */
189  "GEOMETRYCOLLECTION(LINESTRING(0 1,-1 3),LINESTRING(2 3,4 5))",
190  "{\"type\":\"GeometryCollection\",\"bbox\":[-1,1,4,5],\"geometries\":[{\"type\":\"LineString\",\"coordinates\":[[0,1],[-1,3]]},{\"type\":\"LineString\",\"coordinates\":[[2,3],[4,5]]}]}",
191  NULL, 0, 1);
192 
193  /* Empty GeometryCollection */
195  "GEOMETRYCOLLECTION EMPTY",
196  "{\"type\":\"GeometryCollection\",\"geometries\":[]}",
197  NULL, 0, 1);
198 }
199 
200 static void out_geojson_test_geoms(void)
201 {
202  /* Linestring */
204  "LINESTRING(0 1,2 3,4 5)",
205  "{\"type\":\"LineString\",\"coordinates\":[[0,1],[2,3],[4,5]]}",
206  NULL, 0, 0);
207 
208  /* Polygon */
210  "POLYGON((0 1,2 3,4 5,0 1))",
211  "{\"type\":\"Polygon\",\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
212  NULL, 0, 0);
213 
214  /* Polygon - with internal ring */
216  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
217  "{\"type\":\"Polygon\",\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]],[[6,7],[8,9],[10,11],[6,7]]]}",
218  NULL, 0, 0);
219 
220  /* Multiline */
222  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
223  "{\"type\":\"MultiLineString\",\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
224  NULL, 0, 0);
225 
226  /* MultiPolygon */
228  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
229  "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0,1],[2,3],[4,5],[0,1]]],[[[6,7],[8,9],[10,11],[6,7]]]]}",
230  NULL, 0, 0);
231 
232  /* GeometryCollection */
234  "GEOMETRYCOLLECTION(POINT(0 1),LINESTRING(2 3,4 5))",
235  "{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"Point\",\"coordinates\":[0,1]},{\"type\":\"LineString\",\"coordinates\":[[2,3],[4,5]]}]}",
236  NULL, 0, 0);
237 
238  /* Empty GeometryCollection */
240  "GEOMETRYCOLLECTION EMPTY",
241  "{\"type\":\"GeometryCollection\",\"geometries\":[]}",
242  NULL, 0, 0);
243 
244  /* Nested GeometryCollection */
246  "GEOMETRYCOLLECTION(POINT(0 1),GEOMETRYCOLLECTION(LINESTRING(2 3,4 5)))",
247  "GeoJson: geometry not supported.");
248 
249  /* CircularString */
251  "CIRCULARSTRING(-2 0,0 2,2 0,0 2,2 4)",
252  "lwgeom_to_geojson: 'CircularString' geometry type not supported");
253 
254  /* CompoundCurve */
256  "COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1))",
257  "lwgeom_to_geojson: 'CompoundCurve' geometry type not supported");
258 
259  /* CurvePolygon */
261  "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))",
262  "lwgeom_to_geojson: 'CurvePolygon' geometry type not supported");
263 
264  /* MultiCurve */
266  "MULTICURVE((5 5,3 5,3 3,0 3),CIRCULARSTRING(0 0,2 1,2 2))",
267  "lwgeom_to_geojson: 'MultiCurve' geometry type not supported");
268 
269  /* MultiSurface */
271  "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)))",
272  "lwgeom_to_geojson: 'MultiSurface' geometry type not supported");
273 }
274 
275 /*
276 ** Used by test harness to register the tests in this file.
277 */
278 void out_geojson_suite_setup(void);
279 void out_geojson_suite_setup(void)
280 {
281  CU_pSuite suite = CU_add_suite("geojson_output", NULL, NULL);
285  PG_ADD_TEST(suite, out_geojson_test_bbox);
286  PG_ADD_TEST(suite, out_geojson_test_geoms);
287 }
static uint8_t precision
Definition: cu_in_twkb.c:25
static void out_geojson_test_dims(void)
static void do_geojson_unsupported(char *in, char *out)
static void out_geojson_test_precision(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 cu_error_msg_reset()
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
void out_geojson_suite_setup(void)
#define ASSERT_VARLENA_EQUAL(v, s)
#define PG_ADD_TEST(suite, testfunc)
#define ASSERT_STRING_EQUAL(o, e)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2114
lwvarlena_t * lwgeom_to_geojson(const LWGEOM *geo, const char *srs, int precision, int has_bbox)
Takes a GEOMETRY and returns a GeoJson representation.
void lwfree(void *mem)
Definition: lwutil.c:242
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:905