Read kml file with multiple placemarks in pykml

Catherine Holloway picture Catherine Holloway · Oct 24, 2015 · Viewed 8.5k times · Source

In pykml, I can read the first placemark in a file using the following code:

 with open(filename) as f:
     pm = parser.parse(f).getroot().Document.Folder
     print "got :"
     print pm.Placemark.LineString.coordinates

How can I read multiple placemarks in the same file into python?

Answer

Catherine Holloway picture Catherine Holloway · Oct 24, 2015

This works:

with open(filename) as f:
    doc = parser.parse(f).getroot().Document.Folder
for pm in doc.iterchildren():
    if hasattr(pm, 'LineString'):
        print pm.LineString.coordinates