PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum geom_from_kml ( PG_FUNCTION_ARGS  )

Definition at line 62 of file lwgeom_in_kml.c.

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

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

Here is the call graph for this function: