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")
70 const char *colon_pos;
71 const char *node_name;
74 if (!xn || xn->type != XML_ELEMENT_NODE)
80 node_name = (
const 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);
114 if (!xmldoc || (xmlroot = xmlDocGetRootElement(xmldoc)) == NULL)
118 lwpgerror(
"invalid KML representation");
144 geom = geometry_serialize(lwgeom);
150 PG_RETURN_POINTER(geom);
162 ns = xmlGetNsList(xnode->doc, xnode);
168 if (ns == NULL)
return !is_strict;
170 for (p=ns ; *p ; p++)
172 if ((*p)->href == NULL || (*p)->prefix == NULL ||
173 xnode->ns == NULL || xnode->ns->prefix == NULL)
continue;
175 if (!xmlStrcmp(xnode->ns->prefix, (*p)->prefix))
177 if (!strcmp((
char *) (*p)->href,
KML_NS))
199 static xmlChar *kmlGetProp(xmlNodePtr xnode, xmlChar *prop)
204 return xmlGetProp(xnode, prop);
206 value = xmlGetNsProp(xnode, prop, (xmlChar *)
KML_NS);
209 if (
value == NULL)
value = xmlGetNoNsProp(xnode, prop);
220 static double parse_kml_double(
char *d,
bool space_before,
bool space_after)
244 if (space_before)
while (isspace(*d)) d++;
245 for (st = INIT, p = d ; *p ; p++)
248 lwpgnotice(
"State: %d, *p=%c", st, *p);
252 if (st == INIT || st == NEED_DIG) st = DIG;
253 else if (st == NEED_DIG_DEC) st = DIG_DEC;
254 else if (st == NEED_DIG_EXP || st == EXP) st = DIG_EXP;
255 else if (st == DIG || st == DIG_DEC || st == DIG_EXP);
256 else lwpgerror(
"invalid KML representation");
260 if (st == DIG) st = NEED_DIG_DEC;
261 else lwpgerror(
"invalid KML representation");
263 else if (*p ==
'-' || *p ==
'+')
265 if (st == INIT) st = NEED_DIG;
266 else if (st == EXP) st = NEED_DIG_EXP;
267 else lwpgerror(
"invalid KML representation");
269 else if (*p ==
'e' || *p ==
'E')
271 if (st == DIG || st == DIG_DEC) st = EXP;
272 else lwpgerror(
"invalid KML representation");
274 else if (isspace(*p))
276 if (!space_after) lwpgerror(
"invalid KML representation");
277 if (st == DIG || st == DIG_DEC || st == DIG_EXP)st = END;
278 else if (st == NEED_DIG_DEC) st = END;
280 else lwpgerror(
"invalid KML representation");
282 else lwpgerror(
"invalid KML representation");
285 if (st != DIG && st != NEED_DIG_DEC && st != DIG_DEC && st != DIG_EXP && st != END)
286 lwpgerror(
"invalid KML representation");
301 int seen_kml_dims = 0;
307 if (xnode == NULL) lwpgerror(
"invalid KML representation");
309 for (found =
false ; xnode != NULL ; xnode = xnode->next)
311 if (xnode->type != XML_ELEMENT_NODE)
continue;
318 if (!found) lwpgerror(
"invalid KML representation");
321 kml_coord = xmlNodeGetContent(xnode);
322 p = (
char *) kml_coord;
332 while (*p && isspace(*p)) ++p;
333 for (kml_dims=0; *p ; p++)
336 if ( isdigit(*p) || *p ==
'+' || *p ==
'-' || *p ==
'.' ) {
338 errno = 0; d = strtod(p, &q);
341 lwpgerror(
"invalid KML representation");
343 if (kml_dims == 1) pt.
x = d;
344 else if (kml_dims == 2) pt.
y = d;
345 else if (kml_dims == 3) pt.
z = d;
347 lwpgerror(
"invalid KML representation");
353 if ( *q && ! isspace(*q) && *q !=
',' ) {
354 lwpgerror(
"invalid KML representation");
358 while (*q && isspace(*q)) ++q;
359 if ( isdigit(*q) || *q ==
'+' || *q ==
'-' || *q ==
'.' || ! *q ) {
360 if ( kml_dims < 2 ) lwpgerror(
"invalid KML representation");
361 else if ( kml_dims < 3 ) *hasz =
false;
362 if ( ! seen_kml_dims ) seen_kml_dims = kml_dims;
363 else if ( seen_kml_dims != kml_dims ) {
364 lwpgerror(
"invalid KML representation: mixed coordinates dimension");
371 }
else if ( *p !=
',' && ! isspace(*p) ) {
372 lwpgerror(
"invalid KML representation");
390 if (xnode->children == NULL) lwpgerror(
"invalid KML representation");
392 if (pa->
npoints != 1) lwpgerror(
"invalid KML representation");
405 if (xnode->children == NULL) lwpgerror(
"invalid KML representation");
407 if (pa->
npoints < 2) lwpgerror(
"invalid KML representation");
423 for (xa = xnode->children ; xa != NULL ; xa = xa->next)
427 if (xa->type != XML_ELEMENT_NODE)
continue;
431 for (xb = xa->children ; xb != NULL ; xb = xb->next)
434 if (xb->type != XML_ELEMENT_NODE)
continue;
441 if (ppa[0]->npoints < 4)
442 lwpgerror(
"invalid KML representation");
450 lwpgnotice(
"forced closure on an un-closed KML polygon");
456 if (outer_rings != 1)
457 lwpgerror(
"invalid KML representation");
459 for (ring=1, xa = xnode->children ; xa != NULL ; xa = xa->next)
463 if (xa->type != XML_ELEMENT_NODE)
continue;
467 for (xb = xa->children ; xb != NULL ; xb = xb->next)
470 if (xb->type != XML_ELEMENT_NODE)
continue;
477 if (ppa[ring]->npoints < 4)
478 lwpgerror(
"invalid KML representation");
486 lwpgnotice(
"forced closure on an un-closed KML polygon");
494 if (ppa == NULL || ppa[0] == NULL) lwpgerror(
"invalid KML representation");
510 for (xa = xnode->children ; xa != NULL ; xa = xa->next)
513 if (xa->type != XML_ELEMENT_NODE)
continue;
522 if (xa->children == NULL)
break;
536 xmlNodePtr xa = xnode;
538 while (xa != NULL && (xa->type != XML_ELEMENT_NODE
541 if (xa == NULL) lwpgerror(
"invalid KML representation");
555 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)