我有一个KML文件,其结构如下:
<?xml version="1.0"?><kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<name>Test KML</name>
<description><![CDATA[<p>This is a test version.</p>]]></description>
<Style id="spstyle7">
<IconStyle>
<color>ff4DF6D8</color>
<Icon><href>http://maps.google.com/mapfiles/kml/paddle/wht-blank.png</href></Icon>
</IconStyle>
<LineStyle>
<color>ff4DF6D8</color>
<width>4</width>
</LineStyle>
</Style>
<Folder>
<name>Track1</name>
<visibility>0</visibility>
<name>Test1</name>
<description><![CDATA[test1]]></description>
<Placemark>
<name>test1</name>
<description><![CDATA[test1]]></description>
<MultiGeometry>
<LineString>
<tessellate>true</tessellate>
<altitudeMode>clampToGround</altitudeMode>
<coordinates>
11.000,4.000 11.000,3.000
</coordinates>
</LineString>
<LineString>
<tessellate>true</tessellate>
<altitudeMode>clampToGround</altitudeMode>
<coordinates>
11.000,4.000 12.000,4.000
</coordinates>
</LineString>
<LineString>
<tessellate>true</tessellate>
<altitudeMode>clampToGround</altitudeMode>
<coordinates>
12.000,5.000 12.000,4.000
</coordinates>
</LineString>
</MultiGeometry>
</Placemark>
</Folder>
<Style id="spstyle7">
<IconStyle>
<color>ff4DF6D8</color>
<Icon><href>http://maps.google.com/mapfiles/kml/paddle/wht-blank.png</href></Icon>
</IconStyle>
<LineStyle>
<color>ff4DF6D8</color>
<width>4</width>
</LineStyle>
</Style>
<Folder>
<name>Track2</name>
<visibility>0</visibility>
<name>Test2</name>
<description><![CDATA[test2]]></description>
<Placemark>
<name>test2</name>
<description><![CDATA[test2]]></description>
<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>
9.000,1.000 10.000,1.000
</coordinates>
</LineString>
</MultiGeometry>
</Placemark>
</Folder>
</Document>
</kml>
我希望将 coordinates 标记内的所有坐标放入列表或列表列表中(每个文件夹一个)。
首先,我编写了以下代码:
import xml.etree.ElementTree as ET
tree = ET.parse("test.kml")
root = tree.getroot()
results = root.findall('Folder')
textnumbers = [r.find('Placemark/LineString/coordinates').text for r in results]
print textnumbers
但它返回一个空列表。如果我尝试仅获取文件夹名称,请使用以下代码:
for folder in root.findall('Folder'):
name = folder.find('name')
print name
我也得到一个空字符串。为什么解析器找不到 Folder 标签?任何提示?
提前感谢您提供的任何帮助。
答案 0 :(得分:0)
您假设<{1}}节点不是kml
节点
执行Document
时,您获取tree.getroot()
节点。在这种情况下,如果您只是将代码更改为包含kml
作为root的子节点,那么它应该可以正常工作。
实际上只有改变的行应该是这样的:
Document
答案 1 :(得分:0)
事实上,我在这里找到了一个很好的解决方案:https://gis.stackexchange.com/questions/89543/get-points-from-a-kml-linestring。
相应地调整我的代码:
import xml.etree.ElementTree as ET
tree = ET.parse("test.kml")
root = tree.getroot()
lineStrings = tree.findall('.//{http://earth.google.com/kml/2.1}LineString')
for attributes in lineStrings:
for subAttribute in attributes:
if subAttribute.tag == '{http://earth.google.com/kml/2.1}coordinates':
print subAttribute.tag, subAttribute.text
我已经能够检索所有坐标数据。
其他可能的解决方案(未经测试)也可在此处找到:https://programmingadvent.blogspot.com.br/2013/06/kmzkml-file-parsing-with-python.html此处:http://gsp.humboldt.edu/olm_2016/courses/GSP_318/04_3_2_Parsing_XML.html
致以最诚挚的问候,