PostGIS  3.6.1dev-r@@SVN_REVISION@@

◆ LWGEOM_asKML()

Datum LWGEOM_asKML ( PG_FUNCTION_ARGS  )

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

256 {
257  LWGEOM *lwgeom;
258  lwvarlena_t *kml;
259  const char *default_prefix = ""; /* default prefix */
260  char *prefixbuf;
261  const char *prefix = default_prefix;
262  int32_t srid_from;
263  const int32_t srid_to = 4326;
264 
265  /* Get the geometry */
266  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P_COPY(0);
267  int precision = PG_GETARG_INT32(1);
268  text *prefix_text = PG_GETARG_TEXT_P(2);
269  srid_from = gserialized_get_srid(geom);
270 
271  if ( srid_from == SRID_UNKNOWN )
272  {
273  PG_FREE_IF_COPY(geom, 0);
274  elog(ERROR, "ST_AsKML: Input geometry has unknown (%d) SRID", SRID_UNKNOWN);
275  PG_RETURN_NULL();
276  }
277 
278  /* Condition precision */
279  if (precision < 0)
280  precision = 0;
281 
282  if (VARSIZE_ANY_EXHDR(prefix_text) > 0)
283  {
284  /* +2 is one for the ':' and one for term null */
285  prefixbuf = palloc(VARSIZE_ANY_EXHDR(prefix_text)+2);
286  memcpy(prefixbuf, VARDATA(prefix_text),
287  VARSIZE_ANY_EXHDR(prefix_text));
288  /* add colon and null terminate */
289  prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)] = ':';
290  prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)+1] = '\0';
291  prefix = prefixbuf;
292  }
293 
294  lwgeom = lwgeom_from_gserialized(geom);
295 
296  if (srid_from != srid_to)
297  {
298  LWPROJ *pj;
299  if (lwproj_lookup(srid_from, srid_to, &pj) == LW_FAILURE)
300  {
301  PG_FREE_IF_COPY(geom, 0);
302  elog(ERROR, "ST_AsKML: Failure reading projections from spatial_ref_sys.");
303  PG_RETURN_NULL();
304  }
305  lwgeom_transform(lwgeom, pj);
306  }
307 
308  kml = lwgeom_to_kml2(lwgeom, precision, prefix);
309  if (kml)
310  PG_RETURN_TEXT_P(kml);
311  PG_RETURN_NULL();
312 }
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:155
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:268
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: