PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
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_cache.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 */
40static text*
41cstring2text(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
51static char*
52text2cstring(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
62Datum geom_from_geojson(PG_FUNCTION_ARGS);
63Datum postgis_libjson_version(PG_FUNCTION_ARGS);
64
66Datum 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
82Datum 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 lwfree(geojson);
105 if (!lwgeom)
106 {
107 /* Shouldn't get here */
108 elog(ERROR, "lwgeom_from_geojson returned NULL");
109 PG_RETURN_NULL();
110 }
111
112 if (srs)
113 {
114 srid = GetSRIDCacheBySRS(fcinfo, srs);
115 lwfree(srs);
116 }
117
118 lwgeom_set_srid(lwgeom, srid);
119 geom = geometry_serialize(lwgeom);
120 lwgeom_free(lwgeom);
121
122 PG_RETURN_POINTER(geom);
123#endif
124}
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition cu_print.c:267
#define WGS84_SRID
Definition liblwgeom.h:149
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:1638
void lwgeom_free(LWGEOM *geom)
Definition lwgeom.c:1246
void * lwalloc(size_t size)
Definition lwutil.c:227
void lwfree(void *mem)
Definition lwutil.c:248
LWGEOM * lwgeom_from_geojson(const char *geojson, char **srs)
Create an LWGEOM object from a GeoJSON representation.
This library is the generic geometry handling section of PostGIS.
#define str(s)
static char * text2cstring(const text *textptr)
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)