From: Sandro Santilli Date: Tue, 21 Feb 2012 13:54:55 +0000 (+0000) Subject: Draft an lwgeom_from_gml function (#1591) X-Git-Tag: 2.0.0alpha6~19 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=63721b4a5a8160cb155e860cbe9557cec910152d;p=postgis Draft an lwgeom_from_gml function (#1591) Can't be moved all to liblwgeom due to use of spatial_ref_sys git-svn-id: http://svn.osgeo.org/postgis/trunk@9246 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/postgis/lwgeom_in_gml.c b/postgis/lwgeom_in_gml.c index 0310a3ec6..0d8ce77e1 100644 --- a/postgis/lwgeom_in_gml.c +++ b/postgis/lwgeom_in_gml.c @@ -49,6 +49,7 @@ Datum geom_from_gml(PG_FUNCTION_ARGS); +static LWGEOM* lwgeom_from_gml(const char *wkt); static LWGEOM* parse_gml(xmlNodePtr xnode, bool *hasz, int *root_srid); typedef struct struct_gmlSrs @@ -83,58 +84,24 @@ PG_FUNCTION_INFO_V1(geom_from_gml); Datum geom_from_gml(PG_FUNCTION_ARGS) { GSERIALIZED *geom; - xmlDocPtr xmldoc; text *xml_input; LWGEOM *lwgeom; - int xml_size; char *xml; int root_srid=SRID_UNKNOWN; - bool hasz=true; - xmlNodePtr xmlroot=NULL; /* Get the GML stream */ if (PG_ARGISNULL(0)) PG_RETURN_NULL(); xml_input = PG_GETARG_TEXT_P(0); xml = text2cstring(xml_input); - xml_size = VARSIZE(xml_input) - VARHDRSZ; /* Zero for undefined */ root_srid = PG_GETARG_INT32(1); - /* Begin to Parse XML doc */ - xmlInitParser(); - xmldoc = xmlParseMemory(xml, xml_size); - if (!xmldoc || (xmlroot = xmlDocGetRootElement(xmldoc)) == NULL) - { - xmlFreeDoc(xmldoc); - xmlCleanupParser(); - gml_lwerror("invalid GML representation", 1); - } - - - lwgeom = parse_gml(xmlroot, &hasz, &root_srid); - lwgeom_add_bbox(lwgeom); - if ( root_srid != SRID_UNKNOWN ) + lwgeom = lwgeom_from_gml(xml); + if ( root_srid != SRID_UNKNOWN ) lwgeom->srid = root_srid; - xmlFreeDoc(xmldoc); - xmlCleanupParser(); - - /* GML geometries could be either 2 or 3D and can be nested mixed. - * Missing Z dimension is even tolerated inside some GML coords - * - * So we deal with 3D in all structures allocation, and flag hasz - * to false if we met once a missing Z dimension - * In this case, we force recursive 2D. - */ - if (!hasz) - { - LWGEOM *tmp = lwgeom_force_2d(lwgeom); - lwgeom_free(lwgeom); - lwgeom = tmp; - } - geom = geometry_serialize(lwgeom); lwgeom_free(lwgeom); @@ -1782,6 +1749,59 @@ static LWGEOM* parse_gml_coll(xmlNodePtr xnode, bool *hasz, int *root_srid) return geom; } +/** + * Read GML + */ +static LWGEOM* lwgeom_from_gml(const char* xml) +{ + xmlDocPtr xmldoc; + xmlNodePtr xmlroot=NULL; + int xml_size = strlen(xml); + LWGEOM *lwgeom; + bool hasz=true; + int root_srid=SRID_UNKNOWN; + + /* Begin to Parse XML doc */ + xmlInitParser(); + xmldoc = xmlParseMemory(xml, xml_size); + if (!xmldoc || (xmlroot = xmlDocGetRootElement(xmldoc)) == NULL) + { + xmlFreeDoc(xmldoc); + xmlCleanupParser(); + gml_lwerror("invalid GML representation", 1); + } + + + lwgeom = parse_gml(xmlroot, &hasz, &root_srid); + + xmlFreeDoc(xmldoc); + xmlCleanupParser(); + /* shouldn't we be releasing xmldoc too here ? */ + + + if ( root_srid != SRID_UNKNOWN ) + lwgeom->srid = root_srid; + + /* Should we really do this here ? */ + lwgeom_add_bbox(lwgeom); + + /* GML geometries could be either 2 or 3D and can be nested mixed. + * Missing Z dimension is even tolerated inside some GML coords + * + * So we deal with 3D in all structures allocation, and flag hasz + * to false if we met once a missing Z dimension + * In this case, we force recursive 2D. + */ + if (!hasz) + { + LWGEOM *tmp = lwgeom_force_2d(lwgeom); + lwgeom_free(lwgeom); + lwgeom = tmp; + } + + return lwgeom; +} + /** * Parse GML