Draw Simple Shapes in Google Earth using Python
Read how you can draw simple shapes such as squares and circles in Google earth using KML & Python.
Google Earth is a magnificent tool useful for both GIS amateurs as well as for professionals.
In this post we'll cover how we can generate basic shapes such as squares and circles for Google Earth using python & KML.
KML:
KML (Keyhole Markup Language) is Google Earths XML based file format for describing geographic data. A KML document can contain different kinds of "features" such as:
- Placemarks
- Paths
- Polygons
Read the rest of the post here.
Draw Simple Shapes in Google Earth Part 3 (Draw a Circle)
Again, let us transform our KML square example into a Cheetah template. If you are unfamiliar with Cheetah take a look here!
<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Document> <Style id="examplePolyStyle"> <PolyStyle> <color>ff0000cc</color> </PolyStyle> </Style> <Placemark> <name>hollow box</name> <styleUrl>#examplePolyStyle</styleUrl> <Polygon> <extrude>1</extrude> <altitudeMode>clampToGround</altitudeMode> <outerBoundaryIs> <LinearRing> <coordinates> #for $vertex in $vertices $vertex[1],$vertex[0],0 #end for </coordinates> </LinearRing> </outerBoundaryIs> </Polygon> </Placemark> </Document> </kml>
Again, notice how we replaced the hard coded vertices (coordinates) from the original KML file and replaced them with a Cheetah array and a for loop highlighted in blue text above.
And now the Code:
To actually use this template to generate a KML document lets define a simple Python function.
import geopy from Cheetah.Template import Template def createCircle(aLatLonCenter, aDiameter): lLatLonCenter = geopy.util.parse_geo(aLatLonCenter) lVertices = [] lAngle = 0 lRadius = aDiameter / 2.0 while lAngle <= 360: lVertex = geopy.distance.destination(lLatLonCenter, lAngle, lRadius) lVertices.append(lVertex) lAngle = lAngle + 10 lFirstVertex = lVertices[0] lVertices.append(lFirstVertex) lTemplate = Template(file='kmlshapes.tmpl', searchList=[{'vertices': lVertices}]) return lTemplate
To test this code you can use a simple statement like this:
print createCircle('40 41m 23.50s N 74 2m 43.19s W', 1.0)
and you will get back a KML string defining a red circle around Liberty island in NY.
Note: Geopy is used for parsing the Latitude / Longitude string as well as for calculating the corners of the square. Note also that KML polygon expects the last coordinate to be the same as the first, this is the reason for using 5 coordinates.
Back to the start of this tutorial!
Draw Simple Shapes in Google Earth Part 2
First let us transform our KML square example into a Cheetah template. If you are unfamiliar with Cheetah take a look here!
<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Document> <Style id="examplePolyStyle"> <PolyStyle> <color>ff0000cc</color> </PolyStyle> </Style> <Placemark> <name>hollow box</name> <styleUrl>#examplePolyStyle</styleUrl> <Polygon> <extrude>1</extrude> <altitudeMode>clampToGround</altitudeMode> <outerBoundaryIs> <LinearRing> <coordinates> #for $vertex in $vertices $vertex[1],$vertex[0],0 #end for </coordinates> </LinearRing> </outerBoundaryIs> </Polygon> </Placemark> </Document> </kml>
Notice how we replaced the hard coded vertices (coordinates) from the original KML file and replaced them with a Cheetah array and a for loop highlighted in blue text above.
And now the Code:
To actually use this template to generate a KML document lets define a simple Python function.
import geopy from Cheetah.Template import Template def createSquare(aLatLonCenter, aSize): #parse the latlon string lLatLonCenter = geopy.util.parse_geo(aLatLonCenter) lNECorner = geopy.distance.destination(lLatLonCenter, 45.0, aSize) lSECorner = geopy.distance.destination(lLatLonCenter, 135.0, aSize) lSWCorner = geopy.distance.destination(lLatLonCenter, 225.0, aSize) lNWCorner = geopy.distance.destination(lLatLonCenter, 315.0, aSize) lVertices = [lNECorner, lSECorner, lSWCorner, lNWCorner, lNECorner] lTemplate = Template(file='kmlshapes.tmpl', searchList=[{'vertices': lVertices}]) return lTemplate
To test this code you can use a simple statement like this:
print createSquare('40 41m 23.50s N 74 2m 43.19s W', 0.250)
and you will get back a KML string defining a red square around Liberty island in NY.
Note: Geopy is used for parsing the Latitude / Longitude string as well as for calculating the corners of the square. Note also that KML polygon expects the last coordinate to be the same as the first, this is the reason for using 5 coordinates.
Next lets draw a circle using the same technique:
Draw Simple Shapes in Google Earth
Google Earth is a magnificent tool useful for both GIS amateurs as well as for professionals.
In this post we'll cover how we can generate basic shapes such as squares and circles for Google Earth using python & KML.
KML:
KML (Keyhole Markup Language) is Google Earths XML based file format for describing geographic data. A KML document can contain different kinds of "features" such as:
- Placemarks
- Paths
- Polygons
Drawing a square in KML:
To draw a square shape in Google Earth using KML you can for example use KMLs polygon feature like this:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="examplePolyStyle">
<PolyStyle>
<color>ff0000cc</color>
</PolyStyle>
</Style>
<Placemark>
<name>hollow box</name>
<styleUrl>#examplePolyStyle</styleUrl>
<Polygon>
<extrude>1</extrude>
<altitudeMode>clampToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-74.0432344372,40.6914504354,0
-74.0432345372,40.6882717489,0
-74.0474265739,40.6882717489,0
-74.0474266739,40.6914504354,0
-74.0432344372,40.6914504354,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
NB: In KML a point is specified as (lon, lat, alt), note lon comes before lat.
Next, do it with python (part 2):