Miskatonic University Press

Combining multiple GPX tracks into one file

code geo

As I mentioned, I use OsmAnd when I’m out on my walks. I record my “tracks”—the path I take as I walk—and the data is stored in GPX files, one per day. I like to see them all at once on the map, so I can see everywhere I’ve walked, but now I have 53 GPX files and turning them all on or off is a pain. I needed some way to combine them all into one.

It turns out GPSBabel does the trick. I wrote this shell script to do the merging. It depends on all the GPX files being in a gpx/ directory.

#!/bin/bash

(for FILE in gpx/*
 do
     echo -n " -f $FILE"
 done
) > files-to-combine.txt

gpsbabel -i gpx -b files-to-combine.txt -o gpx -F covid-walks.gpx

rm files-to-combine.txt

sed -i "s#<name>.*</name>#<name>Covid-19 Walks</name>#" covid-walks.gpx

The -b is for batch processing and makes it easy to specify all the input files. The sed line gives the new file a nice name, instead of being a concatenation of all of the old ones. There may be some way to do this in GPSBabel (it has a very long list of possible filters) but a GPX file is just XML, which is just text, and good old sed will always do you right in such a case.

Copying covid-walks.gpx into /Android/data/net.osmand.plus/files/tracks/rec on my phone (here’s how I mount it) makes it visible to OsmAnd, so I just have the one file that needs visibility turned on or off.