问题描述
我已经将一些地址上传到BatchGeo,并下载了要从中提取坐标的生成的KML文件.我设法在此处在线美化了混乱的文本文件,但是我不知道如何解析它以提取共纵坐标.
I've uploaded some addresses to BatchGeo and downloaded the resulting KML file from which I want to extract the coordinates. I managed to prettify the jumbled text file online here, but I don't know how to parse it to extract the co-ordinates.
<?xml version="1.0" ?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Document>
<Placemark>
<name>...</name>
<description>....</description>
<Point>
<coordinates>-3.1034345755337,57.144817425039,0</coordinates>
</Point><address>...</address>
<styleUrl>#0</styleUrl>
</Placemark>
</Document>
</kml>
似乎有多个适用于python的kml库,但文档编制方式却不多(例如 pyKML ).使用本教程,到目前为止,我已经创建了一个'lxml.etree._ElementTree'对象,但是不确定其属性:
There seem to be several kml libraries for python but not much in the way of documentation (e.g. pyKML). Using the tutorial, I have got this far and created an 'lxml.etree._ElementTree' object but I'm not sure of its attributes:
from pykml import parser
kml_file = "BatchGeo.kml"
with open(kml_file) as f:
doc = parser.parse(f)
coordinate = doc.Element("coordinates")
print coordinate
这给出了错误:
AttributeError: 'lxml.etree._ElementTree' object has no attribute 'Element'
那么我如何获得坐标列表?谢谢.
So how do I get a list of co-ordinates? Thanks.
推荐答案
from pykml import parser
root = parser.fromstring(open('BatchGeo.kml', 'r').read())
print root.Document.Placemark.Point.coordinates
请参见 pykml文档
希望有帮助!
这篇关于使用Python从KML BatchGeo文件中提取坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!