PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ geom_from_kml()

Datum geom_from_kml ( PG_FUNCTION_ARGS  )

Definition at line 61 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.

62 {
63  GSERIALIZED *geom;
64  LWGEOM *lwgeom, *hlwgeom;
65  xmlDocPtr xmldoc;
66  text *xml_input;
67  int xml_size;
68  char *xml;
69  bool hasz=true;
70  xmlNodePtr xmlroot=NULL;
71 
72 
73  /* Get the KML stream */
74  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
75  xml_input = PG_GETARG_TEXT_P(0);
76  xml = text2cstring(xml_input);
77  xml_size = VARSIZE(xml_input) - VARHDRSZ;
78 
79  /* Begin to Parse XML doc */
80  xmlInitParser();
81  xmldoc = xmlReadMemory(xml, xml_size, NULL, NULL, XML_PARSE_SAX1);
82  if (!xmldoc || (xmlroot = xmlDocGetRootElement(xmldoc)) == NULL)
83  {
84  xmlFreeDoc(xmldoc);
85  xmlCleanupParser();
86  lwpgerror("invalid KML representation");
87  }
88 
89  lwgeom = parse_kml(xmlroot, &hasz);
90 
91  /* Homogenize geometry result if needed */
92  if (lwgeom->type == COLLECTIONTYPE)
93  {
94  hlwgeom = lwgeom_homogenize(lwgeom);
95  lwgeom_release(lwgeom);
96  lwgeom = hlwgeom;
97  }
98 
99  lwgeom_add_bbox(lwgeom);
100 
101  /* KML geometries could be either 2 or 3D
102  *
103  * So we deal with 3D in all structures allocation, and flag hasz
104  * to false if we met once a missing Z dimension
105  * In this case, we force recursive 2D.
106  */
107  if (!hasz)
108  {
109  LWGEOM *tmp = lwgeom_force_2d(lwgeom);
110  lwgeom_free(lwgeom);
111  lwgeom = tmp;
112  }
113 
114  geom = geometry_serialize(lwgeom);
115  lwgeom_free(lwgeom);
116 
117  xmlFreeDoc(xmldoc);
118  xmlCleanupParser();
119 
120  PG_RETURN_POINTER(geom);
121 }
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
static LWGEOM * parse_kml(xmlNodePtr xnode, bool *hasz)
Parse KML.
LWGEOM * lwgeom_homogenize(const LWGEOM *geom)
Definition: lwhomogenize.c:195
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
Definition: lwgeom.c:690
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:599
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:372
uint8_t type
Definition: liblwgeom.h:380
#define COLLECTIONTYPE
Definition: liblwgeom.h:76
Here is the call graph for this function: