6.4 KML and GPX
Two XML-based formats you'll meet often — KML for Google Earth, GPX for GPS tracks.
Key takeaways
- KML (Keyhole Markup Language) is an OGC standard built around Google Earth's 3D presentation.
- GPX is the de facto standard for GPS tracks, routes, and waypoints.
- Both are XML-based and interoperable, with known quirks.
Introduction
Most modern GIS uses binary or JSON formats, but you'll still encounter plenty of KML and GPX in the wild — especially from consumer mapping tools, navigation devices, and fitness apps. This short lesson covers the essentials of both.
KML
KML was developed by Keyhole Inc., acquired by Google, and published as an OGC standard in 2008. It's XML that describes:
- Placemarks (points) with names, descriptions, and styled icons.
- Paths and polygons.
- Overlays (ground, screen, photo).
- Folders and network links (referenced KML resources).
- Camera views and tours.
A tiny KML file:
1<?xml version="1.0" encoding="UTF-8"?>
2<kml xmlns="http://www.opengis.net/kml/2.2">
3 <Placemark>
4 <name>Atlas HQ</name>
5 <Point>
6 <coordinates>12.5683,55.6761,0</coordinates>
7 </Point>
8 </Placemark>
9</kml>Notice the coordinate order: longitude, latitude, altitude — comma-separated.
KMZ
A KMZ is a ZIP archive containing a KML plus referenced resources (icons, images). It's the preferred distribution format — one file, compressed.
Strengths
- 3D-ready (altitude, extrusions, models).
- Rich presentation (styling, labels, tours).
- Consumer-friendly (Google Earth opens it directly).
Weaknesses
- XML bloat (files are much larger than binary equivalents).
- Presentation is mixed with data (styling in the file).
- Not ideal for analysis workflows.
Converting to/from KML
1ogr2ogr -f KML out.kml parks.gpkg
2ogr2ogr -f GPKG out.gpkg trail.kmlGPX
GPX (GPS Exchange Format) is an open standard for representing GPS data:
- Waypoints — named points of interest.
- Routes — ordered lists of waypoints (planned).
- Tracks — recorded sequences of positions (actual).
A GPX file for a hike:
1<?xml version="1.0" encoding="UTF-8"?>
2<gpx version="1.1" creator="GPS device" xmlns="http://www.topografix.com/GPX/1/1">
3 <trk>
4 <name>Sunday hike</name>
5 <trkseg>
6 <trkpt lat="55.6761" lon="12.5683">
7 <ele>3</ele>
8 <time>2025-04-06T09:00:00Z</time>
9 </trkpt>
10 <trkpt lat="55.6762" lon="12.5685">
11 <ele>4</ele>
12 <time>2025-04-06T09:00:10Z</time>
13 </trkpt>
14 </trkseg>
15 </trk>
16</gpx>Strengths
- Native support in almost every GPS device and app (Garmin, Strava, Komoot, Apple Health, Polar).
- Time-indexed — the default representation of a GPS track.
- Small schema — easy to parse.
Weaknesses
- XML verbosity; GPS traces can be large.
- Limited attribute flexibility — add custom tags via
extensions. - Single CRS only (WGS 84).
GPX processing
import geopandas as gpdModern GPS tools often also emit FIT files (binary, Garmin-specific). Libraries like fitparse (Python) convert FIT ↔ GPX.
When to use each
| Need | Choice |
|---|---|
| Share a location pin with a non-GIS user | KML / KMZ via email or AirDrop |
| 3D presentation in Google Earth | KML / KMZ |
| Record a hike on a phone | GPX |
| Export from Strava, Komoot, Garmin | GPX |
| Integrate with GIS analytics | Convert to GeoPackage |
Common pitfalls
- Axis order: KML is
lon, lat, alt; GPX useslatandlonas XML attributes. Don't confuse. - Altitude modes in KML (
clampToGround,relativeToGround,absolute) matter for 3D display. - Large GPX files from minute-interval trackers: 40 000+ points in a long hike; thin before analysis.
- KML embedded imagery: KMZ can balloon to hundreds of megabytes; extract before processing.
Self-check exercises
1. What's the difference between KML and KMZ?
KML is a single XML text file. KMZ is a ZIP archive containing a KML plus any referenced resources (icons, images, 3D models). KMZ is preferred for distribution because it's compressed and self-contained.
2. What three GPS concepts does GPX represent?
Waypoints (named points of interest), routes (ordered lists of waypoints — a plan), and tracks (recorded sequences of positions — actual movement). Tracks are divided into track segments (trkseg), useful for pauses and resumes.
3. A runner sends you their GPX file with 15,000 points for an hour-long run. How would you reduce it for analysis without losing route shape?
Apply a simplification algorithm — Douglas-Peucker or Visvalingam — with a small tolerance (say 2 m). gpx.simplify(tolerance=2) in GeoPandas/Shapely typically drops 60–80 % of points while preserving the route's visual shape.
Summary
- KML is for presentation (Google Earth, consumer maps); KMZ is its compressed cousin.
- GPX is the GPS-track lingua franca.
- Both are XML, both are universally supported, both have WGS 84 as the only CRS.
- Convert to a modern binary format (GeoPackage, GeoParquet) for analysis.
Further reading
- OGC KML 2.3 specification.
- Topografix GPX 1.1 schema.
- gpsbabel — swiss-army knife for GPS format conversion.
- GDAL drivers for KML and GPX.