本文介绍了在pykml中读取具有多个地标的kml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 pykml 中,我可以使用以下代码读取文件中的第一个地标:
In pykml, I can read the first placemark in a file using the following code:
with open(filename) as f:
pm = parser.parse(f).getroot().Document.Folder
print "got :"
print pm.Placemark.LineString.coordinates
如何将同一文件中的多个地标读入python中?
How can I read multiple placemarks in the same file into python?
推荐答案
假设所有地标都在一个文件夹中,这是一个更简单的解决方案:
An even easier solution, assuming all placemarks are in one folder:
from pykml import parser
with open(filename) as f:
folder = parser.parse(f).getroot().Document.Folder
for pm in folder.Placemark:
print(pm.name)
您还可以使用基础xml库lxml的功能来搜索地标元素.
You can also use features of the underlying xml library lxml to search for placemark elements.
from pykml import parser
from pykml.factory import nsmap
namespace = {"ns": nsmap[None]}
with open(filename) as f:
root = parser.parse(f).getroot()
pms = root.findall(".//ns:Placemark", namespaces=namespace)
for pm in pms:
print(pm.name)
如果您专门搜索具有Linestring子级的地标,则还可以使用xpath进行更复杂的搜索.
If you specifically search for placemarks that have a Linestring child, you can also use xpath for more sophisticated searches.
pms = root.xpath(".//ns:Placemark[.//ns:LineString]", namespaces=namespace)
这篇关于在pykml中读取具有多个地标的kml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!