PostGIS  3.4.0dev-r@@SVN_REVISION@@
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages

◆ LWGEOM_asKML()

Datum LWGEOM_asKML ( PG_FUNCTION_ARGS  )

Definition at line 240 of file postgis/lwgeom_transform.c.

241 {
242  LWGEOM *lwgeom;
243  lwvarlena_t *kml;
244  const char *default_prefix = ""; /* default prefix */
245  char *prefixbuf;
246  const char *prefix = default_prefix;
247  int32_t srid_from;
248  const int32_t srid_to = 4326;
249 
250  /* Get the geometry */
251  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P_COPY(0);
252  int precision = PG_GETARG_INT32(1);
253  text *prefix_text = PG_GETARG_TEXT_P(2);
254  srid_from = gserialized_get_srid(geom);
255 
256  if ( srid_from == SRID_UNKNOWN )
257  {
258  PG_FREE_IF_COPY(geom, 0);
259  elog(ERROR, "ST_AsKML: Input geometry has unknown (%d) SRID", SRID_UNKNOWN);
260  PG_RETURN_NULL();
261  }
262 
263  /* Condition precision */
264  if (precision < 0)
265  precision = 0;
266 
267  if (VARSIZE_ANY_EXHDR(prefix_text) > 0)
268  {
269  /* +2 is one for the ':' and one for term null */
270  prefixbuf = palloc(VARSIZE_ANY_EXHDR(prefix_text)+2);
271  memcpy(prefixbuf, VARDATA(prefix_text),
272  VARSIZE_ANY_EXHDR(prefix_text));
273  /* add colon and null terminate */
274  prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)] = ':';
275  prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)+1] = '\0';
276  prefix = prefixbuf;
277  }
278 
279  lwgeom = lwgeom_from_gserialized(geom);
280 
281  if (srid_from != srid_to)
282  {
283  LWPROJ *pj;
284  if (lwproj_lookup(srid_from, srid_to, &pj) == LW_FAILURE)
285  {
286  PG_FREE_IF_COPY(geom, 0);
287  elog(ERROR, "ST_AsKML: Failure reading projections from spatial_ref_sys.");
288  PG_RETURN_NULL();
289  }
290  lwgeom_transform(lwgeom, pj);
291  }
292 
293  kml = lwgeom_to_kml2(lwgeom, precision, prefix);
294  if (kml)
295  PG_RETURN_TEXT_P(kml);
296  PG_RETURN_NULL();
297 }
static uint8_t precision
Definition: cu_in_twkb.c:25
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:126
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
lwvarlena_t * lwgeom_to_kml2(const LWGEOM *geom, int precision, const char *prefix)
Definition: lwout_kml.c:44
#define LW_FAILURE
Definition: liblwgeom.h:96
int lwgeom_transform(LWGEOM *geom, LWPROJ *pj)
Transform (reproject) a geometry in-place.
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:215

References gserialized_get_srid(), LW_FAILURE, lwgeom_from_gserialized(), lwgeom_to_kml2(), lwgeom_transform(), precision, and SRID_UNKNOWN.

Here is the call graph for this function: