PostGIS  3.7.0dev-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  if (!xmldoc || (xmlroot = xmlDocGetRootElement(xmldoc)) == NULL)
115  {
116  xmlFreeDoc(xmldoc);
117  xmlCleanupParser();
118  lwpgerror("invalid KML representation");
119  }
120 
121  lwgeom = parse_kml(xmlroot, &hasz);
122 
123  /* Homogenize geometry result if needed */
124  if (lwgeom->type == COLLECTIONTYPE)
125  {
126  hlwgeom = lwgeom_homogenize(lwgeom);
127  lwgeom_release(lwgeom);
128  lwgeom = hlwgeom;
129  }
130 
131  /* KML geometries could be either 2 or 3D
132  *
133  * So we deal with 3D in all structures allocation, and flag hasz
134  * to false if we met once a missing Z dimension
135  * In this case, we force recursive 2D.
136  */
137  if (!hasz)
138  {
139  LWGEOM *tmp = lwgeom_force_2d(lwgeom);
140  lwgeom_free(lwgeom);
141  lwgeom = tmp;
142  }
143 
144  geom = geometry_serialize(lwgeom);
145  lwgeom_free(lwgeom);
146 
147  xmlFreeDoc(xmldoc);
148  xmlCleanupParser();
149 
150  PG_RETURN_POINTER(geom);
151 }
#define COLLECTIONTYPE
Definition: liblwgeom.h:108
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1218
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:462

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

Here is the call graph for this function: