0

I have a KML file with the following content. Only the first part of the KML file is shown, because the problem is here. I am parsing the file with PYKML. In node GroundOverlay, there is a child gx:LatLonQuad, which again has a child "coordinates". How can I get the content of "coordinates"?

<?xml version="1.0" standalone="no" ?>
<kml
    xmlns="http://www.google.com/kml/ext/2.2"
    xmlns:gx="http://www.google.com/kml/ext/2.2"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:cs="http://www.orienteering.org/schemas/course-setting" createTime="2022-08-02T16:50:32" creator="Condes version 10.3.0" >
    <Folder>
        <name>ViKaSki Træningsløb 10-08-2022</name>
        <Folder>
            <name>Canvas 1 (1:5.000)</name>
            <GroundOverlay id="Canvas 1 Map" >
                <name>Canvas 1 Map</name>
                <Icon>
                    <href>files/Canvas 1 Map.jpg</href>
                </Icon>
                <gx:LatLonQuad>
                    <coordinates>
                        9.2703219809,56.2281778343 9.2930588405,56.2281260725 9.2931273404,56.2370979805 9.2703851665,56.2371497598
                    </coordinates>
                </gx:LatLonQuad>
                <LatLonBox>
                    <north>56.2371244317</north>
                    <south>56.2281524434</south>
                    <east>9.2930931848</east>
                    <west>9.2703518756</west>
                    <rotation>-0.2337437892</rotation>
                </LatLonBox>
            </GroundOverlay>
        </Folder>
    </Folder>
</kml>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

1 Answers1

0

First setup the data then parse KML and extract the LatLonQuad element.

from pykml import parser

data = '''<?xml version="1.0" standalone="no" ?>
<kml
    xmlns="http://www.google.com/kml/ext/2.2"
    xmlns:gx="http://www.google.com/kml/ext/2.2"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:cs="http://www.orienteering.org/schemas/course-setting" createTime="2022-08-02T16:50:32" creator="Condes version 10.3.0" >
    <Folder>
        <name>ViKaSki Træningsløb 10-08-2022</name>
        <Folder>
            <name>Canvas 1 (1:5.000)</name>
            <GroundOverlay id="Canvas 1 Map" >
                <name>Canvas 1 Map</name>
                <Icon>
                    <href>files/Canvas 1 Map.jpg</href>
                </Icon>
                <gx:LatLonQuad>
                    <coordinates>
                        9.2703219809,56.2281778343 9.2930588405,56.2281260725 9.2931273404,56.2370979805 9.2703851665,56.2371497598
                    </coordinates>
                </gx:LatLonQuad>
                <LatLonBox>
                    <north>56.2371244317</north>
                    <south>56.2281524434</south>
                    <east>9.2930931848</east>
                    <west>9.2703518756</west>
                    <rotation>-0.2337437892</rotation>
                </LatLonBox>
            </GroundOverlay>
        </Folder>
    </Folder>
</kml>'''

Next, parse the KML data and find the LatLonQuad element. Note the GX extension namespace is needed to find the element because it is an extension.

root = parser.fromstring(data)
go = root.Folder.Folder.GroundOverlay
gxns = {"gx": "http://www.google.com/kml/ext/2.2"}
if latLonQuad := go.find("gx:LatLonQuad", namespaces=gxns):    
    print(latLonQuad.coordinates)

Output:

  9.2703219809,56.2281778343 9.2930588405,56.2281260725 9.2931273404,56.2370979805 9.2703851665,56.2371497598
CodeMonkey
  • 22,825
  • 4
  • 35
  • 75