PostGIS  2.5.2dev-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 542 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().

543 {
544 #ifndef HAVE_LIBJSON
545  *srs = NULL;
546  lwerror("You need JSON-C for lwgeom_from_geojson");
547  return NULL;
548 #else /* HAVE_LIBJSON */
549 
550  /* size_t geojson_size = strlen(geojson); */
551 
552  LWGEOM *lwgeom;
553  int hasz=LW_TRUE;
554  json_tokener* jstok = NULL;
555  json_object* poObj = NULL;
556  json_object* poObjSrs = NULL;
557  *srs = NULL;
558 
559  /* Begin to Parse json */
560  jstok = json_tokener_new();
561  poObj = json_tokener_parse_ex(jstok, geojson, -1);
562  if( jstok->err != json_tokener_success)
563  {
564  char err[256];
565  snprintf(err, 256, "%s (at offset %d)", json_tokener_error_desc(jstok->err), jstok->char_offset);
566  json_tokener_free(jstok);
567  json_object_put(poObj);
568  geojson_lwerror(err, 1);
569  return NULL;
570  }
571  json_tokener_free(jstok);
572 
573  poObjSrs = findMemberByName( poObj, "crs" );
574  if (poObjSrs != NULL)
575  {
576  json_object* poObjSrsType = findMemberByName( poObjSrs, "type" );
577  if (poObjSrsType != NULL)
578  {
579  json_object* poObjSrsProps = findMemberByName( poObjSrs, "properties" );
580  if ( poObjSrsProps )
581  {
582  json_object* poNameURL = findMemberByName( poObjSrsProps, "name" );
583  if ( poNameURL )
584  {
585  const char* pszName = json_object_get_string( poNameURL );
586  if ( pszName )
587  {
588  *srs = lwalloc(strlen(pszName) + 1);
589  strcpy(*srs, pszName);
590  }
591  }
592  }
593  }
594  }
595 
596  lwgeom = parse_geojson(poObj, &hasz, 0);
597  json_object_put(poObj);
598 
599  lwgeom_add_bbox(lwgeom);
600 
601  if (!hasz)
602  {
603  LWGEOM *tmp = lwgeom_force_2d(lwgeom);
604  lwgeom_free(lwgeom);
605  lwgeom = tmp;
606 
607  LWDEBUG(2, "geom_from_geojson called.");
608  }
609 
610  return lwgeom;
611 #endif /* HAVE_LIBJSON } */
612 }
static LWGEOM * parse_geojson(json_object *geojson, int *hasz, int root_srid)
Definition: lwin_geojson.c:494
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: