Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Extract data from KML file containing multiple placemarks

Tags:

python

kml

I have a KML file as follows:

<?xml version="1.0"?><kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<name>MST =T</name>
<description><![CDATA[<p>KML test file</p>
<p>This is a test version.</p>]]></description>
<Style id="spstyle5">
    <IconStyle>
        <color>ff3EE23E</color>
        <Icon><href>http://maps.google.com/mapfiles/kml/paddle/wht-blank.png</href></Icon>
    </IconStyle>
    <LineStyle>
        <color>ff3EE23E</color>
        <width>4</width>
    </LineStyle>
</Style>
<Folder>
    <name>Track5</name>
    <visibility>0</visibility>
            <name>sp5_sp6_ Track</name>
            <description><![CDATA[<i>sp5_sp6_</i> track<br>pantrack id:5]]></description>
            <Placemark>
                <name>sp5_sp6_ MST</name>
                <description><![CDATA[<i> species sp5_sp6_</i> mst<br>long= 648.000 corte 0.250000]]></description>
                <styleUrl>#spstyle5</styleUrl>
                <MultiGeometry>
            <LineString>
                        <tessellate>true</tessellate>
                        <altitudeMode>clampToGround</altitudeMode>
                        <coordinates>
                            8.000,8.000 6.000,7.000
                        </coordinates>
                    </LineString>
            <LineString>
                        <tessellate>true</tessellate>
                        <altitudeMode>clampToGround</altitudeMode>
                        <coordinates>
                            5.000,7.000 6.000,7.000
                        </coordinates>
                    </LineString>
            <LineString>
                        <tessellate>true</tessellate>
                        <altitudeMode>clampToGround</altitudeMode>
                        <coordinates>
                            3.000,8.000 5.000,7.000
                        </coordinates>
                    </LineString>
            <LineString>
                        <tessellate>true</tessellate>
                        <altitudeMode>clampToGround</altitudeMode>
                        <coordinates>
                            2.000,8.000 3.000,8.000
                        </coordinates>
                    </LineString>
                </MultiGeometry>
            </Placemark>
</Folder>
<Style id="spstyle7">
    <IconStyle>
        <color>ffBC77DC</color>
        <Icon><href>http://maps.google.com/mapfiles/kml/paddle/wht-blank.png</href></Icon>
    </IconStyle>
    <LineStyle>
        <color>ffBC77DC</color>
        <width>4</width>
    </LineStyle>
</Style>
<Folder>
    <name>Track7</name>
    <visibility>0</visibility>
            <name>sp1_sp2_sp6_ Track</name>
            <description><![CDATA[<i>sp1_sp2_sp6_</i> track<br>pantrack id:7]]></description>
            <Placemark>
                <name>sp1_sp2_sp6_ MST</name>
                <description><![CDATA[<i> species sp1_sp2_sp6_</i> mst<br>long= 441.000 corte 0.250000]]></description>
                <styleUrl>#spstyle7</styleUrl>
                <MultiGeometry>
            <LineString>
                        <tessellate>true</tessellate>
                        <altitudeMode>clampToGround</altitudeMode>
                        <coordinates>
                            11.000,6.000 12.000,6.000
                        </coordinates>
                    </LineString>
            <LineString>
                        <tessellate>true</tessellate>
                        <altitudeMode>clampToGround</altitudeMode>
                        <coordinates>
                            12.000,6.000 12.000,4.000
                        </coordinates>
                    </LineString>
            <LineString>
                        <tessellate>true</tessellate>
                        <altitudeMode>clampToGround</altitudeMode>
                        <coordinates>
                            12.000,4.000 11.000,3.000
                        </coordinates>
                    </LineString>
                </MultiGeometry>
            </Placemark>
</Folder>
<Style id="spstyle8">
    <IconStyle>
        <color>ff97287A</color>
        <Icon><href>http://maps.google.com/mapfiles/kml/paddle/wht-blank.png</href></Icon>
    </IconStyle>
    <LineStyle>
        <color>ff97287A</color>
        <width>4</width>
    </LineStyle>
</Style>
<Folder>
    <name>Track8</name>
    <visibility>0</visibility>
            <name>sp3_sp6_sp4_sp6_ Track</name>
            <description><![CDATA[<i>sp3_sp6_sp4_sp6_</i> track<br>pantrack id:8]]></description>
            <Placemark>
                <name>sp3_sp6_sp4_sp6_ MST</name>
                <description><![CDATA[<i> species sp3_sp6_sp4_sp6_</i> mst<br>long= 757.000 corte 0.250000]]></description>
                <styleUrl>#spstyle8</styleUrl>
                <MultiGeometry>
            <LineString>
                        <tessellate>true</tessellate>
                        <altitudeMode>clampToGround</altitudeMode>
                        <coordinates>
                            8.000,8.000 8.000,7.000
                        </coordinates>
                    </LineString>
            <LineString>
                        <tessellate>true</tessellate>
                        <altitudeMode>clampToGround</altitudeMode>
                        <coordinates>
                            8.000,7.000 11.000,6.000
                        </coordinates>
                    </LineString>
            <LineString>
                        <tessellate>true</tessellate>
                        <altitudeMode>clampToGround</altitudeMode>
                        <coordinates>
                            11.000,6.000 11.000,5.000
                        </coordinates>
                    </LineString>
            <LineString>
                        <tessellate>true</tessellate>
                        <altitudeMode>clampToGround</altitudeMode>
                        <coordinates>
                            11.000,6.000 12.000,6.000
                        </coordinates>
                    </LineString>
            <LineString>
                        <tessellate>true</tessellate>
                        <altitudeMode>clampToGround</altitudeMode>
                        <coordinates>
                            11.000,5.000 12.000,4.000
                        </coordinates>
                    </LineString>
                </MultiGeometry>
            </Placemark>
</Folder>

</Document>
</kml>

I want to extract separately the name of each placemark and all coordinates in the LineString tags associated with that placemark, resulting in something like:

Placemark A 
x, y
x, y
x, y
...
Placemark B
x, y
x, y
x, y

I have tried all the solutions proposed here, but none of them works for my data. In fact, all solutions there are highly generic and no test data was provided for checking them against.

Update: I did some progress with the code below, which allow to extract all placemarks and linecoordinates (I was just forgetting the damned namespace):

from pykml import parser

filename = "output0.kml"
with open(filename) as f:
    root = parser.parse(f).getroot()
    pms = root.findall('.//{http://earth.google.com/kml/2.1}Placemark')
    lst = doc.findall('.//{http://earth.google.com/kml/2.1}LineString')

for pm in pms:
    print(pm.name)
for ls in lst:
    print(ls.coordinates)

But still can't figure out how to extract the coordinates grouped by placemark!

like image 584
maurobio Avatar asked Dec 13 '22 21:12

maurobio


1 Answers

Simply nest the for loops where inner parsing works off the outer parsing element. Also, you can use the built-in etree module since kml are xml files.

import xml.etree.ElementTree as et

doc = et.parse("source.kml")

nmsp = '{http://earth.google.com/kml/2.1}'

for pm in doc.iterfind('.//{0}Placemark'.format(nmsp)):
    print(pm.find('{0}name'.format(nmsp)).text)

    for ls in pm.iterfind('{0}MultiGeometry/{0}LineString/{0}coordinates'.format(nmsp)):
        print(ls.text.strip().replace('\n',''))

# sp5_sp6_ MST
# 8.000,8.000 6.000,7.000
# 5.000,7.000 6.000,7.000
# 3.000,8.000 5.000,7.000
# 2.000,8.000 3.000,8.000
# sp1_sp2_sp6_ MST
# 11.000,6.000 12.000,6.000
# 12.000,6.000 12.000,4.000
# 12.000,4.000 11.000,3.000
# sp3_sp6_sp4_sp6_ MST
# 8.000,8.000 8.000,7.000
# 8.000,7.000 11.000,6.000
# 11.000,6.000 11.000,5.000
# 11.000,6.000 12.000,6.000
# 11.000,5.000 12.000,4.000
like image 183
Parfait Avatar answered Dec 16 '22 11:12

Parfait