PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geom_from_kml()

Datum geom_from_kml ( PG_FUNCTION_ARGS  )

Definition at line 74 of file lwgeom_in_kml.c.

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

75 {
76  GSERIALIZED *geom;
77  LWGEOM *lwgeom, *hlwgeom;
78  xmlDocPtr xmldoc;
79  text *xml_input;
80  int xml_size;
81  char *xml;
82  bool hasz=true;
83  xmlNodePtr xmlroot=NULL;
84 
85 
86  /* Get the KML stream */
87  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
88  xml_input = PG_GETARG_TEXT_P(0);
89  xml = text2cstring(xml_input);
90  xml_size = VARSIZE(xml_input) - VARHDRSZ;
91 
92  /* Begin to Parse XML doc */
93  xmlInitParser();
94  xmldoc = xmlReadMemory(xml, xml_size, NULL, NULL, XML_PARSE_SAX1);
95  if (!xmldoc || (xmlroot = xmlDocGetRootElement(xmldoc)) == NULL)
96  {
97  xmlFreeDoc(xmldoc);
98  xmlCleanupParser();
99  lwpgerror("invalid KML representation");
100  }
101 
102  lwgeom = parse_kml(xmlroot, &hasz);
103 
104  /* Homogenize geometry result if needed */
105  if (lwgeom->type == COLLECTIONTYPE)
106  {
107  hlwgeom = lwgeom_homogenize(lwgeom);
108  lwgeom_release(lwgeom);
109  lwgeom = hlwgeom;
110  }
111 
112  lwgeom_add_bbox(lwgeom);
113 
114  /* KML geometries could be either 2 or 3D
115  *
116  * So we deal with 3D in all structures allocation, and flag hasz
117  * to false if we met once a missing Z dimension
118  * In this case, we force recursive 2D.
119  */
120  if (!hasz)
121  {
122  LWGEOM *tmp = lwgeom_force_2d(lwgeom);
123  lwgeom_free(lwgeom);
124  lwgeom = tmp;
125  }
126 
127  geom = geometry_serialize(lwgeom);
128  lwgeom_free(lwgeom);
129 
130  xmlFreeDoc(xmldoc);
131  xmlCleanupParser();
132 
133  PG_RETURN_POINTER(geom);
134 }
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
static LWGEOM * parse_kml(xmlNodePtr xnode, bool *hasz)
Parse KML.
LWGEOM * lwgeom_homogenize(const LWGEOM *geom)
Definition: lwhomogenize.c:209
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
Definition: lwgeom.c:739
char * text2cstring(const text *textptr)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:648
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:421
uint8_t type
Definition: liblwgeom.h:396
#define COLLECTIONTYPE
Definition: liblwgeom.h:91
Here is the call graph for this function: