PostGIS  2.2.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 512 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().

513 {
514 #ifndef HAVE_LIBJSON
515  *srs = NULL;
516  lwerror("You need JSON-C for lwgeom_from_geojson");
517  return NULL;
518 #else /* HAVE_LIBJSON */
519 
520  /* size_t geojson_size = strlen(geojson); */
521 
522  LWGEOM *lwgeom;
523  int hasz=LW_TRUE;
524  json_tokener* jstok = NULL;
525  json_object* poObj = NULL;
526  json_object* poObjSrs = NULL;
527  *srs = NULL;
528 
529  /* Begin to Parse json */
530  jstok = json_tokener_new();
531  poObj = json_tokener_parse_ex(jstok, geojson, -1);
532  if( jstok->err != json_tokener_success)
533  {
534  char err[256];
535  snprintf(err, 256, "%s (at offset %d)", json_tokener_error_desc(jstok->err), jstok->char_offset);
536  json_tokener_free(jstok);
537  json_object_put(poObj);
538  geojson_lwerror(err, 1);
539  return NULL;
540  }
541  json_tokener_free(jstok);
542 
543  poObjSrs = findMemberByName( poObj, "crs" );
544  if (poObjSrs != NULL)
545  {
546  json_object* poObjSrsType = findMemberByName( poObjSrs, "type" );
547  if (poObjSrsType != NULL)
548  {
549  json_object* poObjSrsProps = findMemberByName( poObjSrs, "properties" );
550  if ( poObjSrsProps )
551  {
552  json_object* poNameURL = findMemberByName( poObjSrsProps, "name" );
553  if ( poNameURL )
554  {
555  const char* pszName = json_object_get_string( poNameURL );
556  if ( pszName )
557  {
558  *srs = lwalloc(strlen(pszName) + 1);
559  strcpy(*srs, pszName);
560  }
561  }
562  }
563  }
564  }
565 
566  lwgeom = parse_geojson(poObj, &hasz, 0);
567  json_object_put(poObj);
568 
569  lwgeom_add_bbox(lwgeom);
570 
571  if (!hasz)
572  {
573  LWGEOM *tmp = lwgeom_force_2d(lwgeom);
574  lwgeom_free(lwgeom);
575  lwgeom = tmp;
576 
577  LWDEBUG(2, "geom_from_geojson called.");
578  }
579 
580  return lwgeom;
581 #endif /* HAVE_LIBJSON } */
582 }
static LWGEOM * parse_geojson(json_object *geojson, int *hasz, int root_srid)
Definition: lwin_geojson.c:464
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
static void geojson_lwerror(char *msg, int error_code)
Definition: lwin_geojson.c:35
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:61
#define json_tokener_error_desc(x)
Definition: lwin_geojson.c:30
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
Definition: lwgeom.c:690
static json_object * findMemberByName(json_object *poObj, const char *pszName)
Definition: lwin_geojson.c:45
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:599
void * lwalloc(size_t size)
Definition: lwutil.c:199
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74
Here is the call graph for this function:
Here is the caller graph for this function: