PostGIS  2.1.10dev-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 Datum geom_from_geojson(PG_FUNCTION_ARGS);
21 Datum postgis_libjson_version(PG_FUNCTION_ARGS);
22 
24 Datum postgis_libjson_version(PG_FUNCTION_ARGS)
25 {
26 #ifndef HAVE_LIBJSON
27  PG_RETURN_NULL();
28 #else /* HAVE_LIBJSON */
29  const char *ver = "UNKNOWN";
30  text *result = cstring2text(ver);
31  PG_RETURN_POINTER(result);
32 #endif
33 }
34 
36 Datum geom_from_geojson(PG_FUNCTION_ARGS)
37 {
38 #ifndef HAVE_LIBJSON
39  elog(ERROR, "You need JSON-C for ST_GeomFromGeoJSON");
40  PG_RETURN_NULL();
41 #else /* HAVE_LIBJSON */
42 
43  GSERIALIZED *geom;
44  LWGEOM *lwgeom;
45  text *geojson_input;
46  char *geojson;
47  char *srs = NULL;
48 
49  /* Get the geojson stream */
50  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
51  geojson_input = PG_GETARG_TEXT_P(0);
52  geojson = text2cstring(geojson_input);
53 
54  lwgeom = lwgeom_from_geojson(geojson, &srs);
55  if ( ! lwgeom ) {
56  /* Shouldn't get here */
57  elog(ERROR, "lwgeom_from_geojson returned NULL");
58  PG_RETURN_NULL();
59  }
60 
61  if ( srs ) {
62  lwgeom_set_srid(lwgeom, getSRIDbySRS(srs));
63  lwfree(srs);
64  }
65 
66  geom = geometry_serialize(lwgeom);
67  lwgeom_free(lwgeom);
68 
69  PG_RETURN_POINTER(geom);
70 #endif
71 }
72 
Datum geom_from_geojson(PG_FUNCTION_ARGS)
void lwfree(void *mem)
Definition: lwutil.c:190
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
char ** result
Definition: liblwgeom.h:218
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:510