Python从包含多个地标的KML文件中提取数据

时间:2017-09-04 11:13:05

标签: python kml

我有一个KML文件如下:

<?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>

我想分别提取每个地标的名称以及与该地标相关联的LineString标记中的所有坐标,结果如下:

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

我已经尝试了所有提出的解决方案here,但它们都不适用于我的数据。实际上,所有解决方案都是高度通用的,没有提供测试数据来检查它们。

更新:我在下面的代码中取得了一些进展,它允许提取所有地标和线路坐标(我只是忘记该死的命名空间):

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)

但仍然无法弄清楚如何提取按地标分组的坐标

1 个答案:

答案 0 :(得分:2)

简单地嵌套for循环,其中内部解析工作在外部解析元素之外。此外,您可以使用内置的etree模块,因为kml是xml文件。

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