Search code examples
pythonpython-3.xkmlgoogle-earth

How to get all coordinates from kml file?


I saved a couple of coordinates from google earth as a kml file. The following code provides me with the coordinates of the first location. Any idea how to get the remaining ones?-I would somehow have to iterate over all locations.

from pykml import parser
 with open('list.kml', 'r') as f:
 root = parser.parse(f).getroot()
print(root.Document.Placemark.Point.coordinates)

Solution

  • You can use

    for i in root.findall('.//{http://www.opengis.net/kml/2.2}Point'):
        print(i.coordinates)
    

    Or, providing a more exact path to Document/Placemark/Point:

    for i in root.findall('{http://www.opengis.net/kml/2.2}Document/{http://www.opengis.net/kml/2.2}Placemark/{http://www.opengis.net/kml/2.2}Point'):
        print(i.coordinates)