PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ geom_from_kml()

Datum geom_from_kml ( PG_FUNCTION_ARGS  )

Definition at line 94 of file lwgeom_in_kml.c.

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

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

Here is the call graph for this function: