2012-12-04 116 views
5

我已經將一些地址上傳到BatchGeo並下載了我想從中提取座標的最終KML文件。我設法在線here上對混亂的文本文件進行了美化,但我不知道如何解析它以提取座標。使用Python從KML BatchGeo文件中提取座標

<?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> 

似乎有成爲蟒蛇幾個KML庫,但在文檔(例如pyKML)的方法並不多。使用教程,我已經遠遠得到這個並創建了一個「lxml.etree._ElementTree」對象,但我不知道它的屬性:

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' 

所以我如何獲得協調列表?謝謝。

回答

10
from pykml import parser 

root = parser.fromstring(open('BatchGeo.kml', 'r').read()) 
print root.Document.Placemark.Point.coordinates 

看到the pykml docs

希望幫助!