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