PostGIS  2.1.10dev-r@@SVN_REVISION@@
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 510 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().

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

Here is the call graph for this function:

Here is the caller graph for this function: