PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ LWGEOM_asKML()

Datum LWGEOM_asKML ( PG_FUNCTION_ARGS  )

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

171 {
172  LWGEOM *lwgeom;
173  char *kml;
174  text *result;
175  const char *default_prefix = ""; /* default prefix */
176  char *prefixbuf;
177  const char *prefix = default_prefix;
178  int32_t srid_from;
179  const int32_t srid_to = 4326;
180 
181  /* Get the geometry */
182  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P_COPY(0);
183  int precision = PG_GETARG_INT32(1);
184  text *prefix_text = PG_GETARG_TEXT_P(2);
185  srid_from = gserialized_get_srid(geom);
186 
187  if ( srid_from == SRID_UNKNOWN )
188  {
189  PG_FREE_IF_COPY(geom, 0);
190  elog(ERROR, "ST_AsKML: Input geometry has unknown (%d) SRID", SRID_UNKNOWN);
191  PG_RETURN_NULL();
192  }
193 
194  /* Condition precision */
195  if (precision > DBL_DIG)
196  precision = DBL_DIG;
197  if (precision < 0)
198  precision = 0;
199 
200  if (VARSIZE_ANY_EXHDR(prefix_text) > 0)
201  {
202  /* +2 is one for the ':' and one for term null */
203  prefixbuf = palloc(VARSIZE_ANY_EXHDR(prefix_text)+2);
204  memcpy(prefixbuf, VARDATA(prefix_text),
205  VARSIZE_ANY_EXHDR(prefix_text));
206  /* add colon and null terminate */
207  prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)] = ':';
208  prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)+1] = '\0';
209  prefix = prefixbuf;
210  }
211 
212  lwgeom = lwgeom_from_gserialized(geom);
213 
214  if (srid_from != srid_to)
215  {
216  LWPROJ *pj;
217  if (GetPJUsingFCInfo(fcinfo, srid_from, srid_to, &pj) == LW_FAILURE)
218  {
219  PG_FREE_IF_COPY(geom, 0);
220  elog(ERROR, "ST_AsKML: Failure reading projections from spatial_ref_sys.");
221  PG_RETURN_NULL();
222  }
223  lwgeom_transform(lwgeom, pj);
224  }
225 
226  kml = lwgeom_to_kml2(lwgeom, precision, prefix);
227  lwgeom_free(lwgeom);
228  PG_FREE_IF_COPY(geom, 0);
229 
230  if (!kml)
231  PG_RETURN_NULL();
232 
233  result = cstring_to_text(kml);
234  lwfree(kml);
235 
236  PG_RETURN_POINTER(result);
237 }
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
#define LW_FAILURE
Definition: liblwgeom.h:110
int lwgeom_transform(LWGEOM *geom, LWPROJ *pj)
Transform (reproject) a geometry in-place.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
char * lwgeom_to_kml2(const LWGEOM *geom, int precision, const char *prefix)
Definition: lwout_kml.c:44
void lwfree(void *mem)
Definition: lwutil.c:242
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:229

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

Here is the call graph for this function: