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

Definition at line 179 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.

180 {
181  GSERIALIZED *geom;
182  LWGEOM *lwgeom;
183  char *gml = NULL;
184  text *result;
185  int version;
186  char *srs;
187  int srid;
188  int option = 0;
189  int lwopts = LW_GML_IS_DIMS;
190  int precision = DBL_DIG;
191  static const char* default_prefix = "gml:"; /* default prefix */
192  const char* prefix = default_prefix;
193  const char* gml_id = NULL;
194  size_t len;
195  char *gml_id_buf, *prefix_buf;
196  text *prefix_text, *gml_id_text;
197 
198 
199  /* Get the version */
200  version = PG_GETARG_INT32(0);
201  if ( version != 2 && version != 3 )
202  {
203  elog(ERROR, "Only GML 2 and GML 3 are supported");
204  PG_RETURN_NULL();
205  }
206 
207  /* Get the geometry */
208  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
209  geom = PG_GETARG_GSERIALIZED_P(1);
210 
211  /* Retrieve precision if any (default is max) */
212  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
213  {
214  precision = PG_GETARG_INT32(2);
215  /* TODO: leave this to liblwgeom ? */
216  if (precision > DBL_DIG)
217  precision = DBL_DIG;
218  else if (precision < 0)
219  precision = 0;
220  }
221 
222  /* retrieve option */
223  if (PG_NARGS() > 3 && !PG_ARGISNULL(3)) option = PG_GETARG_INT32(3);
224 
225  /* retrieve prefix */
226  if (PG_NARGS() >4 && !PG_ARGISNULL(4))
227  {
228  prefix_text = PG_GETARG_TEXT_P(4);
229  if ( VARSIZE(prefix_text) == VARHDRSZ )
230  {
231  prefix = "";
232  }
233  else
234  {
235  len = VARSIZE(prefix_text)-VARHDRSZ;
236  prefix_buf = palloc(len + 2); /* +2 is one for the ':' and one for term null */
237  memcpy(prefix_buf, VARDATA(prefix_text), len);
238  /* add colon and null terminate */
239  prefix_buf[len] = ':';
240  prefix_buf[len+1] = '\0';
241  prefix = prefix_buf;
242  }
243  }
244 
245  if (PG_NARGS() >5 && !PG_ARGISNULL(5))
246  {
247  gml_id_text = PG_GETARG_TEXT_P(5);
248  if ( VARSIZE(gml_id_text) == VARHDRSZ )
249  {
250  gml_id = "";
251  }
252  else
253  {
254  len = VARSIZE(gml_id_text)-VARHDRSZ;
255  gml_id_buf = palloc(len+1);
256  memcpy(gml_id_buf, VARDATA(gml_id_text), len);
257  gml_id_buf[len] = '\0';
258  gml_id = gml_id_buf;
259  }
260  }
261 
262  srid = gserialized_get_srid(geom);
263  if (srid == SRID_UNKNOWN) srs = NULL;
264  else if (option & 1) srs = getSRSbySRID(srid, false);
265  else srs = getSRSbySRID(srid, true);
266 
267  if (option & 2) lwopts &= ~LW_GML_IS_DIMS;
268  if (option & 4) lwopts |= LW_GML_SHORTLINE;
269  if (option & 8)
270  {
271  elog(ERROR,
272  "Options %d passed to ST_AsGML(geography) sets "
273  "unsupported value 8",
274  option);
275  PG_RETURN_NULL();
276  }
277  if (option & 16) lwopts |= LW_GML_IS_DEGREE;
278  if (option & 32) lwopts |= LW_GML_EXTENT;
279 
280  lwgeom = lwgeom_from_gserialized(geom);
281 
282  if (version == 2)
283  {
284  if (lwopts & LW_GML_EXTENT)
285  gml = lwgeom_extent_to_gml2(
286  lwgeom, srs, precision, prefix);
287  else
288  gml = lwgeom_to_gml2(lwgeom, srs, precision, prefix);
289  }
290  if (version == 3)
291  {
292  if (lwopts & LW_GML_EXTENT)
293  gml = lwgeom_extent_to_gml3(
294  lwgeom, srs, precision, lwopts, prefix);
295  else
296  gml = lwgeom_to_gml3(
297  lwgeom, srs, precision, lwopts, prefix, gml_id);
298  }
299 
300  lwgeom_free(lwgeom);
301  PG_FREE_IF_COPY(geom, 1);
302 
303  /* Return null on null */
304  if ( ! gml )
305  PG_RETURN_NULL();
306 
307  result = cstring2text(gml);
308  lwfree(gml);
309  PG_RETURN_TEXT_P(result);
310 }
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:231
#define LW_GML_SHORTLINE
For GML3, use rather than for lines.
Definition: liblwgeom.h:1539
void lwfree(void *mem)
Definition: lwutil.c:244
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1137
LWGEOM * geom
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
#define LW_GML_EXTENT
For GML2 and GML3, output only extent of geometry.
Definition: liblwgeom.h:1541
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:198
char * lwgeom_extent_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix)
Definition: lwout_gml.c:213
char * getSRSbySRID(int srid, bool short_crs)
Definition: lwgeom_export.c:55
#define LW_GML_IS_DIMS
Macros for specifying GML options.
Definition: liblwgeom.h:1535
char * lwgeom_to_gml3(const LWGEOM *geom, const char *srs, int precision, int opts, const char *prefix, const char *id)
Definition: lwout_gml.c:717
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:99
#define LW_GML_IS_DEGREE
For GML3 only, declare that datas are lat/lon.
Definition: liblwgeom.h:1537

Here is the call graph for this function: