PostGIS  2.3.8dev-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 526 of file lwin_geojson.c.

References findMemberByName(), geojson_lwerror(), json_tokener_error_desc, LW_TRUE, lwalloc(), LWDEBUG, lwerror(), lwgeom_add_bbox(), lwgeom_force_2d(), lwgeom_free(), and parse_geojson().

Referenced by do_geojson_test(), and geom_from_geojson().

527 {
528 #ifndef HAVE_LIBJSON
529  *srs = NULL;
530  lwerror("You need JSON-C for lwgeom_from_geojson");
531  return NULL;
532 #else /* HAVE_LIBJSON */
533 
534  /* size_t geojson_size = strlen(geojson); */
535 
536  LWGEOM *lwgeom;
537  int hasz=LW_TRUE;
538  json_tokener* jstok = NULL;
539  json_object* poObj = NULL;
540  json_object* poObjSrs = NULL;
541  *srs = NULL;
542 
543  /* Begin to Parse json */
544  jstok = json_tokener_new();
545  poObj = json_tokener_parse_ex(jstok, geojson, -1);
546  if( jstok->err != json_tokener_success)
547  {
548  char err[256];
549  snprintf(err, 256, "%s (at offset %d)", json_tokener_error_desc(jstok->err), jstok->char_offset);
550  json_tokener_free(jstok);
551  json_object_put(poObj);
552  geojson_lwerror(err, 1);
553  return NULL;
554  }
555  json_tokener_free(jstok);
556 
557  poObjSrs = findMemberByName( poObj, "crs" );
558  if (poObjSrs != NULL)
559  {
560  json_object* poObjSrsType = findMemberByName( poObjSrs, "type" );
561  if (poObjSrsType != NULL)
562  {
563  json_object* poObjSrsProps = findMemberByName( poObjSrs, "properties" );
564  if ( poObjSrsProps )
565  {
566  json_object* poNameURL = findMemberByName( poObjSrsProps, "name" );
567  if ( poNameURL )
568  {
569  const char* pszName = json_object_get_string( poNameURL );
570  if ( pszName )
571  {
572  *srs = lwalloc(strlen(pszName) + 1);
573  strcpy(*srs, pszName);
574  }
575  }
576  }
577  }
578  }
579 
580  lwgeom = parse_geojson(poObj, &hasz, 0);
581  json_object_put(poObj);
582 
583  lwgeom_add_bbox(lwgeom);
584 
585  if (!hasz)
586  {
587  LWGEOM *tmp = lwgeom_force_2d(lwgeom);
588  lwgeom_free(lwgeom);
589  lwgeom = tmp;
590 
591  LWDEBUG(2, "geom_from_geojson called.");
592  }
593 
594  return lwgeom;
595 #endif /* HAVE_LIBJSON } */
596 }
static LWGEOM * parse_geojson(json_object *geojson, int *hasz, int root_srid)
Definition: lwin_geojson.c:478
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1063
static void geojson_lwerror(char *msg, int error_code)
Definition: lwin_geojson.c:49
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
#define json_tokener_error_desc(x)
Definition: lwin_geojson.c:44
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
Definition: lwgeom.c:703
static json_object * findMemberByName(json_object *poObj, const char *pszName)
Definition: lwin_geojson.c:59
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:612
void * lwalloc(size_t size)
Definition: lwutil.c:227
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:102
Here is the call graph for this function:
Here is the caller graph for this function: