Processing GPX Tracks Using Javascript

How to get more of your exported GPS data

Lucas Andión
Trabe
4 min readAug 10, 2020

--

Photo by Immo Wegmann on Unsplash

GPS data can be stored in a really long variety of formats, but GPX, Garmin’s TCX and google’s KML are the most widely used.

Most of our GPS capable wearables let you export your data in GPX format, and that’s what we are going to analyze.

The GPX format

GPX documents are just XML files that store geographical coordinates.

They contain a metadata header, followed by waypoints, routes, and tracks. There’s also an extensions section for custom elements. You can check the whole schema here.

Routes are designed to be followed and tracks are more like breadcrumb trails, storing real data from an activity.

A Route and one track exploring it.
  • Route: an ordered list of waypoints, representing a series of turn points leading to a destination. It also stores information about the route itself.
  • Waypoint: basic element of a GPX track. It holds the latitude and longitude coordinates, elevation, timestamp and metadata for every point on a track/route. They may have a name, a description and all sorts of information under extensions.
  • Track: an ordered list of track points describing a path. They can (and should but it depends on the implementation) be divided in track segments if the GPS collection is lost.
  • Track point: Just a waypoint on a track.

One GPX file can contain multiple routes or tracks, each one consisting in multiple segments.

Tracks are the subject of our interest while parsing GPS data, because they are the raw information that may need processing.

Parsing the GPX

Every manufacturer is responsible of generating valid GPX, but it is not always the case. The format even enforces including the URL of the software that generates it so people is able to contact its creator in case it fails to validate.

Even with valid GPX, there’s many ways a device can generate the file, different time intervals between track points, different extensions, precision, divided track segments…

In this example we will center our parsing on Amazfit Bip’s exported data. This cheap GPS device stores just one track with a track point every 1 (or more seconds) with decent accuracy except for the elevation. It also gives you your heart rate measurement in extensions.

The format depends on the exporting tool used. In this case it’s more or less well formatted and valid GPX, but I’ve seen tools generating invalid code.

You can could just use any XML parser, but I’d recommend gpx-parser-builder.

Every export tools I tested on my Amazfit Bip / Bip S stored all the information in one track (<trk>) with one track segment (<trkseg>) so I’ll be getting just those track points. We’ll be doing calculations that may not be accurate if we mix separate track segment’s data (since devices should separate it in segments when the GPS signal is lost).

This is how our file looks after parsing.

Using your GPX data

So now that you have access to your track’s data… what?

Well we can use it to enhance its information, the basic being calculating distances and speed. Some devices (for example the Bip S) give you the speed at each track point but it does not have to be the case.

There’s many ways of calculating distances with GPS coordinates, we’ll go with the Haversine formula which assumes the earth is a perfect spherical object.

Using it and estimating Earth’s radius in 6371km we can calculate distances in meters between two waypoints like this.

Now we can iterate over our waypoints and “enhance” them with more information, like the distance from the previous point or the average velocity of the segment, etc.

What now

Now you could do any sort of calculations with this data depending on what you want to do. Extract analytics from your tracks, draw an elevation diagram or just show the track on a map with enhanced information.

In my case I use my watch while I surf so I want to identify how many waves I catch, when I am riding a wave, how long it is, my average speed riding it and show all this information on a map of waves for a session.

--

--

Lucas Andión
Trabe
Writer for

Galician. Software developer @trabe. Bike lover, beer enthusiast, mad traveler, beagle friend, surfer wannabe — https://andion.github.io