PostGIS  3.4.0dev-r@@SVN_REVISION@@
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
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)
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  else
44  {
45  CU_ASSERT_STRING_EQUAL(h, exp);
46  }
47 
48  if ( exp_srs )
49  {
50  if ( ! srs )
51  {
52  fprintf(stderr, "\nIn: %s\nExp: %s\nObt: (null)\n", in, exp_srs);
53  CU_ASSERT_EQUAL(srs, exp_srs);
54  }
55  else if (strcmp(srs, exp_srs))
56  {
57  fprintf(stderr, "\nIn: %s\nExp: %s\nObt: %s\n", in, exp_srs, srs);
58  CU_ASSERT_STRING_EQUAL(srs, exp_srs);
59  }
60  }
61  else if ( srs )
62  {
63  fprintf(stderr, "\nIn: %s\nExp: (null)\nObt: %s\n", in, srs);
64  CU_ASSERT_EQUAL(srs, exp_srs);
65  }
66 
67  lwgeom_free(g);
68  if ( h ) lwfree(h);
69  if ( srs ) lwfree(srs);
70 }
71 
72 static void in_geojson_test_srid(void)
73 {
74  /* Linestring */
76  "LINESTRING(0 1,2 3,4 5)",
77  "{\"type\":\"LineString\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[0,1],[2,3],[4,5]]}",
78  "EPSG:4326");
79 
80  /* Polygon */
82  "POLYGON((0 1,2 3,4 5,0 1))",
83  "{\"type\":\"Polygon\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
84  "EPSG:4326");
85 
86  /* Polygon - with internal ring */
88  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
89  "{\"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]]]}",
90  "EPSG:4326");
91 
92  /* Multiline */
94  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
95  "{\"type\":\"MultiLineString\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
96  "EPSG:4326");
97 
98  /* MultiPolygon */
100  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
101  "{\"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]]]]}",
102  "EPSG:4326");
103 
104  /* Empty GeometryCollection */
106  "GEOMETRYCOLLECTION EMPTY",
107  "{\"type\":\"GeometryCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"geometries\":[]}",
108  "EPSG:4326");
109 }
110 
111 static void in_geojson_test_bbox(void)
112 {
113  /* Linestring */
115  "LINESTRING(0 1,2 3,4 5)",
116  "{\"type\":\"LineString\",\"bbox\":[0,1,4,5],\"coordinates\":[[0,1],[2,3],[4,5]]}",
117  NULL);
118 
119  /* Polygon */
121  "POLYGON((0 1,2 3,4 5,0 1))",
122  "{\"type\":\"Polygon\",\"bbox\":[0,1,4,5],\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
123  NULL);
124 
125  /* Polygon - with internal ring */
127  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
128  "{\"type\":\"Polygon\",\"bbox\":[0,1,4,5],\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]],[[6,7],[8,9],[10,11],[6,7]]]}",
129  NULL);
130 
131  /* Multiline */
133  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
134  "{\"type\":\"MultiLineString\",\"bbox\":[0,1,10,11],\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
135  NULL);
136 
137  /* MultiPolygon */
139  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
140  "{\"type\":\"MultiPolygon\",\"bbox\":[0,1,10,11],\"coordinates\":[[[[0,1],[2,3],[4,5],[0,1]]],[[[6,7],[8,9],[10,11],[6,7]]]]}",
141  NULL);
142 
143  /* GeometryCollection */
145  "GEOMETRYCOLLECTION(LINESTRING(0 1,-1 3),LINESTRING(2 3,4 5))",
146  "{\"type\":\"GeometryCollection\",\"bbox\":[-1,1,4,5],\"geometries\":[{\"type\":\"LineString\",\"coordinates\":[[0,1],[-1,3]]},{\"type\":\"LineString\",\"coordinates\":[[2,3],[4,5]]}]}",
147  NULL);
148 
149  /* Empty GeometryCollection */
151  "GEOMETRYCOLLECTION EMPTY",
152  "{\"type\":\"GeometryCollection\",\"geometries\":[]}",
153  NULL);
154 }
155 
156 static void in_geojson_test_geoms(void)
157 {
158  /* Linestring */
160  "LINESTRING(0 1,2 3,4 5)",
161  "{\"type\":\"LineString\",\"coordinates\":[[0,1],[2,3],[4,5]]}",
162  NULL);
163 
164  /* Polygon */
166  "POLYGON((0 1,2 3,4 5,0 1))",
167  "{\"type\":\"Polygon\",\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]]]}",
168  NULL);
169 
170  /* Polygon - with internal ring */
172  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
173  "{\"type\":\"Polygon\",\"coordinates\":[[[0,1],[2,3],[4,5],[0,1]],[[6,7],[8,9],[10,11],[6,7]]]}",
174  NULL);
175 
176  /* Multiline */
178  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
179  "{\"type\":\"MultiLineString\",\"coordinates\":[[[0,1],[2,3],[4,5]],[[6,7],[8,9],[10,11]]]}",
180  NULL);
181 
182  /* MultiPolygon */
184  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
185  "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0,1],[2,3],[4,5],[0,1]]],[[[6,7],[8,9],[10,11],[6,7]]]]}",
186  NULL);
187 
188  /* MultiPolygon with internal rings */
189  /* See http://trac.osgeo.org/postgis/ticket/2216 */
191  "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)))",
192  "{'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]]]]}",
193  NULL);
194 
195  /* GeometryCollection */
197  "GEOMETRYCOLLECTION(POINT(0 1),LINESTRING(2 3,4 5))",
198  "{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"Point\",\"coordinates\":[0,1]},{\"type\":\"LineString\",\"coordinates\":[[2,3],[4,5]]}]}",
199  NULL);
200 
201  /* Empty GeometryCollection */
203  "GEOMETRYCOLLECTION EMPTY",
204  "{\"type\":\"GeometryCollection\",\"geometries\":[]}",
205  NULL);
206 
207  /* Empty Point */
209  "POINT EMPTY",
210  "{\"type\":\"Point\",\"coordinates\":[]}",
211  NULL);
212 
213  /* Empty LineString */
215  "LINESTRING EMPTY",
216  "{\"type\":\"LineString\",\"coordinates\":[]}",
217  NULL);
218 
219  /* Empty Polygon */
221  "POLYGON EMPTY",
222  "{\"type\":\"Polygon\",\"coordinates\":[]}",
223  NULL);
224 
225  /* Empty MultiPoint */
227  "MULTIPOINT EMPTY",
228  "{\"type\":\"MultiPoint\",\"coordinates\":[]}",
229  NULL);
230 
231  /* Empty MultiPolygon */
233  "MULTIPOLYGON EMPTY",
234  "{\"type\":\"MultiPolygon\",\"coordinates\":[]}",
235  NULL);
236 }
237 
238 /*
239 ** Used by test harness to register the tests in this file.
240 */
241 void in_geojson_suite_setup(void);
242 void in_geojson_suite_setup(void)
243 {
244  CU_pSuite suite = CU_add_suite("geojson_input", NULL, NULL);
246  PG_ADD_TEST(suite, in_geojson_test_bbox);
247  PG_ADD_TEST(suite, in_geojson_test_geoms);
248 }
static void in_geojson_test_srid(void)
Definition: cu_in_geojson.c:72
static void do_geojson_test(const char *exp, char *in, char *exp_srs)
Definition: cu_in_geojson.c:21
void in_geojson_suite_setup(void)
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
#define PG_ADD_TEST(suite, testfunc)
LWGEOM * lwgeom_from_geojson(const char *geojson, char **srs)
Create an LWGEOM object from a GeoJSON representation.
Definition: lwin_geojson.c:411
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
#define WKT_EXTENDED
Definition: liblwgeom.h:2186
void lwfree(void *mem)
Definition: lwutil.c:242
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:708