So what is
KML
and how does it work?
Google (Google Inc. 2008) describe
KML
as ...
"an XML grammar and file format for modeling and storing geographic features such as points, lines, images, and polygons for display in Google Earth"
... and use the spatial browser metaphor quite nicely when stating that ...
"A KML file is processed by Google Earth in a similar way that HTML and XML files are processed by web browsers.
Like HTML, KML has a tag-based structure with names and attributes used for specific display purposes.
Thus, Google Earth acts as a browser of KML files." (Google Inc. 2008)
KML
can be used to :
We will see how to do some of these things and how they enable us to produce cartography and support visualization in this
short introduction to
KML
.
We will do so by looking at some simple
KML
examples. Each example contains three sections ...
In each case you should ...
As it is an
XML
language
KML
consists of data (text, numbers and other characters) marked up, or surrounded by, elements that describe the text.
Elements are distinguished from text that they surround as they are enclosed by < and > symbols. Within these symbols are the element name and any attributes associated with the element. Attributes have values contained in "double quote marks". Elements begin with <elementName> and end with </elementName>. Any text between these opening and closing elements can be considered as being 'marked-up' with or 'tagged' by the element and its attributes.
Elements can be arranged hierarchically. An element that is within another element is said to be a 'child' of the enclosing 'parent' element. Child elements inherit the properties of their parents.
Elements that are used to specify attributes but that do not mark up textual content and do not contain children can be specified with a single tag, closing with /> - for example : <elementName attribute="value"/>.
As is the case with all
XML
languages,
KML
files must be both :
The Placemark element is the basis of
KML
visualization and cartography.
A
KML
file can contain any number of Placemark elements.
These contain children that allow us to specify coordinates and symbolise using points, lines and areas :
Point contains coordinatesLineString contains coordinatesPolygon contains outerBoundaryIs contains LinearRing contains coordinatesHere is a simple
KML
file that contains elements and attributes. It is both
well-formed
and
valid
and contains a single Placemark.
Do try to get a feel for the basic structure of
KML
with the opening and closing elements and associated attributes and the hierarchical relationships between elements.
<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.1"><Document><name>example kml document</name><Placemark><name>a simple placemark</name><description>a description</description><Point><coordinates>-105.020,39.744</coordinates></Point></Placemark></Document></kml>The example file consists of an opening line that indicates that this is an
XML
file. Then we have a kml element that points to the document in which the
KML
language is defined. This root element encloses all other
KML
elements. Within this is a Document element - these are used to structure the
KML
. In our Document is a single Placemark, which is the element that describes a symbol or geographic feature. This element contains three others :
name - encloses text that names the Placemarkdescription - encloses text that provides a fuller textual description of the PlacemarkPoint - encloses a further child element :
coordinates - contains comma separated values to define the longitude, latitude and (optionally) elevation of the PlacemarkThis hierarchical arrangement of elements with attributes is typical of
KML
.
All elements within the Placemark are associated with the Placemark.
Here is another simple
KML
file, that contains some additional elements, that is both
well-formed
and
valid
.
<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.1"><Document><Placemark><name>Mile High Stadium</name><description>(INVESCO Field) home of the Denver Broncos</description><Point><coordinates>-105.020,39.744</coordinates></Point></Placemark><Placemark><name>Colorado</name><description>state outline - polygon</description><Polygon><outerBoundaryIs><LinearRing><coordinates>-109.051,40.998 -108.378,40.998 -107.003,41.004-106.318,40.998 -104.051,41.004 -102.052,41.004-102.046,40.002 -102.041,36.995 -102.999,37.000-104.330,36.995 -105.875,36.995 -106.872,36.995-106.877,37.000 -107.967,36.995 -109.046,37.000-109.040,38.161 -109.062,38.276 -109.051,38.999-109.051,40.998</coordinates></LinearRing></outerBoundaryIs></Polygon></Placemark></Document></kml>Elements to consider in this example include :
Polygon - encloses various elements defining the polygon geometry associated with the Placemark, including :
outerBoundaryIs - defines the outer polygon boundary (and thus allows for 'donut' polygons) and contains :
LinearRing - an ordered sequence of coordinates defined in :
coordinates - the comma separated values that define the longitude, latitude and (optionally) elevation of the Polygon associated with this Placemark.
To see how we can create and edit
KML
, try the following ...
You can make additions and changes to the file, save your edits and then reload the
KML
file in
Google Earth
.
So, why not edit the code to see whether you can ...
Point symbols that identify interesting features :
name and description features to add labels
coordinates to change the outline described in the PolygonYou can see coordinates at the bottom of the screen as you move the cursor around in
Google Earth
. The coordinates used in these exercises are given in decimal degrees. Click and to select 'Degrees' or 'Decimal' rather than 'Degrees, Minutes, Seconds' to change the way in which coordinates are displayed
in
Google Earth
.
Remember to make your edits, save the changes and then reload the
KML
file in
Google Earth
to view the results.
NOTE : this is a very rudimentary introduction and you are encouraged to experiment with the various tutorials and copious documentation and impressive examples that are available online :
Many more elements and attributes and relationships between them are described in this documentation.