Extract Coordinates from KML BatchGeo File with Python

时光怂恿深爱的人放手 提交于 2020-01-28 01:58:06

问题


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>

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

This gives the error:

AttributeError: 'lxml.etree._ElementTree' object has no attribute 'Element'

So how do I get a list of co-ordinates? Thanks.


回答1:


from pykml import parser

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

see the pykml docs

hope that helps!



来源:https://stackoverflow.com/questions/13712132/extract-coordinates-from-kml-batchgeo-file-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!