similar to this: Extract Coordinates from KML BatchGeo File with Python
But I want to know how to inspect the data object, as well as how to iterate it, and parse all the Placemark
to get the coordinates
.
The below is the how the KML looks like, and there are multiple <Placemark>
tags.
Sample KML data:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd http://www.google.com/kml/ext/2.2 http://code.google.com/apis/kml/schema/kml22gx.xsd">
<Document id="...">
<name>...</name>
<Snippet></Snippet>
<Folder id="...">
<name>...</name>
<Snippet></Snippet>
<Placemark id="...">
<name>....</name>
<Snippet></Snippet>
<description>...</description>
<styleUrl>....</styleUrl>
<Point>
<altitudeMode>...</altitudeMode>
<coordinates> 103.xxx,1.xxx,0</coordinates>
</Point>
</Placemark>
<Placemark id="...">
...
</Placemark>
</Folder>
<Style id="...">
<IconStyle>
<Icon><href>...</href></Icon>
<scale>0.250000</scale>
</IconStyle>
<LabelStyle>
<color>00000000</color>
<scale>0.000000</scale>
</LabelStyle>
<PolyStyle>
<color>ff000000</color>
<outline>0</outline>
</PolyStyle>
</Style>
</Document>
</kml>
This is what I have, extract.py:
from pykml import parser
from os import path
kml_file = path.join('list.kml')
with open(kml_file) as f:
doc = parser.parse(f).getroot()
print doc.Document.Folder.Placemark.Point.coordinates
This prints the first coordinates
.
General python question:
How can I inspect doc
, find out about its type, and print out the values it contains?
Task question:
How do I iterate through all the Placemark
and get its coordinates
?
Have tried the following but nothing is printed.
for e in doc.Document.Folder.iter('Placemark'):
print e
I've found the answers.
To parse Placemark
, this is the code
for e in doc.Document.Folder.Placemark:
coor = e.Point.coordinates.text.split(',')
To find the object type, use type(object)
.
Not sure why findall()
and iter()
didn't work though:
doc.Document.Folder.findall('Placemark')
for e in doc.Document.Folder.iter('Placemark'):
Both returned empty.
Update: Was missing the namespace for findall
to work.
doc.findall('.//{http://www.opengis.net/kml/2.2}Placemark')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With