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

Definition at line 195 of file geography_inout.c.

References getSRSbySRID(), LW_GML_IS_DEGREE, LW_GML_IS_DIMS, lwfree(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_to_gml2(), lwgeom_to_gml3(), precision, and SRID_DEFAULT.

196 {
197  LWGEOM *lwgeom = NULL;
198  GSERIALIZED *g = NULL;
199  char *gml;
200  text *result;
201  int version;
202  char *srs;
203  int srid = SRID_DEFAULT;
204  int precision = DBL_DIG;
205  int option=0;
206  int lwopts = LW_GML_IS_DIMS;
207  static const char *default_prefix = "gml:";
208  const char *prefix = default_prefix;
209  char *prefix_buf = "";
210  text *prefix_text, *id_text = NULL;
211  const char *id=NULL;
212  char *id_buf;
213 
214 
215  /* Get the version */
216  version = PG_GETARG_INT32(0);
217  if ( version != 2 && version != 3 )
218  {
219  elog(ERROR, "Only GML 2 and GML 3 are supported");
220  PG_RETURN_NULL();
221  }
222 
223  /* Get the geography */
224  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
225  g = PG_GETARG_GSERIALIZED_P(1);
226 
227  /* Convert to lwgeom so we can run the old functions */
228  lwgeom = lwgeom_from_gserialized(g);
229 
230  /* Retrieve precision if any (default is max) */
231  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
232  {
233  precision = PG_GETARG_INT32(2);
234  /* TODO: leave this to liblwgeom */
235  if ( precision > DBL_DIG )
236  precision = DBL_DIG;
237  else if ( precision < 0 ) precision = 0;
238  }
239 
240  /* retrieve option */
241  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
242  option = PG_GETARG_INT32(3);
243 
244 
245  /* retrieve prefix */
246  if (PG_NARGS() >4 && !PG_ARGISNULL(4))
247  {
248  prefix_text = PG_GETARG_TEXT_P(4);
249  if ( VARSIZE(prefix_text)-VARHDRSZ == 0 )
250  {
251  prefix = "";
252  }
253  else
254  {
255  /* +2 is one for the ':' and one for term null */
256  prefix_buf = palloc(VARSIZE(prefix_text)-VARHDRSZ+2);
257  memcpy(prefix_buf, VARDATA(prefix_text),
258  VARSIZE(prefix_text)-VARHDRSZ);
259  /* add colon and null terminate */
260  prefix_buf[VARSIZE(prefix_text)-VARHDRSZ] = ':';
261  prefix_buf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0';
262  prefix = prefix_buf;
263  }
264  }
265 
266  /* retrieve id */
267  if (PG_NARGS() >5 && !PG_ARGISNULL(5))
268  {
269  id_text = PG_GETARG_TEXT_P(5);
270  if ( VARSIZE(id_text)-VARHDRSZ == 0 )
271  {
272  id = "";
273  }
274  else
275  {
276  id_buf = palloc(VARSIZE(id_text)-VARHDRSZ+1);
277  memcpy(id_buf, VARDATA(id_text), VARSIZE(id_text)-VARHDRSZ);
278  prefix_buf[VARSIZE(id_text)-VARHDRSZ+1] = '\0';
279  id = id_buf;
280  }
281  }
282 
283  if (option & 1) srs = getSRSbySRID(srid, false);
284  else srs = getSRSbySRID(srid, true);
285  if (!srs)
286  {
287  elog(ERROR, "SRID %d unknown in spatial_ref_sys table", SRID_DEFAULT);
288  PG_RETURN_NULL();
289  }
290 
291  /* Revert lat/lon only with long SRS */
292  if (option & 1) lwopts |= LW_GML_IS_DEGREE;
293  if (option & 2) lwopts &= ~LW_GML_IS_DIMS;
294 
295  if (version == 2)
296  gml = lwgeom_to_gml2(lwgeom, srs, precision, prefix);
297  else
298  gml = lwgeom_to_gml3(lwgeom, srs, precision, lwopts, prefix, id);
299 
300  lwgeom_free(lwgeom);
301  PG_FREE_IF_COPY(g, 1);
302 
303  /* Return null on null */
304  if ( ! gml )
305  PG_RETURN_NULL();
306 
307  /* Turn string result into text for return */
308  result = cstring2text(gml);
309  lwfree(gml);
310 
311  PG_RETURN_TEXT_P(result);
312 }
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
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
#define SRID_DEFAULT
Definition: liblwgeom.h:179
uint8_t precision
Definition: cu_in_twkb.c:25
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
#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: