Home > Software engineering >  Unable to read or fetch nested elements/text from the GML file using Python
Unable to read or fetch nested elements/text from the GML file using Python

Time:07-13

I am trying to read the .GML file and want to fetch the co-ordinates from the <gml:posList> tag. I am not able to do it.

Input file:

<?xml version="1.0" encoding="UTF-8"?>
<os:FeatureCollection xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:gss="http://www.isotc211.org/2005/gss" xmlns:os="http://namespaces.os.uk/product/1.0" xmlns:gsr="http://www.isotc211.org/2005/gsr" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:oml="http://namespaces.os.uk/open/oml/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gts="http://www.isotc211.org/2005/gts" gml:id="OSOpenMapLocal" xsi:schemaLocation="http://namespaces.os.uk/open/oml/1.0 https://ordnancesurvey.co.uk/xml/open/oml/1.0/OSOpenMapLocal.xsd">
    <gml:boundedBy>
        <gml:Envelope srsName="urn:ogc:def:crs:EPSG::27700" srsDimension="2">
            <gml:lowerCorner>390000 1130000</gml:lowerCorner>
            <gml:upperCorner>400000 1150000</gml:upperCorner>
        </gml:Envelope>
    </gml:boundedBy>
    <os:metadata xlink:href="http://www.os.uk/xml/products/OML.xml"/>
    <os:featureMember>
        <oml:Building gml:id="idEA5BECB9-B5EC-40A7-87F2-FFE7BACCD4C6">
            <oml:geometry>
                <gml:Surface gml:id="idEA5BECB9-B5EC-40A7-87F2-FFE7BACCD4C6-0" srsName="urn:ogc:def:crs:EPSG::27700" srsDimension="2">
                    <gml:patches>
                        <gml:PolygonPatch>
                            <gml:exterior>
                                <gml:LinearRing>
                                    <gml:posList>397291.02 1138758.65 397282 1138766.94 397278.95 1138763.63 397287.97 1138755.34 397291.02 1138758.65</gml:posList>
                                </gml:LinearRing>
                            </gml:exterior>
                        </gml:PolygonPatch>
                    </gml:patches>
                </gml:Surface>
            </oml:geometry>
            <oml:featureCode>15014</oml:featureCode>
        </oml:Building>
    </os:featureMember>
    <os:featureMember>
        <oml:Building gml:id="idD50A8852-CE5F-4DFC-93E8-2B876D6E3FD4">
            <oml:geometry>
                <gml:Surface gml:id="idD50A8852-CE5F-4DFC-93E8-2B876D6E3FD4-0" srsName="urn:ogc:def:crs:EPSG::27700" srsDimension="2">
                    <gml:patches>
                        <gml:PolygonPatch>
                            <gml:exterior>
                                <gml:LinearRing>
                                    <gml:posList>397408.26 1138885.87 397398.45 1138886.03 397398.33 1138878.94 397408.14 1138878.78 397408.26 1138885.87</gml:posList>
                                </gml:LinearRing>
                            </gml:exterior>
                        </gml:PolygonPatch>
                    </gml:patches>
                </gml:Surface>
            </oml:geometry>
            <oml:featureCode>15014</oml:featureCode>
        </oml:Building>
    </os:featureMember>
    <os:featureMember>
        <oml:Building gml:id="id900D7042-EF0D-4900-93FB-C9049C5A0E98">
            <oml:geometry>
                <gml:Surface gml:id="id900D7042-EF0D-4900-93FB-C9049C5A0E98-0" srsName="urn:ogc:def:crs:EPSG::27700" srsDimension="2">
                    <gml:patches>
                        <gml:PolygonPatch>
                            <gml:exterior>
                                <gml:LinearRing>
                                    <gml:posList>397439.25 1138892.27 397425.68 1138897.42 397421.71 1138886.96 397435.28 1138881.81 397439.25 1138892.27</gml:posList>
                                </gml:LinearRing>
                            </gml:exterior>
                        </gml:PolygonPatch>
                    </gml:patches>
                </gml:Surface>
            </oml:geometry>
            <oml:featureCode>15014</oml:featureCode>
        </oml:Building>
    </os:featureMember>
    <os:featureMember>
        <oml:Building gml:id="id908A62CC-ADE7-4B44-9459-FCE0555989FD">
            <oml:geometry>
                <gml:Surface gml:id="id908A62CC-ADE7-4B44-9459-FCE0555989FD-0" srsName="urn:ogc:def:crs:EPSG::27700" srsDimension="2">
                    <gml:patches>
                        <gml:PolygonPatch>
                            <gml:exterior>
                                <gml:LinearRing>
                                    <gml:posList>397435.85 1139053.06 397423.74 1139045.31 397427.43 1139039.54 397439.54 1139047.29 397435.85 1139053.06</gml:posList>
                                </gml:LinearRing>
                            </gml:exterior>
                        </gml:PolygonPatch>
                    </gml:patches>
                </gml:Surface>
            </oml:geometry>
            <oml:featureCode>15014</oml:featureCode>
        </oml:Building>
    </os:featureMember>
    <os:featureMember>
        <oml:Building gml:id="id5BA3AE25-F67B-414F-B130-3D12B41D0005">
            <oml:geometry>
                <gml:Surface gml:id="id5BA3AE25-F67B-414F-B130-3D12B41D0005-0" srsName="urn:ogc:def:crs:EPSG::27700" srsDimension="2">
                    <gml:patches>
                        <gml:PolygonPatch>
                            <gml:exterior>
                                <gml:LinearRing>
                                    <gml:posList>397437.52 1138900.13 397442.08 1138911.18 397437.39 1138913.12 397432.83 1138902.07 397437.52 1138900.13</gml:posList>
                                </gml:LinearRing>
                            </gml:exterior>
                        </gml:PolygonPatch>
                    </gml:patches>
                </gml:Surface>
            </oml:geometry>
            <oml:featureCode>15014</oml:featureCode>
        </oml:Building>
    </os:featureMember>
    <os:featureMember>
        <oml:Building gml:id="id3FB22189-2930-4098-814A-88FB0A3A1555">
            <oml:geometry>
                <gml:Surface gml:id="id3FB22189-2930-4098-814A-88FB0A3A1555-0" srsName="urn:ogc:def:crs:EPSG::27700" srsDimension="2">
                    <gml:patches>
                        <gml:PolygonPatch>
                            <gml:exterior>
                                <gml:LinearRing>
                                    <gml:posList>396545.45 1136429.42 396545.31 1136422.76 396549.2 1136422.68 396549.34 1136429.34 396545.45 1136429.42</gml:posList>
                                </gml:LinearRing>
                            </gml:exterior>
                        </gml:PolygonPatch>
                    </gml:patches>
                </gml:Surface>
            </oml:geometry>
            <oml:featureCode>15014</oml:featureCode>
        </oml:Building>
    </os:featureMember>
    <os:featureMember>
        <oml:Building gml:id="idE681433D-9934-4CD9-B503-1142384D5246">
            <oml:geometry>
                <gml:Surface gml:id="idE681433D-9934-4CD9-B503-1142384D5246-0" srsName="urn:ogc:def:crs:EPSG::27700" srsDimension="2">
                    <gml:patches>
                        <gml:PolygonPatch>
                            <gml:exterior>
                                <gml:LinearRing>
                                    <gml:posList>396502.52 1136798.79 396491.61 1136800.1 396491.19 1136796.63 396502.11 1136795.32 396502.52 1136798.79</gml:posList>
                                </gml:LinearRing>
                            </gml:exterior>
                        </gml:PolygonPatch>
                    </gml:patches>
                </gml:Surface>
            </oml:geometry>
            <oml:featureCode>15014</oml:featureCode>
        </oml:Building>
    </os:featureMember>
    <os:featureMember>
        <oml:Building gml:id="id81E51D80-0DBA-46BC-B12B-F4550025CBB2">
            <oml:geometry>
                <gml:Surface gml:id="id81E51D80-0DBA-46BC-B12B-F4550025CBB2-0" srsName="urn:ogc:def:crs:EPSG::27700" srsDimension="2">
                    <gml:patches>
                        <gml:PolygonPatch>
                            <gml:exterior>
                                <gml:LinearRing>
                                    <gml:posList>396298.76 1137049.65 396292.52 1137039.78 396298.5 1137036 396304.74 1137045.88 396298.76 1137049.65</gml:posList>
                                </gml:LinearRing>
                            </gml:exterior>
                        </gml:PolygonPatch>
                    </gml:patches>
                </gml:Surface>
            </oml:geometry>
            <oml:featureCode>15014</oml:featureCode>
        </oml:Building>
    </os:featureMember>
    <os:featureMember>
        <oml:Building gml:id="idF509D605-3102-49FC-89B1-36C8E210C83A">
            <oml:geometry>
                <gml:Surface gml:id="idF509D605-3102-49FC-89B1-36C8E210C83A-0" srsName="urn:ogc:def:crs:EPSG::27700" srsDimension="2">
                    <gml:patches>
                        <gml:PolygonPatch>
                            <gml:exterior>
                                <gml:LinearRing>
                                    <gml:posList>396275 1137043 396283.5 1137055.5 396278.69 1137058.77 396270.19 1137046.27 396275 1137043</gml:posList>
                                </gml:LinearRing>
                            </gml:exterior>
                        </gml:PolygonPatch>
                    </gml:patches>
                </gml:Surface>
            </oml:geometry>
            <oml:featureCode>15014</oml:featureCode>
        </oml:Building>
    </os:featureMember>
    <os:featureMember>
        <oml:Building gml:id="id6E040C7E-E534-46B9-877B-39165715B400">
            <oml:geometry>
                <gml:Surface gml:id="id6E040C7E-E534-46B9-877B-39165715B400-0" srsName="urn:ogc:def:crs:EPSG::27700" srsDimension="2">
                    <gml:patches>
                        <gml:PolygonPatch>
                            <gml:exterior>
                                <gml:LinearRing>
                                    <gml:posList>396298.56 1137058.27 396295.06 1137071.27 396288.5 1137069.5 396292 1137056.5 396298.56 1137058.27</gml:posList>
                                </gml:LinearRing>
                            </gml:exterior>
                        </gml:PolygonPatch>
                    </gml:patches>
                </gml:Surface>
            </oml:geometry>
            <oml:featureCode>15014</oml:featureCode>
        </oml:Building>
    </os:featureMember>
</os:FeatureCollection>

Below is my code (Just trying to get the root as first step):

import xml.etree.ElementTree as et

data = et.parse("HT.gml")
root = data.getroot()
print(root)

Output: <Element '{http://namespaces.os.uk/product/1.0}FeatureCollection' at 0x0000018838AE82C0>

Even the output is incorrect. Could anyone please let me know how to extract data from .GML file?

Thanks!

CodePudding user response:

If all you want is the content of the posList tags, you can do something simple like this:

result = []
for el in root.iter():
     if el.tag == '{http://www.opengis.net/gml/3.2}posList':
         results.append(el.text)
print(result)

If you want to filter by particular tags or more complicated paths, the easiest way is to create an XPath query. For the case you asked in the comments (all posLists inside oml:Building), you can do like this:

import xml.etree.ElementTree as et

data = et.parse("HT.gml")
root = data.getroot()

xpath_query = './/{http://namespaces.os.uk/open/oml/1.0}Building//{http://www.opengis.net/gml/3.2}posList'

result = []
for el in root.findall(xpath_query):
    result.append(el.text)
print(result)

Note that you have to give the full namespace of the tag (gml is not part of the name of the tag, it is just an alias for the actual namespace, which is a URI). The URI of each alias is in the root element, in the attributes that look like xmlns:alias.

  • Related