Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

lxml - Extract Coordinates from KML BatchGeo File with Python

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...