-
-
Notifications
You must be signed in to change notification settings - Fork 381
/
lwin_geojson.c
472 lines (407 loc) · 11.5 KB
/
lwin_geojson.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
*
* PostGIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* PostGIS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*
* Copyright 2019 Darafei Praliaskouski <me@komzpa.net>
* Copyright 2013 Sandro Santilli <strk@kbt.io>
* Copyright 2011 Kashif Rasul <kashif.rasul@gmail.com>
*
**********************************************************************/
#include "liblwgeom.h"
#include "lwgeom_log.h"
#include "../postgis_config.h"
#if defined(HAVE_LIBJSON)
#define JSON_C_VERSION_013 (13 << 8)
#include <json.h>
#if !defined(JSON_C_VERSION_NUM) || JSON_C_VERSION_NUM < JSON_C_VERSION_013
#include <json_object_private.h>
#endif
#ifndef JSON_C_VERSION
/* Adds support for libjson < 0.10 */
#define json_tokener_error_desc(x) json_tokener_errors[(x)]
#endif
#include <string.h>
/* Prototype */
static LWGEOM *parse_geojson(json_object *geojson, int *hasz);
static inline json_object *
findMemberByName(json_object *poObj, const char *pszName)
{
json_object *poTmp;
json_object_iter it;
poTmp = poObj;
if (!pszName || !poObj)
return NULL;
it.key = NULL;
it.val = NULL;
it.entry = NULL;
if (json_object_get_object(poTmp))
{
if (!json_object_get_object(poTmp)->head)
{
lwerror("invalid GeoJSON representation");
return NULL;
}
for (it.entry = json_object_get_object(poTmp)->head;
(it.entry ? (it.key = (char *)it.entry->k, it.val = (json_object *)it.entry->v, it.entry) : 0);
it.entry = it.entry->next)
{
if (strcasecmp((char *)it.key, pszName) == 0)
return it.val;
}
}
return NULL;
}
static inline json_object *
parse_coordinates(json_object *geojson)
{
json_object *coordinates = findMemberByName(geojson, "coordinates");
if (!coordinates)
{
lwerror("Unable to find 'coordinates' in GeoJSON string");
return NULL;
}
if (json_type_array != json_object_get_type(coordinates))
{
lwerror("The 'coordinates' in GeoJSON are not an array");
return NULL;
}
return coordinates;
}
static inline int
parse_geojson_coord(json_object *poObj, int *hasz, POINTARRAY *pa)
{
POINT4D pt = {0, 0, 0, 0};
if (json_object_get_type(poObj) == json_type_array)
{
json_object *poObjCoord = NULL;
const int nSize = json_object_array_length(poObj);
if (nSize == 0)
return LW_TRUE;
if (nSize < 2)
{
lwerror("Too few ordinates in GeoJSON");
return LW_FAILURE;
}
/* Read X coordinate */
poObjCoord = json_object_array_get_idx(poObj, 0);
pt.x = json_object_get_double(poObjCoord);
/* Read Y coordinate */
poObjCoord = json_object_array_get_idx(poObj, 1);
pt.y = json_object_get_double(poObjCoord);
if (nSize > 2) /* should this be >= 3 ? */
{
/* Read Z coordinate */
poObjCoord = json_object_array_get_idx(poObj, 2);
pt.z = json_object_get_double(poObjCoord);
*hasz = LW_TRUE;
}
}
else
{
/* If it's not an array, just don't handle it */
lwerror("The 'coordinates' in GeoJSON are not sufficiently nested");
return LW_FAILURE;
}
return ptarray_append_point(pa, &pt, LW_TRUE);
}
static inline LWGEOM *
parse_geojson_point(json_object *geojson, int *hasz)
{
json_object *coords = parse_coordinates(geojson);
if (!coords)
return NULL;
POINTARRAY *pa = ptarray_construct_empty(1, 0, 1);
parse_geojson_coord(coords, hasz, pa);
return (LWGEOM *)lwpoint_construct(0, NULL, pa);
}
static inline LWGEOM *
parse_geojson_linestring(json_object *geojson, int *hasz)
{
json_object *points = parse_coordinates(geojson);
if (!points)
return NULL;
POINTARRAY *pa = ptarray_construct_empty(1, 0, 1);
const int nPoints = json_object_array_length(points);
for (int i = 0; i < nPoints; i++)
{
json_object *coords = json_object_array_get_idx(points, i);
parse_geojson_coord(coords, hasz, pa);
}
return (LWGEOM *)lwline_construct(0, NULL, pa);
}
static inline LWPOLY *
parse_geojson_poly_rings(json_object *rings, int *hasz)
{
if (!rings || json_object_get_type(rings) != json_type_array)
return NULL;
int nRings = json_object_array_length(rings);
/* No rings => POLYGON EMPTY */
if (!nRings)
return lwpoly_construct_empty(0, 1, 0);
/* Expecting up to nRings otherwise */
POINTARRAY **ppa = (POINTARRAY **)lwalloc(sizeof(POINTARRAY *) * nRings);
int o = 0;
for (int i = 0; i < nRings; i++)
{
json_object *points = json_object_array_get_idx(rings, i);
if (!points || json_object_get_type(points) != json_type_array)
{
for (int k = 0; k < o; k++)
ptarray_free(ppa[k]);
lwfree(ppa);
lwerror("The 'coordinates' in GeoJSON ring are not an array");
return NULL;
}
int nPoints = json_object_array_length(points);
/* Skip empty rings */
if (!nPoints)
{
/* Empty outer? Don't promote first hole to outer, holes don't matter. */
if (!i)
break;
else
continue;
}
ppa[o] = ptarray_construct_empty(1, 0, 1);
for (int j = 0; j < nPoints; j++)
{
json_object *coords = NULL;
coords = json_object_array_get_idx(points, j);
if (LW_FAILURE == parse_geojson_coord(coords, hasz, ppa[o]))
{
for (int k = 0; k <= o; k++)
ptarray_free(ppa[k]);
lwfree(ppa);
lwerror("The 'coordinates' in GeoJSON are not sufficiently nested");
return NULL;
}
}
o++;
}
/* All the rings were empty! */
if (!o)
{
lwfree(ppa);
return lwpoly_construct_empty(0, 1, 0);
}
return lwpoly_construct(0, NULL, o, ppa);
}
static inline LWGEOM *
parse_geojson_polygon(json_object *geojson, int *hasz)
{
return (LWGEOM *)parse_geojson_poly_rings(parse_coordinates(geojson), hasz);
}
static inline LWGEOM *
parse_geojson_multipoint(json_object *geojson, int *hasz)
{
json_object *points = parse_coordinates(geojson);
if (!points)
return NULL;
LWMPOINT *geom = (LWMPOINT *)lwcollection_construct_empty(MULTIPOINTTYPE, 0, 1, 0);
const int nPoints = json_object_array_length(points);
for (int i = 0; i < nPoints; ++i)
{
POINTARRAY *pa = ptarray_construct_empty(1, 0, 1);
json_object *coord = json_object_array_get_idx(points, i);
if (parse_geojson_coord(coord, hasz, pa))
geom = lwmpoint_add_lwpoint(geom, lwpoint_construct(0, NULL, pa));
else
{
lwmpoint_free(geom);
ptarray_free(pa);
return NULL;
}
}
return (LWGEOM *)geom;
}
static inline LWGEOM *
parse_geojson_multilinestring(json_object *geojson, int *hasz)
{
json_object *mls = parse_coordinates(geojson);
if (!mls)
return NULL;
LWMLINE *geom = (LWMLINE *)lwcollection_construct_empty(MULTILINETYPE, 0, 1, 0);
const int nLines = json_object_array_length(mls);
for (int i = 0; i < nLines; ++i)
{
POINTARRAY *pa = ptarray_construct_empty(1, 0, 1);
json_object *coords = json_object_array_get_idx(mls, i);
if (json_type_array == json_object_get_type(coords))
{
const int nPoints = json_object_array_length(coords);
for (int j = 0; j < nPoints; ++j)
{
json_object *coord = json_object_array_get_idx(coords, j);
if (!parse_geojson_coord(coord, hasz, pa))
{
lwmline_free(geom);
ptarray_free(pa);
return NULL;
}
}
geom = lwmline_add_lwline(geom, lwline_construct(0, NULL, pa));
}
else
{
lwmline_free(geom);
ptarray_free(pa);
return NULL;
}
}
return (LWGEOM *)geom;
}
static inline LWGEOM *
parse_geojson_multipolygon(json_object *geojson, int *hasz)
{
json_object *polys = parse_coordinates(geojson);
if (!polys)
return NULL;
LWGEOM *geom = (LWGEOM *)lwcollection_construct_empty(MULTIPOLYGONTYPE, 0, 1, 0);
int nPolys = json_object_array_length(polys);
for (int i = 0; i < nPolys; ++i)
{
json_object *rings = json_object_array_get_idx(polys, i);
LWPOLY *poly = parse_geojson_poly_rings(rings, hasz);
if (poly)
geom = (LWGEOM *)lwmpoly_add_lwpoly((LWMPOLY *)geom, poly);
}
return geom;
}
static inline LWGEOM *
parse_geojson_geometrycollection(json_object *geojson, int *hasz)
{
json_object *poObjGeoms = findMemberByName(geojson, "geometries");
if (!poObjGeoms)
{
lwerror("Unable to find 'geometries' in GeoJSON string");
return NULL;
}
LWGEOM *geom = (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, 0, 1, 0);
if (json_type_array == json_object_get_type(poObjGeoms))
{
const int nGeoms = json_object_array_length(poObjGeoms);
for (int i = 0; i < nGeoms; ++i)
{
json_object *poObjGeom = json_object_array_get_idx(poObjGeoms, i);
LWGEOM *t = parse_geojson(poObjGeom, hasz);
if (t)
geom = (LWGEOM *)lwcollection_add_lwgeom((LWCOLLECTION *)geom, t);
else
{
lwgeom_free(geom);
return NULL;
}
}
}
return geom;
}
static inline LWGEOM *
parse_geojson(json_object *geojson, int *hasz)
{
json_object *type = NULL;
const char *name;
if (!geojson)
{
lwerror("invalid GeoJSON representation");
return NULL;
}
type = findMemberByName(geojson, "type");
if (!type)
{
lwerror("unknown GeoJSON type");
return NULL;
}
name = json_object_get_string(type);
if (strcasecmp(name, "Point") == 0)
return parse_geojson_point(geojson, hasz);
if (strcasecmp(name, "LineString") == 0)
return parse_geojson_linestring(geojson, hasz);
if (strcasecmp(name, "Polygon") == 0)
return parse_geojson_polygon(geojson, hasz);
if (strcasecmp(name, "MultiPoint") == 0)
return parse_geojson_multipoint(geojson, hasz);
if (strcasecmp(name, "MultiLineString") == 0)
return parse_geojson_multilinestring(geojson, hasz);
if (strcasecmp(name, "MultiPolygon") == 0)
return parse_geojson_multipolygon(geojson, hasz);
if (strcasecmp(name, "GeometryCollection") == 0)
return parse_geojson_geometrycollection(geojson, hasz);
lwerror("invalid GeoJson representation");
return NULL; /* Never reach */
}
#endif /* HAVE_LIBJSON */
LWGEOM *
lwgeom_from_geojson(const char *geojson, char **srs)
{
#ifndef HAVE_LIBJSON
*srs = NULL;
lwerror("You need JSON-C for lwgeom_from_geojson");
return NULL;
#else /* HAVE_LIBJSON */
/* Begin to Parse json */
json_tokener *jstok = json_tokener_new();
json_object *poObj = json_tokener_parse_ex(jstok, geojson, -1);
if (jstok->err != json_tokener_success)
{
char err[256];
snprintf(err, 256, "%s (at offset %d)", json_tokener_error_desc(jstok->err), jstok->char_offset);
json_tokener_free(jstok);
json_object_put(poObj);
lwerror("%s", err);
return NULL;
}
json_tokener_free(jstok);
*srs = NULL;
json_object *poObjSrs = findMemberByName(poObj, "crs");
if (poObjSrs != NULL)
{
json_object *poObjSrsType = findMemberByName(poObjSrs, "type");
if (poObjSrsType != NULL)
{
json_object *poObjSrsProps = findMemberByName(poObjSrs, "properties");
if (poObjSrsProps)
{
json_object *poNameURL = findMemberByName(poObjSrsProps, "name");
if (poNameURL)
{
const char *pszName = json_object_get_string(poNameURL);
if (pszName)
{
*srs = lwalloc(strlen(pszName) + 1);
strcpy(*srs, pszName);
}
}
}
}
}
int hasz = LW_FALSE;
LWGEOM *lwgeom = parse_geojson(poObj, &hasz);
json_object_put(poObj);
if (!lwgeom)
return NULL;
if (!hasz)
{
LWGEOM *tmp = lwgeom_force_2d(lwgeom);
lwgeom_free(lwgeom);
lwgeom = tmp;
}
lwgeom_add_bbox(lwgeom);
return lwgeom;
#endif /* HAVE_LIBJSON */
}