PostGIS  2.2.8dev-r@@SVN_REVISION@@
lwgeom_in_geojson.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * Copyright 2011 Kashif Rasul <kashif.rasul@gmail.com>
5  *
6  * This is free software; you can redistribute and/or modify it under
7  * the terms of the GNU General Public Licence. See the COPYING file.
8  *
9  **********************************************************************/
10 
11 #include <assert.h>
12 
13 #include "postgres.h"
14 
15 #include "../postgis_config.h"
16 #include "lwgeom_pg.h"
17 #include "liblwgeom.h"
18 #include "lwgeom_export.h"
19 
20 #ifdef HAVE_LIBJSON
21 # ifdef HAVE_LIBJSON_C
22 # include <json-c/json.h>
23 # else
24 # include <json/json.h>
25 # endif
26 #endif
27 
28 Datum geom_from_geojson(PG_FUNCTION_ARGS);
29 Datum postgis_libjson_version(PG_FUNCTION_ARGS);
30 
32 Datum postgis_libjson_version(PG_FUNCTION_ARGS)
33 {
34 #ifndef HAVE_LIBJSON
35  PG_RETURN_NULL();
36 #else /* HAVE_LIBJSON */
37 # ifdef JSON_C_VERSION
38  const char *ver = json_c_version();
39 # else
40  const char *ver = "UNKNOWN";
41 # endif
42  text *result = cstring2text(ver);
43  PG_RETURN_POINTER(result);
44 #endif
45 }
46 
48 Datum geom_from_geojson(PG_FUNCTION_ARGS)
49 {
50 #ifndef HAVE_LIBJSON
51  elog(ERROR, "You need JSON-C for ST_GeomFromGeoJSON");
52  PG_RETURN_NULL();
53 #else /* HAVE_LIBJSON */
54 
55  GSERIALIZED *geom;
56  LWGEOM *lwgeom;
57  text *geojson_input;
58  char *geojson;
59  char *srs = NULL;
60 
61  /* Get the geojson stream */
62  if (PG_ARGISNULL(0))
63  PG_RETURN_NULL();
64 
65  geojson_input = PG_GETARG_TEXT_P(0);
66  geojson = text2cstring(geojson_input);
67 
68  lwgeom = lwgeom_from_geojson(geojson, &srs);
69  if ( ! lwgeom )
70  {
71  /* Shouldn't get here */
72  elog(ERROR, "lwgeom_from_geojson returned NULL");
73  PG_RETURN_NULL();
74  }
75 
76  if ( srs )
77  {
78  lwgeom_set_srid(lwgeom, getSRIDbySRS(srs));
79  lwfree(srs);
80  }
81 
82  geom = geometry_serialize(lwgeom);
83  lwgeom_free(lwgeom);
84 
85  PG_RETURN_POINTER(geom);
86 #endif
87 }
88 
Datum geom_from_geojson(PG_FUNCTION_ARGS)
void lwfree(void *mem)
Definition: lwutil.c:214
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
Datum postgis_libjson_version(PG_FUNCTION_ARGS)
char * text2cstring(const text *textptr)
int getSRIDbySRS(const char *srs)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
void lwgeom_set_srid(LWGEOM *geom, int srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
PG_FUNCTION_INFO_V1(postgis_libjson_version)
This library is the generic geometry handling section of PostGIS.
LWGEOM * lwgeom_from_geojson(const char *geojson, char **srs)
Create an LWGEOM object from a GeoJSON representation.
Definition: lwin_geojson.c:512