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

Definition at line 196 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(), result, and SRID_DEFAULT.

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

Here is the call graph for this function: