KML (Keyhole Markup Language) was developed by Keyhole, Inc for adding geographic annotation, models, and simple animation to their Keyhole Earth Viewer. Keyhole was taken over by Google in 2004, and the Earth Viewer is now known as Google Earth. Google have also adopted it for use on their Google Maps platform, and it is quickly becoming the de facto standard for geographic annotation.
KML is based on XML (eXtensible Markup Language) and is relative compact. This makes it easy to use and to learn. With the widespread use of XML, reading and writing most KML entities is not difficult.
Here is a simple example taken from the maps at EcoMapCostaRica.com. This project used OpenLayers to display a number of KML layers. Here is a subset of the “plants” layer displayed in Google Earth:
Google Earth coverage of rural Costa Rica is limited, but this image shows the basic concept. Here is the subset of the “plants” layer that created the above image:
<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://earth.google.com/kml/2.0"> <Document> <Style id="plant_mono"> <LineStyle> <color>ff00ffff</color> <width>1</width> </LineStyle> <PolyStyle> <color>400000ff</color> <colorMode>normal</colorMode> <fill>1</fill> <outline>1</outline> </PolyStyle> </Style> <Style id="plant_mixed"> <LineStyle> <color>ff00ffff</color> <width>1</width> </LineStyle> <PolyStyle> <color>4000ffff</color> <colorMode>normal</colorMode> <fill>1</fill> <outline>1</outline> </PolyStyle> </Style> <Placemark> <name>plant_group 13 </name> <description><![CDATA[ Attributes: teak4 mixture mowed and path through 30.0 0.0 0.8 tegr 1.0 3.7 ]]></description> <styleUrl>#plant_mono</styleUrl> <Polygon><extrude>1</extrude><tessellate>1</tessellate><altitudeMode>clampToGround</altitudeMode> <outerBoundaryIs><LinearRing><coordinates> -84.665533612,10.530410987 -84.665550763,10.530443767 -84.665603146,10.530452067 -84.665640665,10.530480124 -84.665789193,10.530499770 -84.665827720,10.530535105 -84.665969448,10.530613760 -84.666052138,10.530696910 -84.666207507,10.530777026 -84.666194577,10.530559123 -84.666196820,10.530494167 -84.666159203,10.530481401 -84.666039968,10.530365172 -84.666047506,10.530324014 -84.665923514,10.530151891 -84.665885596,10.530131013 -84.665888968,10.530339959 -84.665875487,10.530393512 -84.665779902,10.530361366 -84.665653041,10.530358925 -84.665596419,10.530358204 -84.665533612,10.530410987 </coordinates></LinearRing></outerBoundaryIs></Polygon> </Placemark> <Placemark> <name> plant_group 14 </name> <description><![CDATA[ Attributes: mixed1 mixture 30.0 0.0 0.4 diva 1.5 2.0 ]]></description> <styleUrl>#plant_mixed</styleUrl> <Polygon><extrude>1</extrude><tessellate>1</tessellate><altitudeMode>clampToGround</altitudeMode> <outerBoundaryIs><LinearRing><coordinates> -84.664548244,10.530933509 -84.664564284,10.530993512 -84.664736319,10.531218727 -84.664927109,10.531382244 -84.665083986,10.531267227 -84.665066305,10.531067386 -84.664966344,10.531074149 -84.664936851,10.531010803 -84.664980932,10.530999069 -84.665006455,10.530952101 -84.664959611,10.530856002 -84.664923128,10.530819423 -84.664965186,10.530674110 -84.665036311,10.530669921 -84.665040756,10.530557965 -84.665007284,10.530463465 -84.665059498,10.530406425 -84.665152735,10.530112157 -84.665029178,10.529956127 -84.664768798,10.529968889 -84.664698726,10.530470951 -84.664584035,10.530510744 -84.664515875,10.530554159 -84.664499029,10.530737246 -84.664548244,10.530933509 </coordinates></LinearRing></outerBoundaryIs></Polygon> </Placemark> </Document> </kml>
A KML file consists of a Document entity which contains the required shapes. These shapes are referred to as Placemarks. A placemark has a name, description, and style. Althought they are often thought of as pushpins (points), they can also be lines, polgyons (as above), and 3d models using the COLLADA format. Placemark geometries can also be defined as with a ‘MultiGeometry’ entity, to define the placemark with multiple shapes. For example, a farm could be defined with a property boundary (polygon) and a house (point).
The above example defines two named styles to represent two different kinds of plantation (plant_mixed, plant_mono). This is the usual method of applying styles, but it is also possible to define a style locally within a placemark definition. It is also possible to define styles in an external KML file by giving the full URL in the styleUrl tag.
Placemarks can also be defined in 3d by giving them an altitude. This can be defined relative to sea level or terrain height, and it can be ‘extruded’ to give it thickness.
Placemark descriptions can include standard XHTML, but these should be enclosed with CDATA tags. Key/value data can be included in extension tags. For example, a map of cinemas might include extension tags for the owner, number of screens, and number of seats.
Viewpoints can also be defined in 3D. They can be specified for particular features (LookAt element) or as general viewpoints (Camera element).
KML files can also import images, both as ground overlays (eg. an aerial photograph) or as a photograph (eg. a ‘digital billboard’). As well as being flat, photographs can be defined as cylinders. Cylindrical photographs can provide simple but effective backdrops for 3D models.
Time stamps or time spans can be applied to features, allowing for simple animations. For example, a hiker’s progress could be marked with multiple time-stamped placemarks.
Areas of the map can be defined as regions using the Region element. These can be set to appear for specific scales. For example, complex 3D models or areas of dense placemarks could be set to only appear at high zoom levels. When zoomed out, these could be replaced with simpler representations. This reduces clutter and speeds rendering times.
It should be apparant that KML is a very powerful language, especially for 3D geographic browsers such as Google Earth. However it does have some limitations. Although it is becoming a standard for geographic annotation, most of these viewers are 2D viewers. These simply cannot render 3D models or provide a good representation of altitude. Other aspects of the standard (eg. animation) are often not supported. Finally, KML is an annotation language. It is great for drawing shapes, bitmaps, and models on existing maps. It does not offer the model richness required to define a complete map. Instead, a standard such as GML or ESRI Shape should be chosen instead.