PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ geom_from_kml()

Datum geom_from_kml ( PG_FUNCTION_ARGS  )

Definition at line 96 of file lwgeom_in_kml.c.

97{
98 GSERIALIZED *geom;
99 LWGEOM *lwgeom, *hlwgeom;
100 xmlDocPtr xmldoc;
101 text *xml_input;
102 int xml_size;
103 char *xml;
104 bool hasz=true;
105 xmlNodePtr xmlroot=NULL;
106
107 /* Get the KML stream */
108 if (PG_ARGISNULL(0)) PG_RETURN_NULL();
109 xml_input = PG_GETARG_TEXT_P(0);
110 xml = text_to_cstring(xml_input);
111 xml_size = VARSIZE_ANY_EXHDR(xml_input);
112
113 /* Begin to Parse XML doc */
114 xmlInitParser();
115 xmldoc = xmlReadMemory(xml, xml_size, NULL, NULL, 0);
116 // xmldoc = xmlReadMemory(xml, xml_size, NULL, NULL, XML_PARSE_SAX1);
117 if (!xmldoc || (xmlroot = xmlDocGetRootElement(xmldoc)) == NULL)
118 {
119 xmlFreeDoc(xmldoc);
120 xmlCleanupParser();
121 lwpgerror("invalid KML representation");
122 }
123
124 lwgeom = parse_kml(xmlroot, &hasz);
125
126 /* Homogenize geometry result if needed */
127 if (lwgeom->type == COLLECTIONTYPE)
128 {
129 hlwgeom = lwgeom_homogenize(lwgeom);
130 lwgeom_release(lwgeom);
131 lwgeom = hlwgeom;
132 }
133
134 lwgeom_add_bbox(lwgeom);
135
136 /* KML geometries could be either 2 or 3D
137 *
138 * So we deal with 3D in all structures allocation, and flag hasz
139 * to false if we met once a missing Z dimension
140 * In this case, we force recursive 2D.
141 */
142 if (!hasz)
143 {
144 LWGEOM *tmp = lwgeom_force_2d(lwgeom);
145 lwgeom_free(lwgeom);
146 lwgeom = tmp;
147 }
148
149 geom = geometry_serialize(lwgeom);
150 lwgeom_free(lwgeom);
151
152 xmlFreeDoc(xmldoc);
153 xmlCleanupParser();
154
155 PG_RETURN_POINTER(geom);
156}
#define COLLECTIONTYPE
Definition liblwgeom.h:122
void lwgeom_free(LWGEOM *geom)
Definition lwgeom.c:1138
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
Definition lwgeom.c:775
LWGEOM * lwgeom_homogenize(const LWGEOM *geom)
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition lwgeom.c:450
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition lwgeom.c:677
static LWGEOM * parse_kml(xmlNodePtr xnode, bool *hasz)
Parse KML.
char * text_to_cstring(const text *textptr)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint8_t type
Definition liblwgeom.h:448

References COLLECTIONTYPE, geometry_serialize(), lwgeom_add_bbox(), lwgeom_force_2d(), lwgeom_free(), lwgeom_homogenize(), lwgeom_release(), parse_kml(), text_to_cstring(), and LWGEOM::type.

Here is the call graph for this function: