PostGIS  2.4.9dev-r@@SVN_REVISION@@
lwgeom_in_geojson.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright 2011 Kashif Rasul <kashif.rasul@gmail.com>
22  *
23  **********************************************************************/
24 
25 
26 #include <assert.h>
27 
28 #include "postgres.h"
29 
30 #include "../postgis_config.h"
31 #include "lwgeom_pg.h"
32 #include "liblwgeom.h"
33 #include "lwgeom_export.h"
34 
35 #ifdef HAVE_LIBJSON
36 # ifdef HAVE_LIBJSON_C
37 # include <json-c/json.h>
38 # else
39 # include <json/json.h>
40 # endif
41 #endif
42 
43 Datum geom_from_geojson(PG_FUNCTION_ARGS);
44 Datum postgis_libjson_version(PG_FUNCTION_ARGS);
45 
47 Datum postgis_libjson_version(PG_FUNCTION_ARGS)
48 {
49 #ifndef HAVE_LIBJSON
50  PG_RETURN_NULL();
51 #else /* HAVE_LIBJSON */
52 # ifdef JSON_C_VERSION
53  const char *ver = json_c_version();
54 # else
55  const char *ver = "UNKNOWN";
56 # endif
57  text *result = cstring2text(ver);
58  PG_RETURN_POINTER(result);
59 #endif
60 }
61 
63 Datum geom_from_geojson(PG_FUNCTION_ARGS)
64 {
65 #ifndef HAVE_LIBJSON
66  elog(ERROR, "You need JSON-C for ST_GeomFromGeoJSON");
67  PG_RETURN_NULL();
68 #else /* HAVE_LIBJSON */
69 
70  GSERIALIZED *geom;
71  LWGEOM *lwgeom;
72  text *geojson_input;
73  char *geojson;
74  char *srs = NULL;
75 
76  /* Get the geojson stream */
77  if (PG_ARGISNULL(0))
78  PG_RETURN_NULL();
79 
80  geojson_input = PG_GETARG_TEXT_P(0);
81  geojson = text2cstring(geojson_input);
82 
83  lwgeom = lwgeom_from_geojson(geojson, &srs);
84  if ( ! lwgeom )
85  {
86  /* Shouldn't get here */
87  elog(ERROR, "lwgeom_from_geojson returned NULL");
88  PG_RETURN_NULL();
89  }
90 
91  if ( srs )
92  {
93  lwgeom_set_srid(lwgeom, getSRIDbySRS(srs));
94  lwfree(srs);
95  }
96 
97  geom = geometry_serialize(lwgeom);
98  lwgeom_free(lwgeom);
99 
100  PG_RETURN_POINTER(geom);
101 #endif
102 }
103 
Datum geom_from_geojson(PG_FUNCTION_ARGS)
void lwfree(void *mem)
Definition: lwutil.c:244
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
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:539