PostGIS  2.5.7dev-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 
42 /* We don't include <utils/builtins.h> to avoid collisions with json-c/json.h */
43 static text*
44 cstring2text(const char *cstring)
45 {
46  size_t len = strlen(cstring);
47  text *result = (text *) palloc(len + VARHDRSZ);
48  SET_VARSIZE(result, len + VARHDRSZ);
49  memcpy(VARDATA(result), cstring, len);
50 
51  return result;
52 }
53 
54 static char*
55 text2cstring(const text *textptr)
56 {
57  size_t size = VARSIZE(textptr) - VARHDRSZ;
58  char *str = lwalloc(size+1);
59  memcpy(str, VARDATA(textptr), size);
60  str[size]='\0';
61  return str;
62 }
63 #endif
64 
65 Datum geom_from_geojson(PG_FUNCTION_ARGS);
66 Datum postgis_libjson_version(PG_FUNCTION_ARGS);
67 
69 Datum postgis_libjson_version(PG_FUNCTION_ARGS)
70 {
71 #ifndef HAVE_LIBJSON
72  PG_RETURN_NULL();
73 #else /* HAVE_LIBJSON */
74 # ifdef JSON_C_VERSION
75  const char *ver = json_c_version();
76 # else
77  const char *ver = "UNKNOWN";
78 # endif
79  text *result = cstring2text(ver);
80  PG_RETURN_POINTER(result);
81 #endif
82 }
83 
85 Datum geom_from_geojson(PG_FUNCTION_ARGS)
86 {
87 #ifndef HAVE_LIBJSON
88  elog(ERROR, "You need JSON-C for ST_GeomFromGeoJSON");
89  PG_RETURN_NULL();
90 #else /* HAVE_LIBJSON */
91 
92  GSERIALIZED *geom;
93  LWGEOM *lwgeom;
94  text *geojson_input;
95  char *geojson;
96  char *srs = NULL;
97 
98  /* Get the geojson stream */
99  if (PG_ARGISNULL(0))
100  PG_RETURN_NULL();
101 
102  geojson_input = PG_GETARG_TEXT_P(0);
103  geojson = text2cstring(geojson_input);
104 
105  lwgeom = lwgeom_from_geojson(geojson, &srs);
106  if ( ! lwgeom )
107  {
108  /* Shouldn't get here */
109  elog(ERROR, "lwgeom_from_geojson returned NULL");
110  PG_RETURN_NULL();
111  }
112 
113  if ( srs )
114  {
115  lwgeom_set_srid(lwgeom, getSRIDbySRS(fcinfo, srs));
116  lwfree(srs);
117  }
118 
119  geom = geometry_serialize(lwgeom);
120  lwgeom_free(lwgeom);
121 
122  PG_RETURN_POINTER(geom);
123 #endif
124 }
125 
LWGEOM * lwgeom_from_geojson(const char *geojson, char **srs)
Create an LWGEOM object from a GeoJSON representation.
Definition: lwin_geojson.c:539
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
void lwfree(void *mem)
Definition: lwutil.c:244
void * lwalloc(size_t size)
Definition: lwutil.c:229
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...
This library is the generic geometry handling section of PostGIS.
int getSRIDbySRS(FunctionCallInfo fcinfo, const char *srs)
PG_FUNCTION_INFO_V1(postgis_libjson_version)
Datum postgis_libjson_version(PG_FUNCTION_ARGS)
Datum geom_from_geojson(PG_FUNCTION_ARGS)
static text * cstring2text(const char *cstring)
static char * text2cstring(const text *textptr)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)