KML: map packaging for the Web

Setup (one-time hack for GIS Lab machines)

If you don't have a Start menu entry for Google Earth, you can add one by:

  1. In Windows Explorer, browse to C:\Program Files (x86)\Google\Google Earth\client
  2. Right-click on googleearth.exe and select Pin to start menu

Web

KML is to geodata as HTML is to text

simple KML example

Copy:

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Placemark>
    <name>Frew's office (Bren Hall 4524)</name>
    <description>http://frew.eri.ucsb.edu</description>
    <Point>
      <coordinates>-119.84183,34.413415,0</coordinates>
    </Point>
  </Placemark>
</kml>

into Notepad++ and save as frew.kml.

Double-click frew.kml in Windows Explorer. (Windows knows that files with names ending in .kml should be opened by Google Earth.)

Google Earth launches and you see something like this:

Frew's office on Google Earth

picking the example apart:

<?xml version="1.0" encoding="utf-8"?>

This document is marked up with XML, as defined by version 1.0 of the XML standard, and possibly including non-Roman-alphabet characters (e.g., всемирная паутина).

All XML documents require this as the first line of text.

<kml xmlns="http://www.opengis.net/kml/2.2">

The top-level XML element ("there can be only 1..."). Everything inside the kml element is marked up with KML, as defined by version 2.2 of the KML standard.

  <Placemark>

The Placemark element defines a single placemark, one of the fundamental KML geographic feature types. Just a like a feature in a shapefile, a placemark has both geometry and attributes.

    <name>Frew's office (Bren Hall 4524)</name>

name is a required text attribute for a placemark. Google Earth will display the value of name next to whatever icon it uses for the placemark; other KML clients may do other things with it.

Note: By convention, KML uses capitalized tag names (e.g. Placemark) for elements that can contain other elements. (Yes, the kml element is an exception to this...)

    <description>http://frew.eri.ucsb.edu</description>

description is an optional text attribute for a placemark. Google Earth will display the value of description inside a pop-up text balloon when you select (click on) the associated placemark. Google Earth recognizes URLs in a description, and makes them live (clickable).

    <Point>

Point defines the placemark's geometry. KML supports all the usual vector geometry types: points, lines, polygons, etc.

      <coordinates>-119.84183,34.413415,0</coordinates>

A Point element must always contain a coordinates element, which specifies the placemark's location in WGS 84 geographic (lat-lon) coordinates.

Note: WGS 84 is the only coordinate system supported by KML.

    </Point>
  </Placemark>
</kml>

KMZ: packaging KML with support files

Click on this link: Islay.kmz. Save it somewhere and then double-click on the saved file. This is a simple example of a KML document with multiple features and image annotations.

The .kmz suffix indicates that the file is a compressed KML document. If you want to see what's inside it (as opposed to just displaying it), you have to uncompress it first. From Windows Explorer, right-click Islay.kmz and select 7-Zip→Extract to "Islay\". This will create the following folder structure:

Islay\
    doc.kml
    images\
        Ardbeg.jpg
        Bruichladdich.jpg
        ...

That's right, a .kmz file is really a Zip archive in disguise.

A .kmz file always contains a top-level file named doc.kml which is the actual KML document. The KMZ format is thus often used to compress a really huge KML document so it will download faster.

A .kmz file may also contain any number of additional files and folders, which doc.kml can reference. The .kmz file can thus be a stand-alone packaging containing all the data needed to render the map.

In this example, the folder images contains image files that are referenced by placemark <description> elements in doc.kml. (Look at doc.kml in Notepad++ to see how this is done.)

How fancy can KML get?

(Don't ask...)

Seriously, while KML is full-flegded vector data format (it can do everything a shapefile can, except for coordinate systems other than WGS84), it's really a map document format:

We could teach a whole class on this (we teach some in the advanced GIS course). Meanwhile, if you want to go hog-wild with KML bells-n-whistles, check out:

Creating KML with ArcGIS

To display a KML document in ArcGIS, use the KML To Layer tool.

To export an ArcGIS map layer, use the Layer to KML tool.

To export an entire ArcGIS map's active layers, use the Map To KML tool.

The ArcGIS KML export tools do correctly save the layer(s)'s style information in the generated KML.

Displaying KML in a web browser

You can load a local .kml or .kmz file into Google Earth, QGIS, ArcGIS, etc.; but you can't load a local file into a web browser that's running a mapping application like Google Maps. This is for security reasons (otherwise, any Web program could upload random files from your computer to evil.com or wherever.)

In order to load a KML document into a browser-based mapping client, the KML document must be accessible via a URL. In plain English, this means you have to park it on a web server somewhere.

Once upon a time (like, a couple of years ago) you could display KML in Google Maps just by dropping the URL into the Google Maps search box. That doesn't work anymore—the only way to do this is to create a web page that has some code in it that loads the KML into a Google Map. Here's are a couple of examples:

Note that Google Maps isn't as flexible as Google Earth (for example, note that the cute whisky glass icons aren't displayed.) However, Google Maps only needs a web browser, whereas Google Earth requires a whole separate client. Your choice...