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")
74 if (!xn || xn->type != XML_ELEMENT_NODE)
80 node_name = (
char*)xn->name;
81 colon_pos = strchr(node_name,
':');
83 node_name = colon_pos + 1;
85 return strcmp(node_name, kml_name) == 0;
103 xmlNodePtr xmlroot=NULL;
106 if (PG_ARGISNULL(0)) PG_RETURN_NULL();
107 xml_input = PG_GETARG_TEXT_P(0);
108 xml = text_to_cstring(xml_input);
109 xml_size = VARSIZE_ANY_EXHDR(xml_input);
113 xmldoc = xmlReadMemory(xml, xml_size, NULL, NULL, 0);
115 if (!xmldoc || (xmlroot = xmlDocGetRootElement(xmldoc)) == NULL)
119 lwpgerror(
"invalid KML representation");
145 geom = geometry_serialize(lwgeom);
151 PG_RETURN_POINTER(geom);
163 ns = xmlGetNsList(xnode->doc, xnode);
169 if (ns == NULL)
return !is_strict;
171 for (p=ns ; *p ; p++)
173 if ((*p)->href == NULL || (*p)->prefix == NULL ||
174 xnode->ns == NULL || xnode->ns->prefix == NULL)
continue;
176 if (!xmlStrcmp(xnode->ns->prefix, (*p)->prefix))
178 if (!strcmp((
char *) (*p)->href,
KML_NS))
200 static xmlChar *kmlGetProp(xmlNodePtr xnode, xmlChar *prop)
205 return xmlGetProp(xnode, prop);
207 value = xmlGetNsProp(xnode, prop, (xmlChar *)
KML_NS);
210 if (
value == NULL)
value = xmlGetNoNsProp(xnode, prop);
221 static double parse_kml_double(
char *d,
bool space_before,
bool space_after)
245 if (space_before)
while (isspace(*d)) d++;
246 for (st = INIT, p = d ; *p ; p++)
249 lwpgnotice(
"State: %d, *p=%c", st, *p);
253 if (st == INIT || st == NEED_DIG) st = DIG;
254 else if (st == NEED_DIG_DEC) st = DIG_DEC;
255 else if (st == NEED_DIG_EXP || st == EXP) st = DIG_EXP;
256 else if (st == DIG || st == DIG_DEC || st == DIG_EXP);
257 else lwpgerror(
"invalid KML representation");
261 if (st == DIG) st = NEED_DIG_DEC;
262 else lwpgerror(
"invalid KML representation");
264 else if (*p ==
'-' || *p ==
'+')
266 if (st == INIT) st = NEED_DIG;
267 else if (st == EXP) st = NEED_DIG_EXP;
268 else lwpgerror(
"invalid KML representation");
270 else if (*p ==
'e' || *p ==
'E')
272 if (st == DIG || st == DIG_DEC) st = EXP;
273 else lwpgerror(
"invalid KML representation");
275 else if (isspace(*p))
277 if (!space_after) lwpgerror(
"invalid KML representation");
278 if (st == DIG || st == DIG_DEC || st == DIG_EXP)st = END;
279 else if (st == NEED_DIG_DEC) st = END;
281 else lwpgerror(
"invalid KML representation");
283 else lwpgerror(
"invalid KML representation");
286 if (st != DIG && st != NEED_DIG_DEC && st != DIG_DEC && st != DIG_EXP && st != END)
287 lwpgerror(
"invalid KML representation");
302 int seen_kml_dims = 0;
308 if (xnode == NULL) lwpgerror(
"invalid KML representation");
310 for (found =
false ; xnode != NULL ; xnode = xnode->next)
312 if (xnode->type != XML_ELEMENT_NODE)
continue;
319 if (!found) lwpgerror(
"invalid KML representation");
322 kml_coord = xmlNodeGetContent(xnode);
323 p = (
char *) kml_coord;
333 while (*p && isspace(*p)) ++p;
334 for (kml_dims=0; *p ; p++)
337 if ( isdigit(*p) || *p ==
'+' || *p ==
'-' || *p ==
'.' ) {
339 errno = 0; d = strtod(p, &q);
342 lwpgerror(
"invalid KML representation");
344 if (kml_dims == 1) pt.
x = d;
345 else if (kml_dims == 2) pt.
y = d;
346 else if (kml_dims == 3) pt.
z = d;
348 lwpgerror(
"invalid KML representation");
354 if ( *q && ! isspace(*q) && *q !=
',' ) {
355 lwpgerror(
"invalid KML representation");
359 while (*q && isspace(*q)) ++q;
360 if ( isdigit(*q) || *q ==
'+' || *q ==
'-' || *q ==
'.' || ! *q ) {
361 if ( kml_dims < 2 ) lwpgerror(
"invalid KML representation");
362 else if ( kml_dims < 3 ) *hasz =
false;
363 if ( ! seen_kml_dims ) seen_kml_dims = kml_dims;
364 else if ( seen_kml_dims != kml_dims ) {
365 lwpgerror(
"invalid KML representation: mixed coordinates dimension");
372 }
else if ( *p !=
',' && ! isspace(*p) ) {
373 lwpgerror(
"invalid KML representation");
391 if (xnode->children == NULL) lwpgerror(
"invalid KML representation");
393 if (pa->
npoints != 1) lwpgerror(
"invalid KML representation");
406 if (xnode->children == NULL) lwpgerror(
"invalid KML representation");
408 if (pa->
npoints < 2) lwpgerror(
"invalid KML representation");
424 for (xa = xnode->children ; xa != NULL ; xa = xa->next)
428 if (xa->type != XML_ELEMENT_NODE)
continue;
432 for (xb = xa->children ; xb != NULL ; xb = xb->next)
435 if (xb->type != XML_ELEMENT_NODE)
continue;
442 if (ppa[0]->npoints < 4)
443 lwpgerror(
"invalid KML representation");
451 lwpgnotice(
"forced closure on an un-closed KML polygon");
457 if (outer_rings != 1)
458 lwpgerror(
"invalid KML representation");
460 for (ring=1, xa = xnode->children ; xa != NULL ; xa = xa->next)
464 if (xa->type != XML_ELEMENT_NODE)
continue;
468 for (xb = xa->children ; xb != NULL ; xb = xb->next)
471 if (xb->type != XML_ELEMENT_NODE)
continue;
478 if (ppa[ring]->npoints < 4)
479 lwpgerror(
"invalid KML representation");
487 lwpgnotice(
"forced closure on an un-closed KML polygon");
495 if (ppa == NULL || ppa[0] == NULL) lwpgerror(
"invalid KML representation");
511 for (xa = xnode->children ; xa != NULL ; xa = xa->next)
514 if (xa->type != XML_ELEMENT_NODE)
continue;
523 if (xa->children == NULL)
break;
537 xmlNodePtr xa = xnode;
539 while (xa != NULL && (xa->type != XML_ELEMENT_NODE
542 if (xa == NULL) lwpgerror(
"invalid KML representation");
556 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.
static bool is_kml_element(xmlNodePtr xn, const char *kml_name)