gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:22:26 25 4
gpt4 key购买 nike

我有一个 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)

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

最佳答案

只需嵌套 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

关于Python 从包含多个地标的 KML 文件中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46035813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com