PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ geom_from_kml()

Datum geom_from_kml ( PG_FUNCTION_ARGS  )

Definition at line 72 of file lwgeom_in_kml.c.

73 {
74  GSERIALIZED *geom;
75  LWGEOM *lwgeom, *hlwgeom;
76  xmlDocPtr xmldoc;
77  text *xml_input;
78  int xml_size;
79  char *xml;
80  bool hasz=true;
81  xmlNodePtr xmlroot=NULL;
82 
83  /* Get the KML stream */
84  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
85  xml_input = PG_GETARG_TEXT_P(0);
86  xml = text_to_cstring(xml_input);
87  xml_size = VARSIZE_ANY_EXHDR(xml_input);
88 
89  /* Begin to Parse XML doc */
90  xmlInitParser();
91  xmldoc = xmlReadMemory(xml, xml_size, NULL, NULL, XML_PARSE_SAX1);
92  if (!xmldoc || (xmlroot = xmlDocGetRootElement(xmldoc)) == NULL)
93  {
94  xmlFreeDoc(xmldoc);
95  xmlCleanupParser();
96  lwpgerror("invalid KML representation");
97  }
98 
99  lwgeom = parse_kml(xmlroot, &hasz);
100 
101  /* Homogenize geometry result if needed */
102  if (lwgeom->type == COLLECTIONTYPE)
103  {
104  hlwgeom = lwgeom_homogenize(lwgeom);
105  lwgeom_release(lwgeom);
106  lwgeom = hlwgeom;
107  }
108 
109  /* KML geometries could be either 2 or 3D
110  *
111  * So we deal with 3D in all structures allocation, and flag hasz
112  * to false if we met once a missing Z dimension
113  * In this case, we force recursive 2D.
114  */
115  if (!hasz)
116  {
117  LWGEOM *tmp = lwgeom_force_2d(lwgeom);
118  lwgeom_free(lwgeom);
119  lwgeom = tmp;
120  }
121 
122  geom = geometry_serialize(lwgeom);
123  lwgeom_free(lwgeom);
124 
125  xmlFreeDoc(xmldoc);
126  xmlCleanupParser();
127 
128  PG_RETURN_POINTER(geom);
129 }
#define COLLECTIONTYPE
Definition: liblwgeom.h:108
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: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: