PostGIS  2.5.0beta2dev-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 532 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().

533 {
534 #ifndef HAVE_LIBJSON
535  *srs = NULL;
536  lwerror("You need JSON-C for lwgeom_from_geojson");
537  return NULL;
538 #else /* HAVE_LIBJSON */
539 
540  /* size_t geojson_size = strlen(geojson); */
541 
542  LWGEOM *lwgeom;
543  int hasz=LW_TRUE;
544  json_tokener* jstok = NULL;
545  json_object* poObj = NULL;
546  json_object* poObjSrs = NULL;
547  *srs = NULL;
548 
549  /* Begin to Parse json */
550  jstok = json_tokener_new();
551  poObj = json_tokener_parse_ex(jstok, geojson, -1);
552  if( jstok->err != json_tokener_success)
553  {
554  char err[256];
555  snprintf(err, 256, "%s (at offset %d)", json_tokener_error_desc(jstok->err), jstok->char_offset);
556  json_tokener_free(jstok);
557  json_object_put(poObj);
558  geojson_lwerror(err, 1);
559  return NULL;
560  }
561  json_tokener_free(jstok);
562 
563  poObjSrs = findMemberByName( poObj, "crs" );
564  if (poObjSrs != NULL)
565  {
566  json_object* poObjSrsType = findMemberByName( poObjSrs, "type" );
567  if (poObjSrsType != NULL)
568  {
569  json_object* poObjSrsProps = findMemberByName( poObjSrs, "properties" );
570  if ( poObjSrsProps )
571  {
572  json_object* poNameURL = findMemberByName( poObjSrsProps, "name" );
573  if ( poNameURL )
574  {
575  const char* pszName = json_object_get_string( poNameURL );
576  if ( pszName )
577  {
578  *srs = lwalloc(strlen(pszName) + 1);
579  strcpy(*srs, pszName);
580  }
581  }
582  }
583  }
584  }
585 
586  lwgeom = parse_geojson(poObj, &hasz, 0);
587  json_object_put(poObj);
588 
589  lwgeom_add_bbox(lwgeom);
590 
591  if (!hasz)
592  {
593  LWGEOM *tmp = lwgeom_force_2d(lwgeom);
594  lwgeom_free(lwgeom);
595  lwgeom = tmp;
596 
597  LWDEBUG(2, "geom_from_geojson called.");
598  }
599 
600  return lwgeom;
601 #endif /* HAVE_LIBJSON } */
602 }
static LWGEOM * parse_geojson(json_object *geojson, int *hasz, int root_srid)
Definition: lwin_geojson.c:484
static void geojson_lwerror(char *msg, __attribute__((__unused__)) int error_code)
Definition: lwin_geojson.c:55
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#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:50
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
Definition: lwgeom.c:784
static json_object * findMemberByName(json_object *poObj, const char *pszName)
Definition: lwin_geojson.c:65
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:686
void * lwalloc(size_t size)
Definition: lwutil.c:229
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
Here is the call graph for this function:
Here is the caller graph for this function: