41 #include "utils/builtins.h"
43 #include <libxml/tree.h>
44 #include <libxml/parser.h>
47 #include "../postgis_config.h"
48 #include "lwgeom_pg.h"
64 #define KML_NS ((char *) "http://www.opengis.net/kml/2.2")
81 xmlNodePtr xmlroot=NULL;
84 if (PG_ARGISNULL(0)) PG_RETURN_NULL();
85 xml_input = PG_GETARG_TEXT_P(0);
86 xml = text_to_cstring(xml_input);
87 xml_size = VARSIZE_ANY_EXHDR(xml_input);
91 xmldoc = xmlReadMemory(xml, xml_size, NULL, NULL, XML_PARSE_SAX1);
92 if (!xmldoc || (xmlroot = xmlDocGetRootElement(xmldoc)) == NULL)
96 lwpgerror(
"invalid KML representation");
122 geom = geometry_serialize(lwgeom);
128 PG_RETURN_POINTER(geom);
140 ns = xmlGetNsList(xnode->doc, xnode);
146 if (ns == NULL)
return !is_strict;
148 for (p=ns ; *p ; p++)
150 if ((*p)->href == NULL || (*p)->prefix == NULL ||
151 xnode->ns == NULL || xnode->ns->prefix == NULL)
continue;
153 if (!xmlStrcmp(xnode->ns->prefix, (*p)->prefix))
155 if (!strcmp((
char *) (*p)->href,
KML_NS))
177 static xmlChar *kmlGetProp(xmlNodePtr xnode, xmlChar *prop)
182 return xmlGetProp(xnode, prop);
184 value = xmlGetNsProp(xnode, prop, (xmlChar *)
KML_NS);
187 if (
value == NULL)
value = xmlGetNoNsProp(xnode, prop);
198 static double parse_kml_double(
char *d,
bool space_before,
bool space_after)
222 if (space_before)
while (isspace(*d)) d++;
223 for (st = INIT, p = d ; *p ; p++)
226 lwpgnotice(
"State: %d, *p=%c", st, *p);
230 if (st == INIT || st == NEED_DIG) st = DIG;
231 else if (st == NEED_DIG_DEC) st = DIG_DEC;
232 else if (st == NEED_DIG_EXP || st == EXP) st = DIG_EXP;
233 else if (st == DIG || st == DIG_DEC || st == DIG_EXP);
234 else lwpgerror(
"invalid KML representation");
238 if (st == DIG) st = NEED_DIG_DEC;
239 else lwpgerror(
"invalid KML representation");
241 else if (*p ==
'-' || *p ==
'+')
243 if (st == INIT) st = NEED_DIG;
244 else if (st == EXP) st = NEED_DIG_EXP;
245 else lwpgerror(
"invalid KML representation");
247 else if (*p ==
'e' || *p ==
'E')
249 if (st == DIG || st == DIG_DEC) st = EXP;
250 else lwpgerror(
"invalid KML representation");
252 else if (isspace(*p))
254 if (!space_after) lwpgerror(
"invalid KML representation");
255 if (st == DIG || st == DIG_DEC || st == DIG_EXP)st = END;
256 else if (st == NEED_DIG_DEC) st = END;
258 else lwpgerror(
"invalid KML representation");
260 else lwpgerror(
"invalid KML representation");
263 if (st != DIG && st != NEED_DIG_DEC && st != DIG_DEC && st != DIG_EXP && st != END)
264 lwpgerror(
"invalid KML representation");
279 int seen_kml_dims = 0;
285 if (xnode == NULL) lwpgerror(
"invalid KML representation");
287 for (found =
false ; xnode != NULL ; xnode = xnode->next)
289 if (xnode->type != XML_ELEMENT_NODE)
continue;
291 if (strcmp((
char *) xnode->name,
"coordinates"))
continue;
296 if (!found) lwpgerror(
"invalid KML representation");
299 kml_coord = xmlNodeGetContent(xnode);
300 p = (
char *) kml_coord;
310 while (*p && isspace(*p)) ++p;
311 for (kml_dims=0; *p ; p++)
314 if ( isdigit(*p) || *p ==
'+' || *p ==
'-' || *p ==
'.' ) {
316 errno = 0; d = strtod(p, &q);
319 lwpgerror(
"invalid KML representation");
321 if (kml_dims == 1) pt.
x = d;
322 else if (kml_dims == 2) pt.
y = d;
323 else if (kml_dims == 3) pt.
z = d;
325 lwpgerror(
"invalid KML representation");
331 if ( *q && ! isspace(*q) && *q !=
',' ) {
332 lwpgerror(
"invalid KML representation");
336 while (*q && isspace(*q)) ++q;
337 if ( isdigit(*q) || *q ==
'+' || *q ==
'-' || *q ==
'.' || ! *q ) {
338 if ( kml_dims < 2 ) lwpgerror(
"invalid KML representation");
339 else if ( kml_dims < 3 ) *hasz =
false;
340 if ( ! seen_kml_dims ) seen_kml_dims = kml_dims;
341 else if ( seen_kml_dims != kml_dims ) {
342 lwpgerror(
"invalid KML representation: mixed coordinates dimension");
349 }
else if ( *p !=
',' && ! isspace(*p) ) {
350 lwpgerror(
"invalid KML representation");
368 if (xnode->children == NULL) lwpgerror(
"invalid KML representation");
370 if (pa->
npoints != 1) lwpgerror(
"invalid KML representation");
383 if (xnode->children == NULL) lwpgerror(
"invalid KML representation");
385 if (pa->
npoints < 2) lwpgerror(
"invalid KML representation");
401 for (xa = xnode->children ; xa != NULL ; xa = xa->next)
405 if (xa->type != XML_ELEMENT_NODE)
continue;
407 if (strcmp((
char *) xa->name,
"outerBoundaryIs"))
continue;
409 for (xb = xa->children ; xb != NULL ; xb = xb->next)
412 if (xb->type != XML_ELEMENT_NODE)
continue;
414 if (strcmp((
char *) xb->name,
"LinearRing"))
continue;
419 if (ppa[0]->npoints < 4)
420 lwpgerror(
"invalid KML representation");
428 lwpgnotice(
"forced closure on an un-closed KML polygon");
434 if (outer_rings != 1)
435 lwpgerror(
"invalid KML representation");
437 for (ring=1, xa = xnode->children ; xa != NULL ; xa = xa->next)
441 if (xa->type != XML_ELEMENT_NODE)
continue;
443 if (strcmp((
char *) xa->name,
"innerBoundaryIs"))
continue;
445 for (xb = xa->children ; xb != NULL ; xb = xb->next)
448 if (xb->type != XML_ELEMENT_NODE)
continue;
450 if (strcmp((
char *) xb->name,
"LinearRing"))
continue;
455 if (ppa[ring]->npoints < 4)
456 lwpgerror(
"invalid KML representation");
464 lwpgnotice(
"forced closure on an un-closed KML polygon");
472 if (ppa == NULL || ppa[0] == NULL) lwpgerror(
"invalid KML representation");
488 for (xa = xnode->children ; xa != NULL ; xa = xa->next)
491 if (xa->type != XML_ELEMENT_NODE)
continue;
494 if ( !strcmp((
char *) xa->name,
"Point")
495 || !strcmp((
char *) xa->name,
"LineString")
496 || !strcmp((
char *) xa->name,
"Polygon")
497 || !strcmp((
char *) xa->name,
"MultiGeometry"))
500 if (xa->children == NULL)
break;
514 xmlNodePtr xa = xnode;
516 while (xa != NULL && (xa->type != XML_ELEMENT_NODE
519 if (xa == NULL) lwpgerror(
"invalid KML representation");
521 if (!strcmp((
char *) xa->name,
"Point"))
524 if (!strcmp((
char *) xa->name,
"LineString"))
527 if (!strcmp((
char *) xa->name,
"Polygon"))
530 if (!strcmp((
char *) xa->name,
"MultiGeometry"))
533 lwpgerror(
"invalid KML representation");
void lwgeom_free(LWGEOM *geom)
int ptarray_is_closed_3d(const POINTARRAY *pa)
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
void * lwrealloc(void *mem, size_t size)
LWGEOM * lwgeom_homogenize(const LWGEOM *geom)
LWPOINT * lwpoint_construct(int32_t srid, GBOX *bbox, POINTARRAY *point)
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_FALSE,...
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
void * lwalloc(size_t size)
int ptarray_is_closed_2d(const POINTARRAY *pa)
#define LW_TRUE
Return types for functions with status returns.
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
LWPOLY * lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
This library is the generic geometry handling section of PostGIS.
static LWGEOM * parse_kml(xmlNodePtr xnode, bool *hasz)
Parse KML.
static LWGEOM * parse_kml_line(xmlNodePtr xnode, bool *hasz)
Parse KML lineString.
static POINTARRAY * parse_kml_coordinates(xmlNodePtr xnode, bool *hasz)
Parse kml:coordinates.
PG_FUNCTION_INFO_V1(geom_from_kml)
Ability to parse KML geometry fragment and to return an LWGEOM or an error message.
static LWGEOM * parse_kml_multi(xmlNodePtr xnode, bool *hasz)
Parse KML MultiGeometry.
static LWGEOM * parse_kml_polygon(xmlNodePtr xnode, bool *hasz)
Parse KML Polygon.
static LWGEOM * parse_kml_point(xmlNodePtr xnode, bool *hasz)
Parse KML point.
Datum geom_from_kml(PG_FUNCTION_ARGS)
static bool is_kml_namespace(xmlNodePtr xnode, bool is_strict)
Return false if current element namespace is not a KML one Return true otherwise.