PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ LWGEOM_asKML()

Datum LWGEOM_asKML ( PG_FUNCTION_ARGS  )

Definition at line 309 of file lwgeom_export.c.

References dumpnode::geom, lwfree(), LWGEOM_asGeoJson_old(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_to_kml2(), PG_FUNCTION_INFO_V1(), and precision.

Referenced by LWGEOM_asGML().

310 {
311  GSERIALIZED *geom;
312  LWGEOM *lwgeom;
313  char *kml;
314  text *result;
315  int version;
316  int precision = DBL_DIG;
317  static const char* default_prefix = ""; /* default prefix */
318  char *prefixbuf;
319  const char* prefix = default_prefix;
320  text *prefix_text;
321 
322 
323  /* Get the version */
324  version = PG_GETARG_INT32(0);
325  if ( version != 2)
326  {
327  elog(ERROR, "Only KML 2 is supported");
328  PG_RETURN_NULL();
329  }
330 
331  /* Get the geometry */
332  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
333  geom = PG_GETARG_GSERIALIZED_P(1);
334 
335  /* Retrieve precision if any (default is max) */
336  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
337  {
338  /* TODO: leave this to liblwgeom ? */
339  precision = PG_GETARG_INT32(2);
340  if ( precision > DBL_DIG )
341  precision = DBL_DIG;
342  else if ( precision < 0 ) precision = 0;
343  }
344 
345  /* retrieve prefix */
346  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
347  {
348  prefix_text = PG_GETARG_TEXT_P(3);
349  if ( VARSIZE(prefix_text)-VARHDRSZ == 0 )
350  {
351  prefix = "";
352  }
353  else
354  {
355  /* +2 is one for the ':' and one for term null */
356  prefixbuf = palloc(VARSIZE(prefix_text)-VARHDRSZ+2);
357  memcpy(prefixbuf, VARDATA(prefix_text),
358  VARSIZE(prefix_text)-VARHDRSZ);
359  /* add colon and null terminate */
360  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ] = ':';
361  prefixbuf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0';
362  prefix = prefixbuf;
363  }
364  }
365 
366  lwgeom = lwgeom_from_gserialized(geom);
367  kml = lwgeom_to_kml2(lwgeom, precision, prefix);
368  lwgeom_free(lwgeom);
369  PG_FREE_IF_COPY(geom, 1);
370 
371  if( ! kml )
372  PG_RETURN_NULL();
373 
374  result = cstring2text(kml);
375  lwfree(kml);
376 
377  PG_RETURN_POINTER(result);
378 }
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
uint8_t precision
Definition: cu_in_twkb.c:25
char * lwgeom_to_kml2(const LWGEOM *geom, int precision, const char *prefix)
Definition: lwout_kml.c:43
Here is the call graph for this function:
Here is the caller graph for this function: