KML

What is a KML file?

The Google Earth and Maps format for places, lines and areas.

What KML is

KML is a plain-text format you can open in any editor. It is used for mapping.

The extension is .kml, and the full name is Keyhole Markup Language. Both matter less than what the file can hold, which is what the rest of this page is about.

Where KML came from

It comes from Keyhole. The specification is OGC KML 2.3.

Age is worth knowing here for one practical reason: the older a format is, the more programs have had time to learn it.

The specification is public

It is published in full, so anyone can implement it from the document rather than by inspection, which is why it turns up in so many programs and why files written twenty years ago still open. A published specification is not the same thing as a royalty-free one: where a format wraps a codec, the patent licensing is a separate question the standard does not settle.

Nothing is thrown away

KML stores its content exactly. Saving it again changes nothing, so it can be opened, edited and re-saved as often as you like without accumulating damage — which is what makes it a working format rather than a delivery one.

What opens KML

Google Earth, QGIS and ArcGIS read it, and so do most programs of the same kind.

If a file will not open, the format is rarely the problem — it is more often that the program predates it. Converting to something older is the reliable way past that, and it is what the rest of this site is for.

Opening it in a browser

No browser reads it.

That is the single most common reason to convert it: not that the format is bad, but that the place you want to show the file cannot read it.

It is a working format

KML is meant to be opened and changed. Keep the file in this format for as long as the work is going on, and export from it whenever a finished copy is needed.

A map document, not a list of coordinates

KML describes what should appear on a map and how it should look. A placemark with an icon and a description that opens in a bubble when clicked. A line with a colour and a width. A polygon with a fill, extruded upwards to a height. A folder structure grouping them, with each layer switchable on and off.

That makes it a different kind of thing from GPX, which is a list of measured points and no opinion about presentation. A KML can hold a track too, but its purpose is showing something to somebody — which is why it is what Google Earth and Google My Maps produce, and why the files people share tend to be KML.

KMZ is a zipped KML, and usually the better file

A KMZ is a ZIP archive containing a KML plus everything it references — custom icons, photographs, overlay images. Rename it to .zip and any archive tool opens it.

It is the more practical form for anything with images, because a bare KML pointing at pictures on your own computer shows nothing on anybody else’s. The XML also compresses heavily, so a large dataset is often a fifth of the size. Google Earth writes KMZ by default for exactly these reasons, and unzipping to get the plain KML is a one-step operation when a tool insists on it.

Points, lines and areas

Three geometry types cover nearly everything. A point is a placemark — a shop, a viewpoint, a survey marker. A line string is a path, a boundary or a route. A polygon is an area: a property, a district, a search zone, a planning application.

Polygons are where KML does something GPX cannot. An area has an inside, it can carry holes, it can be filled and made partly transparent, and it can be extruded into a three-dimensional block. Anything involving territory rather than travel wants this format, and translating such a file into a track format loses the concept entirely.

Styling, and why the file has so much of it

Colours, icons, line widths, label sizes and transparency are all in the document, and colours are written in an order that catches everybody out: alpha, blue, green, red, which is the reverse of the usual notation. A shade that looks wrong is nearly always this.

Styles can be defined once and referenced by many features, which is what keeps a file with two thousand placemarks manageable. It also means a bulk restyling is a change in one place — worth knowing before anybody edits two thousand elements by hand.

Ground overlays and network links

A ground overlay pins an image to the map between four coordinates: a scanned historical map, a site plan, an aerial photograph, a heat map from somewhere else. It is the feature that turns Google Earth from a viewer into somewhere you can compare things, and no activity format has an equivalent.

A network link goes further and points the file at a URL, refreshing on a timer. That is how live data gets into Google Earth — vehicle positions, weather, sensor readings — and it means a KML can be a few lines long and still show a constantly updating dataset. It also means a shared KML may show nothing at all once whatever it pointed at has gone.

Getting a KML into something other than Google Earth

Google My Maps imports it directly and is the easiest way to put a shared map on the web. QGIS reads it for anything analytical. Most GIS software will, though several will want it as GeoJSON or Shapefile first, which is the usual conversion in that direction.

What almost nothing does is display it in place — no browser opens a KML on its own, and a mail client will offer to download it and nothing more. That is the standard reason people convert: not because the format is deficient, but because the place they want to show it cannot read it.

Converting between KML and GPX

From GPX to KML is the straightforward direction: a track becomes a line string, waypoints become placemarks, and styling has to be supplied since the source had none. The result is a file that looks like something rather than a bare line.

The reverse loses whatever GPX cannot express, which is most of what a KML is for — polygons have no equivalent, overlays and network links have none, styling is dropped, and the folder structure flattens. If the KML is really a track, the conversion is clean; if it is a map, expect to keep the original.

The facts, in one place

Identifiers and provenance for the KML format.
Extension.kml
Media typeapplication/vnd.google-earth.kml+xml
Published byKeyhole
SpecificationOGC KML 2.3

KML files: common questions

How do I open a KML file?

Google Earth is the natural home, and Google My Maps imports one directly if you want it on the web. QGIS opens them for analytical work. No browser displays a KML on its own, which is the usual reason people convert.

What is the difference between KML and KMZ?

A KMZ is a ZIP archive containing the KML plus its icons, photographs and overlay images. It is the better file to share, since a bare KML referencing local images shows nothing on another machine, and the compression is substantial.

What is the difference between KML and GPX?

KML describes a map — placemarks, styled lines, filled areas, overlays — and is what Google Earth speaks. GPX describes activity and navigation data — waypoints, routes and recorded tracks — and is what GPS devices speak. Converting a map to GPX loses most of what makes it a map.

Why are my colours wrong?

KML writes colour as alpha, blue, green, red, which is the reverse of the usual hexadecimal order. A value copied from anywhere else comes out with red and blue swapped.

Can a KML file contain photos?

It can reference them, and a bare KML pointing at images on your own computer will show nothing to anyone else. Save as KMZ instead, which packages the images inside the file.

Why does a shared KML show nothing?

Either it references local files that did not travel with it, or it uses a network link pointing at a URL that no longer serves anything. The first is fixed by sending a KMZ; the second cannot be fixed from the file.