PostGIS  2.1.10dev-r@@SVN_REVISION@@
lwgeom_export.c
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id:$
3  *
4  * PostGIS - Export functions for PostgreSQL/PostGIS
5  * Copyright 2009-2011 Olivier Courtin <olivier.courtin@oslandia.com>
6  *
7  * This is free software; you can redistribute and/or modify it under
8  * the terms of the GNU General Public Licence. See the COPYING file.
9  *
10  **********************************************************************/
11 
12 
17 #include "float.h" /* for DBL_DIG */
18 #include "postgres.h"
19 #include "executor/spi.h"
20 
21 #include "../postgis_config.h"
22 #include "lwgeom_pg.h"
23 #include "liblwgeom.h"
24 #include "lwgeom_export.h"
25 
26 Datum LWGEOM_asGML(PG_FUNCTION_ARGS);
27 Datum LWGEOM_asKML(PG_FUNCTION_ARGS);
28 Datum LWGEOM_asGeoJson(PG_FUNCTION_ARGS);
29 Datum LWGEOM_asSVG(PG_FUNCTION_ARGS);
30 Datum LWGEOM_asX3D(PG_FUNCTION_ARGS);
31 
32 /*
33  * Retrieve an SRS from a given SRID
34  * Require valid spatial_ref_sys table entry
35  *
36  * Could return SRS as short one (i.e EPSG:4326)
37  * or as long one: (i.e urn:ogc:def:crs:EPSG::4326)
38  */
39 char * getSRSbySRID(int srid, bool short_crs)
40 {
41  char query[256];
42  char *srs, *srscopy;
43  int size, err;
44 
45  if (SPI_OK_CONNECT != SPI_connect ())
46  {
47  elog(NOTICE, "getSRSbySRID: could not connect to SPI manager");
48  SPI_finish();
49  return NULL;
50  }
51 
52  if (short_crs)
53  sprintf(query, "SELECT auth_name||':'||auth_srid \
54  FROM spatial_ref_sys WHERE srid='%d'", srid);
55  else
56  sprintf(query, "SELECT 'urn:ogc:def:crs:'||auth_name||'::'||auth_srid \
57  FROM spatial_ref_sys WHERE srid='%d'", srid);
58 
59  err = SPI_exec(query, 1);
60  if ( err < 0 )
61  {
62  elog(NOTICE, "getSRSbySRID: error executing query %d", err);
63  SPI_finish();
64  return NULL;
65  }
66 
67  /* no entry in spatial_ref_sys */
68  if (SPI_processed <= 0)
69  {
70  SPI_finish();
71  return NULL;
72  }
73 
74  /* get result */
75  srs = SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);
76 
77  /* NULL result */
78  if ( ! srs )
79  {
80  SPI_finish();
81  return NULL;
82  }
83 
84  /* copy result to upper executor context */
85  size = strlen(srs)+1;
86  srscopy = SPI_palloc(size);
87  memcpy(srscopy, srs, size);
88 
89  /* disconnect from SPI */
90  SPI_finish();
91 
92  return srscopy;
93 }
94 
95 
96 /*
97 * Retrieve an SRID from a given SRS
98 * Require valid spatial_ref_sys table entry
99 *
100 */
101 int getSRIDbySRS(const char* srs)
102 {
103  char query[256];
104  int srid, err;
105 
106  if (srs == NULL)
107  return 0;
108 
109  if (SPI_OK_CONNECT != SPI_connect ())
110  {
111  elog(NOTICE, "getSRIDbySRS: could not connect to SPI manager");
112  SPI_finish();
113  return 0;
114  }
115 
116  sprintf(query,
117  "SELECT srid "
118  "FROM spatial_ref_sys, "
119  "regexp_matches('%s', E'([a-z]+):([0-9]+)', 'gi') AS re "
120  "WHERE re[1] ILIKE auth_name AND int4(re[2]) = auth_srid", srs);
121 
122  err = SPI_exec(query, 1);
123  if ( err < 0 )
124  {
125  elog(NOTICE, "getSRIDbySRS: error executing query %d", err);
126  SPI_finish();
127  return 0;
128  }
129 
130  /* no entry in spatial_ref_sys */
131  if (SPI_processed <= 0)
132  {
133  sprintf(query,
134  "SELECT srid "
135  "FROM spatial_ref_sys, "
136  "regexp_matches('%s', E'urn:ogc:def:crs:([a-z]+):.*:([0-9]+)', 'gi') AS re "
137  "WHERE re[1] ILIKE auth_name AND int4(re[2]) = auth_srid", srs);
138 
139  err = SPI_exec(query, 1);
140  if ( err < 0 )
141  {
142  elog(NOTICE, "getSRIDbySRS: error executing query %d", err);
143  SPI_finish();
144  return 0;
145  }
146 
147  if (SPI_processed <= 0) {
148  SPI_finish();
149  return 0;
150  }
151  }
152 
153  srid = atoi(SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1));
154  if ( ! srs )
155  {
156  SPI_finish();
157  return 0;
158  }
159 
160  SPI_finish();
161 
162  return srid;
163 }
164 
165 
170 Datum LWGEOM_asGML(PG_FUNCTION_ARGS)
171 {
172  GSERIALIZED *geom;
173  LWGEOM *lwgeom;
174  char *gml = NULL;
175  text *result;
176  int version;
177  char *srs;
178  int srid;
179  int option = 0;
180  int lwopts = LW_GML_IS_DIMS;
181  int precision = DBL_DIG;
182  static const char* default_prefix = "gml:"; /* default prefix */
183  const char* prefix = default_prefix;
184  const char* gml_id = NULL;
185  size_t len;
186  char *gml_id_buf, *prefix_buf;
187  text *prefix_text, *gml_id_text;
188 
189 
190  /* Get the version */
191  version = PG_GETARG_INT32(0);
192  if ( version != 2 && version != 3 )
193  {
194  elog(ERROR, "Only GML 2 and GML 3 are supported");
195  PG_RETURN_NULL();
196  }
197 
198  /* Get the geometry */
199  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
200  geom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
201 
202  /* Retrieve precision if any (default is max) */
203  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
204  {
205  precision = PG_GETARG_INT32(2);
206  /* TODO: leave this to liblwgeom ? */
207  if ( precision > DBL_DIG )
208  precision = DBL_DIG;
209  else if ( precision < 0 ) precision = 0;
210  }
211 
212  /* retrieve option */
213  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
214  option = PG_GETARG_INT32(3);
215 
216  /* retrieve prefix */
217  if (PG_NARGS() >4 && !PG_ARGISNULL(4))
218  {
219  prefix_text = PG_GETARG_TEXT_P(4);
220  if ( VARSIZE(prefix_text) == VARHDRSZ )
221  {
222  prefix = "";
223  }
224  else
225  {
226  len = VARSIZE(prefix_text)-VARHDRSZ;
227  prefix_buf = palloc(len + 2); /* +2 is one for the ':' and one for term null */
228  memcpy(prefix_buf, VARDATA(prefix_text), len);
229  /* add colon and null terminate */
230  prefix_buf[len] = ':';
231  prefix_buf[len+1] = '\0';
232  prefix = prefix_buf;
233  }
234  }
235 
236  if (PG_NARGS() >5 && !PG_ARGISNULL(5))
237  {
238  gml_id_text = PG_GETARG_TEXT_P(5);
239  if ( VARSIZE(gml_id_text) == VARHDRSZ )
240  {
241  gml_id = "";
242  }
243  else
244  {
245  len = VARSIZE(gml_id_text)-VARHDRSZ;
246  gml_id_buf = palloc(len+1);
247  memcpy(gml_id_buf, VARDATA(gml_id_text), len);
248  gml_id_buf[len] = '\0';
249  gml_id = gml_id_buf;
250  }
251  }
252 
253  srid = gserialized_get_srid(geom);
254  if (srid == SRID_UNKNOWN) srs = NULL;
255  else if (option & 1) srs = getSRSbySRID(srid, false);
256  else srs = getSRSbySRID(srid, true);
257 
258  if (option & 2) lwopts &= ~LW_GML_IS_DIMS;
259  if (option & 4) lwopts |= LW_GML_SHORTLINE;
260  if (option & 16) lwopts |= LW_GML_IS_DEGREE;
261  if (option & 32) lwopts |= LW_GML_EXTENT;
262 
263  lwgeom = lwgeom_from_gserialized(geom);
264 
265  if (version == 2 && lwopts & LW_GML_EXTENT)
266  gml = lwgeom_extent_to_gml2(lwgeom, srs, precision, prefix);
267  else if (version == 2)
268  gml = lwgeom_to_gml2(lwgeom, srs, precision, prefix);
269  else if (version == 3 && lwopts & LW_GML_EXTENT)
270  gml = lwgeom_extent_to_gml3(lwgeom, srs, precision, lwopts, prefix);
271  else if (version == 3)
272  gml = lwgeom_to_gml3(lwgeom, srs, precision, lwopts, prefix, gml_id);
273 
274  lwgeom_free(lwgeom);
275  PG_FREE_IF_COPY(geom, 1);
276 
277  /* Return null on null */
278  if ( ! gml )
279  PG_RETURN_NULL();
280 
281  result = cstring2text(gml);
282  lwfree(gml);
283  PG_RETURN_TEXT_P(result);
284 }
285 
286 
291 Datum LWGEOM_asKML(PG_FUNCTION_ARGS)
292 {
293  GSERIALIZED *geom;
294  LWGEOM *lwgeom;
295  char *kml;
296  text *result;
297  int version;
298  int precision = DBL_DIG;
299  static const char* default_prefix = ""; /* default prefix */
300  char *prefixbuf;
301  const char* prefix = default_prefix;
302  text *prefix_text;
303 
304 
305  /* Get the version */
306  version = PG_GETARG_INT32(0);
307  if ( version != 2)
308  {
309  elog(ERROR, "Only KML 2 is supported");
310  PG_RETURN_NULL();
311  }
312 
313  /* Get the geometry */
314  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
315  geom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
316 
317  /* Retrieve precision if any (default is max) */
318  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
319  {
320  /* TODO: leave this to liblwgeom ? */
321  precision = PG_GETARG_INT32(2);
322  if ( precision > DBL_DIG )
323  precision = DBL_DIG;
324  else if ( precision < 0 ) precision = 0;
325  }
326 
327  /* retrieve prefix */
328  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
329  {
330  prefix_text = PG_GETARG_TEXT_P(3);
331  if ( VARSIZE(prefix_text)-VARHDRSZ == 0 )
332  {
333  prefix = "";
334  }
335  else
336  {
337  /* +2 is one for the ':' and one for term null */
338  prefixbuf = palloc(VARSIZE(prefix_text)-VARHDRSZ+2);
339  memcpy(prefixbuf, VARDATA(prefix_text),
340  VARSIZE(prefix_text)-VARHDRSZ);
341  /* add colon and null terminate */
342  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ] = ':';
343  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0';
344  prefix = prefixbuf;
345  }
346  }
347 
348  lwgeom = lwgeom_from_gserialized(geom);
349  kml = lwgeom_to_kml2(lwgeom, precision, prefix);
350  lwgeom_free(lwgeom);
351  PG_FREE_IF_COPY(geom, 1);
352 
353  if( ! kml )
354  PG_RETURN_NULL();
355 
356  result = cstring2text(kml);
357  lwfree(kml);
358 
359  PG_RETURN_POINTER(result);
360 }
361 
362 
367 Datum LWGEOM_asGeoJson(PG_FUNCTION_ARGS)
368 {
369  GSERIALIZED *geom;
370  LWGEOM *lwgeom;
371  char *geojson;
372  text *result;
373  int srid;
374  int version;
375  int option = 0;
376  int has_bbox = 0;
377  int precision = DBL_DIG;
378  char * srs = NULL;
379 
380  /* Get the version */
381  version = PG_GETARG_INT32(0);
382  if ( version != 1)
383  {
384  elog(ERROR, "Only GeoJSON 1 is supported");
385  PG_RETURN_NULL();
386  }
387 
388  /* Get the geometry */
389  if (PG_ARGISNULL(1) ) PG_RETURN_NULL();
390  geom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
391 
392  /* Retrieve precision if any (default is max) */
393  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
394  {
395  precision = PG_GETARG_INT32(2);
396  if ( precision > DBL_DIG )
397  precision = DBL_DIG;
398  else if ( precision < 0 ) precision = 0;
399  }
400 
401  /* Retrieve output option
402  * 0 = without option (default)
403  * 1 = bbox
404  * 2 = short crs
405  * 4 = long crs
406  */
407  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
408  option = PG_GETARG_INT32(3);
409 
410  if (option & 2 || option & 4)
411  {
412  srid = gserialized_get_srid(geom);
413  if ( srid != SRID_UNKNOWN )
414  {
415  if (option & 2) srs = getSRSbySRID(srid, true);
416  if (option & 4) srs = getSRSbySRID(srid, false);
417  if (!srs)
418  {
419  elog( ERROR,
420  "SRID %i unknown in spatial_ref_sys table",
421  srid);
422  PG_RETURN_NULL();
423  }
424  }
425  }
426 
427  if (option & 1) has_bbox = 1;
428 
429  lwgeom = lwgeom_from_gserialized(geom);
430  geojson = lwgeom_to_geojson(lwgeom, srs, precision, has_bbox);
431  lwgeom_free(lwgeom);
432 
433  PG_FREE_IF_COPY(geom, 1);
434  if (srs) pfree(srs);
435 
436  result = cstring2text(geojson);
437 
438  lwfree(geojson);
439 
440  PG_RETURN_TEXT_P(result);
441 }
442 
443 
448 Datum LWGEOM_asSVG(PG_FUNCTION_ARGS)
449 {
450  GSERIALIZED *geom;
451  LWGEOM *lwgeom;
452  char *svg;
453  text *result;
454  int relative = 0;
455  int precision=DBL_DIG;
456 
457  if ( PG_ARGISNULL(0) ) PG_RETURN_NULL();
458 
459  geom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
460 
461  /* check for relative path notation */
462  if ( PG_NARGS() > 1 && ! PG_ARGISNULL(1) )
463  relative = PG_GETARG_INT32(1) ? 1:0;
464 
465  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
466  {
467  precision = PG_GETARG_INT32(2);
468  /* TODO: leave this to liblwgeom ? */
469  if ( precision > DBL_DIG )
470  precision = DBL_DIG;
471  else if ( precision < 0 ) precision = 0;
472  }
473 
474  lwgeom = lwgeom_from_gserialized(geom);
475  svg = lwgeom_to_svg(lwgeom, precision, relative);
476  result = cstring2text(svg);
477  lwgeom_free(lwgeom);
478  pfree(svg);
479  PG_FREE_IF_COPY(geom, 0);
480 
481  PG_RETURN_TEXT_P(result);
482 }
483 
488 Datum LWGEOM_asX3D(PG_FUNCTION_ARGS)
489 {
490  GSERIALIZED *geom;
491  LWGEOM *lwgeom;
492  char *x3d;
493  text *result;
494  int version;
495  char *srs;
496  int srid;
497  int option = 0;
498  int precision = DBL_DIG;
499  static const char* default_defid = "x3d:"; /* default defid */
500  char *defidbuf;
501  const char* defid = default_defid;
502  text *defid_text;
503 
504  /* Get the version */
505  version = PG_GETARG_INT32(0);
506  if ( version != 3 )
507  {
508  elog(ERROR, "Only X3D version 3 are supported");
509  PG_RETURN_NULL();
510  }
511 
512  /* Get the geometry */
513  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
514  geom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
515 
516  /* Retrieve precision if any (default is max) */
517  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
518  {
519  precision = PG_GETARG_INT32(2);
520  /* TODO: leave this to liblwgeom ? */
521  if ( precision > DBL_DIG )
522  precision = DBL_DIG;
523  else if ( precision < 0 ) precision = 0;
524  }
525 
526  /* retrieve option */
527  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
528  option = PG_GETARG_INT32(3);
529 
530  /* retrieve defid */
531  if (PG_NARGS() >4 && !PG_ARGISNULL(4))
532  {
533  defid_text = PG_GETARG_TEXT_P(4);
534  if ( VARSIZE(defid_text)-VARHDRSZ == 0 )
535  {
536  defid = "";
537  }
538  else
539  {
540  /* +2 is one for the ':' and one for term null */
541  defidbuf = palloc(VARSIZE(defid_text)-VARHDRSZ+2);
542  memcpy(defidbuf, VARDATA(defid_text),
543  VARSIZE(defid_text)-VARHDRSZ);
544  /* add colon and null terminate */
545  defidbuf[VARSIZE(defid_text)-VARHDRSZ] = ':';
546  defidbuf[VARSIZE(defid_text)-VARHDRSZ+1] = '\0';
547  defid = defidbuf;
548  }
549  }
550 
551  srid = gserialized_get_srid(geom);
552  if (srid == SRID_UNKNOWN) srs = NULL;
553  else if (option & 1) srs = getSRSbySRID(srid, false);
554  else srs = getSRSbySRID(srid, true);
555 
556  lwgeom = lwgeom_from_gserialized(geom);
557 
558  x3d = lwgeom_to_x3d3(lwgeom, srs, precision,option, defid);
559 
560  lwgeom_free(lwgeom);
561  PG_FREE_IF_COPY(geom, 1);
562 
563  result = cstring2text(x3d);
564  lwfree(x3d);
565 
566  PG_RETURN_TEXT_P(result);
567 }
char * lwgeom_to_gml2(const LWGEOM *geom, const char *srs, int precision, const char *prefix)
VERSION GML 2 takes a GEOMETRY and returns a GML2 representation.
Definition: lwout_gml.c:211
Datum LWGEOM_asX3D(PG_FUNCTION_ARGS)
#define LW_GML_SHORTLINE
For GML3, use rather than for lines.
Definition: liblwgeom.h:1362
char * lwgeom_to_x3d3(const LWGEOM *geom, char *srs, int precision, int opts, const char *defid)
Definition: lwout_x3d.c:49
void lwfree(void *mem)
Definition: lwutil.c:190
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
char * lwgeom_to_svg(const LWGEOM *geom, int precision, int relative)
Takes a GEOMETRY and returns a SVG representation.
Definition: lwout_svg.c:43
PG_FUNCTION_INFO_V1(LWGEOM_asGML)
Encode feature in GML.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
Datum LWGEOM_asSVG(PG_FUNCTION_ARGS)
char ** result
Definition: liblwgeom.h:218
LWGEOM * geom
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:154
char * lwgeom_to_geojson(const LWGEOM *geo, char *srs, int precision, int has_bbox)
Takes a GEOMETRY and returns a GeoJson representation.
Definition: lwout_geojson.c:35
#define LW_GML_EXTENT
For GML2 and GML3, output only extent of geometry.
Definition: liblwgeom.h:1364
Datum LWGEOM_asGeoJson(PG_FUNCTION_ARGS)
char * lwgeom_extent_to_gml2(const LWGEOM *geom, const char *srs, int precision, const char *prefix)
Definition: lwout_gml.c:178
int getSRIDbySRS(const char *srs)
char * lwgeom_extent_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix)
Definition: lwout_gml.c:193
Datum LWGEOM_asKML(PG_FUNCTION_ARGS)
char * getSRSbySRID(int srid, bool short_crs)
Definition: lwgeom_export.c:39
#define LW_GML_IS_DIMS
Macros for specifying GML options.
Definition: liblwgeom.h:1358
char * lwgeom_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix, const char *id)
Definition: lwout_gml.c:711
Datum LWGEOM_asGML(PG_FUNCTION_ARGS)
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:70
This library is the generic geometry handling section of PostGIS.
char * lwgeom_to_kml2(const LWGEOM *geom, int precision, const char *prefix)
Definition: lwout_kml.c:32
#define LW_GML_IS_DEGREE
For GML3 only, declare that datas are lat/lon.
Definition: liblwgeom.h:1360