如何从 Python 中的 KML 文件获取坐标列表
How to get a list of coordinates from a KML file in Python
我有一个具有以下结构的 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
但它 returns 是一个空列表。如果我尝试只获取 Folder 名称,代码如下:
for folder in root.findall('Folder'):
name = folder.find('name')
print name
我也得到一个空字符串。为什么解析器找不到 Folder 标签?有什么提示吗?
在此先感谢您提供的任何帮助。
您的根是 kml
节点而不是您假设的 Document
节点
当您执行 tree.getroot()
时,您会获取 kml
节点。在这种情况下,如果您只是更改代码以将 Document
作为根的子节点包含在内,它应该可以工作。
实际上只有一行要更改:
result = root.findall('Document/Folder')
其实我在这里找到了一个很好的解决方案: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 and here: http://gsp.humboldt.edu/olm_2016/courses/GSP_318/04_3_2_Parsing_XML.html
此致,
我有一个具有以下结构的 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
但它 returns 是一个空列表。如果我尝试只获取 Folder 名称,代码如下:
for folder in root.findall('Folder'):
name = folder.find('name')
print name
我也得到一个空字符串。为什么解析器找不到 Folder 标签?有什么提示吗?
在此先感谢您提供的任何帮助。
您的根是 kml
节点而不是您假设的 Document
节点
当您执行 tree.getroot()
时,您会获取 kml
节点。在这种情况下,如果您只是更改代码以将 Document
作为根的子节点包含在内,它应该可以工作。
实际上只有一行要更改:
result = root.findall('Document/Folder')
其实我在这里找到了一个很好的解决方案: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 and here: http://gsp.humboldt.edu/olm_2016/courses/GSP_318/04_3_2_Parsing_XML.html
此致,