PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ geom_from_kml()

Datum geom_from_kml ( PG_FUNCTION_ARGS  )

Definition at line 75 of file lwgeom_in_kml.c.

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

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