PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum LWGEOM_asGML ( PG_FUNCTION_ARGS  )

Definition at line 170 of file lwgeom_export.c.

References dumpnode::geom, getSRSbySRID(), gserialized_get_srid(), LW_GML_EXTENT, LW_GML_IS_DEGREE, LW_GML_IS_DIMS, LW_GML_SHORTLINE, lwfree(), lwgeom_extent_to_gml2(), lwgeom_extent_to_gml3(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_to_gml2(), lwgeom_to_gml3(), precision, and SRID_UNKNOWN.

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 = PG_GETARG_GSERIALIZED_P(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 }
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:218
#define LW_GML_SHORTLINE
For GML3, use rather than for lines.
Definition: liblwgeom.h:1473
void lwfree(void *mem)
Definition: lwutil.c:214
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
LWGEOM * geom
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:172
#define LW_GML_EXTENT
For GML2 and GML3, output only extent of geometry.
Definition: liblwgeom.h:1475
uint8_t precision
Definition: cu_in_twkb.c:25
char * lwgeom_extent_to_gml2(const LWGEOM *geom, const char *srs, int precision, const char *prefix)
Definition: lwout_gml.c:185
char * lwgeom_extent_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix)
Definition: lwout_gml.c:200
char * getSRSbySRID(int srid, bool short_crs)
Definition: lwgeom_export.c:40
#define LW_GML_IS_DIMS
Macros for specifying GML options.
Definition: liblwgeom.h:1469
char * lwgeom_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix, const char *id)
Definition: lwout_gml.c:723
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:69
#define LW_GML_IS_DEGREE
For GML3 only, declare that datas are lat/lon.
Definition: liblwgeom.h:1471

Here is the call graph for this function: