I have a KML that looks like this file (only the "Document" portion is repeated another 2,000 times or so with slightly different coordinates in each entry).
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Document>
<Placemark>
<Polygon>
<extrude>1</extrude>
<tesselate>1</tesselate>
<altitudeMode>relativeToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-89.634425,40.73053,16
-89.633951,40.73053,16
-89.633951,40.73013,16
-89.634425,40.73013,16
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
<Style>
<PolyStyle>
<color>#5a14F000</color>
<outline>1</outline>
</PolyStyle>
</Style>
</Placemark>
<Document>
</kml>
The file was exported out of Google Earth. I'm trying to upload into a mapping tool (like CartoDB or Mapbox), however the file is rejected as having errors. I've ran the file through a KML validator like this one: KMLValidator. The changes I've determined to get it to upload are:
1) Replace line 2 with:
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2">
2) "Close the coordinates" This means that the co-ordinates currently listed are essentially a square (4 corners) to satisfy the validator I have to close the polygon by repeating the first set of coordinate. So the target would be:
<coordinates>
-89.634425,40.73053,16
-89.633951,40.73053,16
-89.633951,40.73013,16
-89.634425,40.73013,16
-89.634425,40.73053,16
</coordinates>
However, my problem is that I'm having trouble with updating the co-ordinates in an efficient way. So far this is the best I could come up with (with help from this post:
from pykml import parser from os import path
from lxml import etree
kml_file = path.join( \
'C:\Development', \
'sample.kml')
# Source: https://stackoverflow.com/questions/13712132/extract-coordinates-from-kml-batchgeo-file-with-python
root = parser.fromstring(open(kml_file, 'r').read())
coordinates_before = root.Document.Placemark.Polygon.outerBoundaryIs.LinearRing.coordinates
# Print the coordinates (only prints the first branch )
print 'coordinates before'
print coordinates_before
# Set the coordinates to a new value - Attempting to update to new values
# Get Errors from this
root.Document.Placemark.Polygon.outerBoundaryIs.LinearRing.coordinates
= coordinates_before+"1,1,1"
coordinates_after = root.Document.Placemark.Polygon.outerBoundaryIs.LinearRing.coordinates
print 'coordinates after'
print coordinates_after
# # Create and store a string representation of full KML tree
root_string = etree.tostring(root, pretty_print=True)
# # # Print the string representation using pretty_print
print root_string
As you can see I can manage to add an additional set of values (1,1,1), but
a) I'm not using the values from the first coordinate (rather just dummy values)
b) it's only updating the first branch (how can I scale it to repeat another 2,000 times?
c) also when I've updated the output file shows this text
"coordinates xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str">"
Apologies if this is an overly in depth question, I've just been struggling with this for too long and seems like there should be an easy way that I'm missing. Thanks in advance for any help.