How to access specific latitude and longitude off kml tag?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

How to access specific latitude and longitude off kml <coordinate> tag?



I am trying to organize the longitudes and latitudes of a .kml file into two separate arrays- one for the longitudes and one for the latitudes. I can access the coordinates in the .kml tag in my .kml file, as shown here:


>>> print root.Document.Placemark.Polygon.outerBoundaryIs.LinearRing.coordinates



-66.88943028804978,18.36088172088213,0 -66.86666454770418,18.34807414584603,0 -66.87505121318034,18.32676800819395,0 -66.85010093893582,18.35136710854946,0 -66.86795531478315,18.36533631930146,0 -66.88943028804978,18.36088172088213,0


-66.88943028804978,18.36088172088213,0 -66.86666454770418,18.34807414584603,0 -66.87505121318034,18.32676800819395,0 -66.85010093893582,18.35136710854946,0 -66.86795531478315,18.36533631930146,0


-66.88943028804978,18.36088172088213,0



Now I want to put them in an array, or at least access the x and y values of each coordinate separatly. I've tried typing:


print root.Document.Placemark.Polygon.outerBoundaryIs.LinearRing.coordinates[1]



But I get back this error:


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "lxml.objectify.pyx", line 297, in lxml.objectify.ObjectifiedElement.__getitem__ (src/lxml/lxml.objectify.c:4234)
IndexError: 1



How do I get around this? How do I access the x and y values individually?
Cheers!! O.Fried





I got it guys! I made it a string then split it. If anyone wants me to explain more just say so!
– O. Fried
2 days ago




1 Answer
1



Assuming your KML is formatted like this, here is a complete example to parse a KML file and extract its coordinates.


from pykml import parser
import re
kml_str = '<kml xmlns="http://www.opengis.net/kml/2.2">'
'<Document>'
'<name>tennis-poly</name>'
'<Placemark>'
'<name>red</name>'
'<Polygon>'
'<outerBoundaryIs>'
'<LinearRing>'
'<coordinates>'
'-122.43193945401,37.801983684521 '
'-122.431564131101,37.8020327731402 '
'-122.431499536494,37.801715236748 '
'-122.43187136387,37.8016634915437 '
'-122.43193945401,37.801983684521'
'</coordinates>'
'</LinearRing>'
'</outerBoundaryIs>'
'</Polygon>'
'</Placemark>'
'</Document>'
'</kml>'

kml = parser.fromstring(kml_str)
coords = kml.Document.Placemark.Polygon.outerBoundaryIs.LinearRing.coordinates.text
for s in re.split("[ nr]+", coords.strip()):
coord = s.split(",", 3)
# note altitude is the third optional component in a tuple
print "lon=",coord[0], "tlat=",coord[1]



This would output:


lon= -122.43193945401 lat= 37.801983684521
lon= -122.431564131101 lat= 37.8020327731402
lon= -122.431499536494 lat= 37.801715236748
lon= -122.43187136387 lat= 37.8016634915437
lon= -122.43193945401 lat= 37.801983684521






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

PySpark count values by condition

Mass disable jenkins jobs