This page gives an overview of the exercise on creating kml presented on November 3th, 2014 by Kees den Heijer.

In the exercise, python is the primary language and the simplekml package is used for creating kml files.

Simplekml documentation is available at http://simplekml.readthedocs.org/en/latest/

The sample code on this page gives the highlights from the simplekml documentation.

For other languages:

For general kml documentation, see https://developers.google.com/kml/

A gallery with various OpenEarth kml visualisations can be found at KML Screenshots

Exercises are at the bottom of this page.

Getting started

The following lines of code import the simplekml package and create a kml object.

import simplekml
# initialise the kml object
kml = simplekml.Kml()

After adding content to the kml object, it can be saved as a kml file by:

# save as kml
kml.save("test.kml")

or in zipped format, kmz:

# save as kmz
kml.savekmz("test.kmz")

Point

There are basically two ways to deal with new points.

The first one is to create the object first and after that set one or more attributes.

# create point object
pnt = kml.newpoint()
# define the name and coordinates in the name resp. coords attribute of the object
pnt.name = "A Point"
pnt.coords = [(1.0, 2.0)]

The second one is to provide various attributes upon initialisation of the object.

# create point object with name and coords
pnt = kml.newpoint(name="A Point", coords=[(1.0, 2.0)])

Also a mix of the two ways is possible. In this workshop we provide the name upon initialisation and all other attributes separately afterwards.

import simplekml
 
# create kml object with name
kml = simplekml.Kml(name="My first kml")
 
# create point object with name
pnt = kml.newpoint(name="A Point")
# define coordinates
pnt.coords = [(1.0, 2.0)]
 
# save to file
kml.save("point.kml")

A tutorial for creating a kml with multiple points can be found at http://simplekml.readthedocs.org/en/latest/tut_point.html

Linestring

A clear linestring tutorial is available in the documentation of simplekml:

http://simplekml.readthedocs.org/en/latest/tut_linestring.html

Polygon

A polygon can be created as follows:

# create kml polygon object with name 
pol = kml.newpolygon(name='A Polygon')
# define outerboundaries of the polygon
pol.outerboundaryis = [(18.333868,-34.038274), (18.370618,-34.034421),
                       (18.350616,-34.051677),(18.333868,-34.038274)]
# define innerboundaries of the polygon (optional)
pol.innerboundaryis = [(18.347171,-34.040177), (18.355741,-34.039730),
                       (18.350467,-34.048388),(18.347171,-34.040177)]


Overlay

There are overlays in three different types:

  • GroundOverlay: draws an image overlay draped onto the terrain.
  • ScreenOverlay: draws an image overlay fixed to the screen.
  • PhotoOverlay: Geographically locate a photograph in Google Earth.

Groundoverlay


ground = kml.newgroundoverlay(name='GroundOverlay')
ground.icon.href = 'http://simplekml.googlecode.com/hg/samples/resources/smile.png'
ground.gxlatlonquad.coords = [(18.410524,-33.903972),(18.411429,-33.904171),
                              (18.411757,-33.902944),(18.410850,-33.902767)]
ground.latlonbox.north = -33.902828
ground.latlonbox.south = -33.904104
ground.latlonbox.east =  18.410684
ground.latlonbox.west =  18.411633
ground.latlonbox.rotation = -14

ScreenOverlay


screen = kml.newscreenoverlay(name='ScreenOverlay')
screen.icon.href = 'http://simplekml.googlecode.com/hg/samples/resources/simplekml-logo.png'
screen.overlayxy = simplekml.OverlayXY(x=0,y=1,xunits=simplekml.Units.fraction,
                                       yunits=simplekml.Units.fraction)
screen.screenxy = simplekml.ScreenXY(x=15,y=15,xunits=simplekml.Units.pixel,
                                     yunits=simplekml.Units.insetpixels)
screen.size.x = -1
screen.size.y = -1
screen.size.xunits = simplekml.Units.fraction
screen.size.yunits = simplekml.Units.fraction

PhotoOverlay

photo = kml.newphotooverlay(name='PhotoOverlay Test')
photo.camera = simplekml.Camera(longitude=18.410858, latitude=-33.904446, altitude=50,
                                altitudemode=simplekml.AltitudeMode.clamptoground)
photo.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/camera.png'
photo.icon.href = 'http://simplekml.googlecode.com/hg/samples/resources/stadium.jpg'
photo.viewvolume = simplekml.ViewVolume(-25,25,-15,15,1)

Styling

point

The style of a point can be customized by adjusting the style attribute of the point object. The following code assumes an existing point object (pnt) and makes it a red, twice as big circle.

pnt.style.labelstyle.color = simplekml.Color.red  # Make the text red
pnt.style.labelstyle.scale = 2  # Make the text twice as big
pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png'

polygon

The style of a polygon can be customized by adjusting the style attribute of the polygon object. The following code assumes an existing polygon object (pol) and makes its boundaries green with linewidth 5 and its fill color green as well.

pol.style.linestyle.color = simplekml.Color.green
pol.style.linestyle.width = 5
pol.style.polystyle.color = simplekml.Color.changealphaint(100, simplekml.Color.green)


Exercises

  1. Visualise Dutch coastal grain size data in Google Earth.

    1. The data is available in netCDF at http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/grainsize_vtv/grainsize_vtv.nc.html (data also available as csv in grainsizepoint.csv)
    2. Use the variables lat, lon, meanD50
    3. (Advanced) Also use sigmaD50 and find a proper way to display the variance
    Sample code to read netcdf
    from netCDF4 import Dataset
    url = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/grainsize_vtv/grainsize_vtv.nc'
    ds = Dataset(url)
    lat = ds.variables['lat'][:]
    lon = ds.variables['lon'][:]
    D50 = ds.variables['meanD50'][:]
    std = ds.variables['sigmaD50'][:]
    ds.close()

    Suggestion for the icon: http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png

  2. Visualise a line based on the coordinates provided in the attached file track.csv
    1. Start with displaying the track, without elevation and time information
    2. Add the elevation information to it
    3. Additionally, add the time information
  3. Add a colorbar to the kml from exercise 1, as a screenoverlay.

The  following two ipython notebooks include most of the items from the exercises:

https://svn.oss.deltares.nl/repos/openearthtools/trunk/python/notebooks/points.ipynb

https://svn.oss.deltares.nl/repos/openearthtools/trunk/python/notebooks/lines.ipynb


  • No labels