PostGIS  2.4.9dev-r@@SVN_REVISION@@
cu_in_geojson.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * Copyright 2013 Sandro Santilli <strk@kbt.io>
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(const char * exp, char * in, char * exp_srs, int precision, int has_bbox)
22 {
23  LWGEOM *g;
24  char * h = NULL;
25  char * srs = NULL;
26  size_t size;
27 
28  g = lwgeom_from_geojson(in, &srs);
29  if ( ! g )
30  {
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  {
40  fprintf(stderr, "\nIn: %s\nExp: %s\nObt: %s\n", in, exp, h);
41  CU_ASSERT_STRING_EQUAL(h, exp);
42  }
43 
44  if ( exp_srs )
45  {
46  if ( ! srs )
47  {
48  fprintf(stderr, "\nIn: %s\nExp: %s\nObt: (null)\n", in, exp_srs);
49  CU_ASSERT_EQUAL(srs, exp_srs);
50  }
51  else if (strcmp(srs, exp_srs))
52  {
53  fprintf(stderr, "\nIn: %s\nExp: %s\nObt: %s\n", in, exp_srs, srs);
54  CU_ASSERT_STRING_EQUAL(srs, exp_srs);
55  }
56  }
57  else if ( srs )
58  {
59  fprintf(stderr, "\nIn: %s\nExp: (null)\nObt: %s\n", in, srs);
60  CU_ASSERT_EQUAL(srs, exp_srs);
61  }
62 
63  lwgeom_free(g);
64  if ( h ) lwfree(h);
65  if ( srs ) lwfree(srs);
66 }
67 
68 static void in_geojson_test_srid(void)
69 {
70  /* Linestring */
72  "LINESTRING(0 1,2 3,4 5)",
73  "{\"type\":\"LineString\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[0,1],[2,3],[4,5]]}",
74  "EPSG:4326", 0, 0);
75 
76  /* Polygon */
78  "POLYGON((0 1,2 3,4 5,0 1))",
79  "{\"type\":\"Polygon\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
80  "EPSG:4326", 0, 0);
81 
82  /* Polygon - with internal ring */
84  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
85  "{\"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]]]}",
86  "EPSG:4326", 0, 0);
87 
88  /* Multiline */
90  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
91  "{\"type\":\"MultiLineString\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
92  "EPSG:4326", 0, 0);
93 
94  /* MultiPolygon */
96  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
97  "{\"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]]]]}",
98  "EPSG:4326", 0, 0);
99 
100  /* Empty GeometryCollection */
102  "GEOMETRYCOLLECTION EMPTY",
103  "{\"type\":\"GeometryCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"geometries\":[]}",
104  "EPSG:4326", 0, 0);
105 }
106 
107 static void in_geojson_test_bbox(void)
108 {
109  /* Linestring */
111  "LINESTRING(0 1,2 3,4 5)",
112  "{\"type\":\"LineString\",\"bbox\":[0,1,4,5],\"coordinates\":[[0,1],[2,3],[4,5]]}",
113  NULL, 0, 1);
114 
115  /* Polygon */
117  "POLYGON((0 1,2 3,4 5,0 1))",
118  "{\"type\":\"Polygon\",\"bbox\":[0,1,4,5],\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
119  NULL, 0, 1);
120 
121  /* Polygon - with internal ring */
123  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
124  "{\"type\":\"Polygon\",\"bbox\":[0,1,4,5],\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]],[[6,7],[8,9],[10,11],[6,7]]]}",
125  NULL, 0, 1);
126 
127  /* Multiline */
129  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
130  "{\"type\":\"MultiLineString\",\"bbox\":[0,1,10,11],\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
131  NULL, 0, 1);
132 
133  /* MultiPolygon */
135  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
136  "{\"type\":\"MultiPolygon\",\"bbox\":[0,1,10,11],\"coordinates\":[[[[0,1],[2,3],[4,5],[0,1]]],[[[6,7],[8,9],[10,11],[6,7]]]]}",
137  NULL, 0, 1);
138 
139  /* GeometryCollection */
141  "GEOMETRYCOLLECTION(LINESTRING(0 1,-1 3),LINESTRING(2 3,4 5))",
142  "{\"type\":\"GeometryCollection\",\"bbox\":[-1,1,4,5],\"geometries\":[{\"type\":\"LineString\",\"coordinates\":[[0,1],[-1,3]]},{\"type\":\"LineString\",\"coordinates\":[[2,3],[4,5]]}]}",
143  NULL, 0, 1);
144 
145  /* Empty GeometryCollection */
147  "GEOMETRYCOLLECTION EMPTY",
148  "{\"type\":\"GeometryCollection\",\"geometries\":[]}",
149  NULL, 0, 1);
150 }
151 
152 static void in_geojson_test_geoms(void)
153 {
154  /* Linestring */
156  "LINESTRING(0 1,2 3,4 5)",
157  "{\"type\":\"LineString\",\"coordinates\":[[0,1],[2,3],[4,5]]}",
158  NULL, 0, 0);
159 
160  /* Polygon */
162  "POLYGON((0 1,2 3,4 5,0 1))",
163  "{\"type\":\"Polygon\",\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
164  NULL, 0, 0);
165 
166  /* Polygon - with internal ring */
168  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
169  "{\"type\":\"Polygon\",\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]],[[6,7],[8,9],[10,11],[6,7]]]}",
170  NULL, 0, 0);
171 
172  /* Multiline */
174  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
175  "{\"type\":\"MultiLineString\",\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
176  NULL, 0, 0);
177 
178  /* MultiPolygon */
180  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
181  "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0,1],[2,3],[4,5],[0,1]]],[[[6,7],[8,9],[10,11],[6,7]]]]}",
182  NULL, 0, 0);
183 
184  /* MultiPolygon with internal rings */
185  /* See http://trac.osgeo.org/postgis/ticket/2216 */
187  "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)))",
188  "{'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]]]]}",
189  NULL, 0, 0);
190 
191  /* GeometryCollection */
193  "GEOMETRYCOLLECTION(POINT(0 1),LINESTRING(2 3,4 5))",
194  "{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"Point\",\"coordinates\":[0,1]},{\"type\":\"LineString\",\"coordinates\":[[2,3],[4,5]]}]}",
195  NULL, 0, 0);
196 
197  /* Empty GeometryCollection */
199  "GEOMETRYCOLLECTION EMPTY",
200  "{\"type\":\"GeometryCollection\",\"geometries\":[]}",
201  NULL, 0, 0);
202 }
203 
204 /*
205 ** Used by test harness to register the tests in this file.
206 */
207 void in_geojson_suite_setup(void);
209 {
210  CU_pSuite suite = CU_add_suite("geojson_input", NULL, NULL);
214 }
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:669
void lwfree(void *mem)
Definition: lwutil.c:244
static void in_geojson_test_bbox(void)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
static void in_geojson_test_srid(void)
Definition: cu_in_geojson.c:68
static void do_geojson_test(const char *exp, char *in, char *exp_srs, int precision, int has_bbox)
Definition: cu_in_geojson.c:21
static void in_geojson_test_geoms(void)
#define PG_ADD_TEST(suite, testfunc)
uint8_t precision
Definition: cu_in_twkb.c:25
#define WKT_EXTENDED
Definition: liblwgeom.h:2085
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:539