PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geography_as_gml()

Datum geography_as_gml ( PG_FUNCTION_ARGS  )

Definition at line 210 of file geography_inout.c.

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

Referenced by geography_out().

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