PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ LWGEOM_asGML()

Datum LWGEOM_asGML ( PG_FUNCTION_ARGS  )

Definition at line 180 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_asKML(), lwgeom_extent_to_gml2(), lwgeom_extent_to_gml3(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_to_gml2(), lwgeom_to_gml3(), PG_FUNCTION_INFO_V1(), precision, and SRID_UNKNOWN.

Referenced by getSRIDbySRS().

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