PostGIS  2.4.9dev-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 539 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().

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