PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ LWGEOM_asGML()

Datum LWGEOM_asGML ( PG_FUNCTION_ARGS  )

Definition at line 188 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().

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