PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ lwgeom_from_geojson()

LWGEOM * lwgeom_from_geojson ( const char *  geojson,
char **  srs 
)

Create an LWGEOM object from a GeoJSON representation.

Parameters
geojsonthe GeoJSON input
srsoutput parameter. Will be set to a newly allocated string holding the spatial reference string, or NULL if no such parameter is found in input. If not null, the pointer must be freed with lwfree.

Definition at line 411 of file lwin_geojson.c.

412{
413#ifndef HAVE_LIBJSON
414 *srs = NULL;
415 lwerror("You need JSON-C for lwgeom_from_geojson");
416 return NULL;
417#else /* HAVE_LIBJSON */
418
419 /* Begin to Parse json */
420 json_tokener *jstok = json_tokener_new();
421 json_object *poObj = json_tokener_parse_ex(jstok, geojson, -1);
422 if (jstok->err != json_tokener_success)
423 {
424 char err[256];
425 snprintf(err, 256, "%s (at offset %d)", json_tokener_error_desc(jstok->err), jstok->char_offset);
426 json_tokener_free(jstok);
427 json_object_put(poObj);
428 lwerror("%s", err);
429 return NULL;
430 }
431 json_tokener_free(jstok);
432
433 *srs = NULL;
434 json_object *poObjSrs = findMemberByName(poObj, "crs");
435 if (poObjSrs != NULL)
436 {
437 json_object *poObjSrsType = findMemberByName(poObjSrs, "type");
438 if (poObjSrsType != NULL)
439 {
440 json_object *poObjSrsProps = findMemberByName(poObjSrs, "properties");
441 if (poObjSrsProps)
442 {
443 json_object *poNameURL = findMemberByName(poObjSrsProps, "name");
444 if (poNameURL)
445 {
446 const char *pszName = json_object_get_string(poNameURL);
447 if (pszName)
448 {
449 *srs = lwalloc(strlen(pszName) + 1);
450 strcpy(*srs, pszName);
451 }
452 }
453 }
454 }
455 }
456
457 int hasz = LW_FALSE;
458 LWGEOM *lwgeom = parse_geojson(poObj, &hasz);
459 json_object_put(poObj);
460 if (!lwgeom)
461 return NULL;
462
463 if (!hasz)
464 {
465 LWGEOM *tmp = lwgeom_force_2d(lwgeom);
466 lwgeom_free(lwgeom);
467 lwgeom = tmp;
468 }
469 lwgeom_add_bbox(lwgeom);
470 return lwgeom;
471#endif /* HAVE_LIBJSON */
472}
#define LW_FALSE
Definition liblwgeom.h:94
void lwgeom_free(LWGEOM *geom)
Definition lwgeom.c:1246
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
Definition lwgeom.c:821
void * lwalloc(size_t size)
Definition lwutil.c:227
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition lwgeom.c:723
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
#define json_tokener_error_desc(x)
static LWGEOM * parse_geojson(json_object *geojson, int *hasz)
static json_object * findMemberByName(json_object *poObj, const char *pszName)

References findMemberByName(), json_tokener_error_desc, LW_FALSE, lwalloc(), lwerror(), lwgeom_add_bbox(), lwgeom_force_2d(), lwgeom_free(), and parse_geojson().

Referenced by do_geojson_test(), geom_from_geojson(), and LWGEOM_in().

Here is the call graph for this function:
Here is the caller graph for this function: