Datareduction of GPX files with XSLT and GPSBabel

Most GPS heart rate monitors allows the individual training units to be saved as a GPX file – in addition to the geodata, other data are also included that are not necessary for the display in maps and the resolution is far above that required for integration into a homepage – e.g. with WordPress and the OpenStreetMap plugin OSM.
Using the practical example of a half marathon with the Garmin Forerunner 305, this article shows how a reduction to approx. 16% of the GPX file size optimizes the loading time of a homepage without recognizing any significant losses in accuracy. All tools are available under Win, Linux and Mac.

Single steps with their progress:

The individual steps with their progress:

  1. Recorded GPS data: 341 kByte
  2. Reduce data for each waypoint with XSLT: 92.2 kByte
  3. Reduce number of waypoints with GPSBabel: 58 kByte

Adapt the GPX file to your needs with XSLT

If you open the GPX file with a text editor you will find individual points of your training session in the following form:

<trkpt lat="35.8612283" lon="129.2111794">
<ele>40.5128174</ele>
<time>2010-10-17T00:07:14Z</time>
<extensions>
<tp1:TrackPointExtension>
<tp1:hr>127</tp1:hr>
</tp1:TrackPointExtension>
</extensions>
</trkpt>

However, only the geodata are required for the display on the map, i.e. lat and lon (marked in green). Nick gave me the following lines of XSLT code to eliminate the unnecessary data (marked in red) in the GPX file:

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:fn="http://www.w3.org/2005/xpath-functions"
 xmlns:gpx11="http://www.topografix.com/GPX/1/1">
 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- Root node, elements and attributes: Copy the node and recurse -->
 <xsl:template match="/|*|@*">
 <xsl:copy>
 <xsl:apply-templates/>
 </xsl:copy>
 </xsl:template>
<!-- trkpt element: Copy the node; process lat and lon attributes only -->
 <xsl:template match="gpx11:trkpt">
 <xsl:copy>
 <xsl:apply-templates select="@lat|@lon"/>
 </xsl:copy>
 </xsl:template>
 </xsl:stylesheet>

To apply this code to the GPX file we simply use the XSLT service from online-toolz and enter its input file in the left column, the code in the right column and then I get the new GPX file with 69.2kByte, which I save under 2011_HM_Gyeongju_latlon.gpx. Compared to the input file 2011_HM_Gyeongju.gpx with 341.7kByte, this is already a reduction to 20% of the file size.

Adapt the number of points in the GPX file using GPSBabel

If you want to show a run over several hours on a map, you will not need a point every 5 seconds of the run. Assuming that you run steadily, you could for example for a half marathon (21,000 meters) represent a point every 20 meters. That would be 1,050 points. The GPSBabel command is simple:

gpsbabel [Optionen] -i TypeOfInputFile -f NameOfInputFile [Filter] -o TypeOfOutputfile -F NameOfOutputfile

For my example file with 2011_HM_Gyeongju_latlon.gpx (69.2kByte) I choose two filters, one to “simplify” the route and one to limit the number of points to 1,050:

gpsbabel -i gpx -f 2011_HM_Gyeongju_latlon.gpx -x simplify,count=1050 -o gpx -F 2011_HM_Gyeongju_latlon_2100p.gpx

This reduces the size of the file again to 58kB and thus to approx. 16% of the original size with sufficient resolution of the route for the homepage.

Display of the optimized GPX file in Stamen toner with the WordPress OSM plugin

Map created with the WordPress OSM plugin (zoom with mouse button while holding down the Shift key). The file of the route shown is only 16% of the file size as you get it from the Garmin Forerunner 305.

Of course, the two steps (XSLT and GPSBabel) can also be carried out individually, if, for example the XSLT code for the own GPS watch file does not match. However, it can be seen that the decisive factor is removing the unnecessary information in the individual waypoints.

An alternative form of data reduction would of course simply be to run faster 😉

Related links

GPSBabel
XSLT Service von online-toolz
pyTrainer