PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ 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 409 of file lwin_geojson.c.

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

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(), and geom_from_geojson().

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