R (programming language)/Average tracks

From OpenStreetMap Wiki
Jump to navigation Jump to search

“Averaging” several GPS tracks by combining/merging them into a “line of best fit” is a useful method to make a more precise track out of several imprecise tracks. The following is a process using an R script made by Michiel Faber. Michiel has noted that this isn’t the perfect way to do it, but that it works. He points to the TopoFusion software and a paper they have written for a more sophisticated method. The issue has also been discussed on the gpsbabel misc mailing list.

The script has it's own repository at github.com.

Beware that the script is not perfect and the resulting average track might contain anomalities, you should always look through the average track before using it.


  1. You need to be able to use the terminal.
  2. You need the R software package to run the script. See the download page of the R Project, or:
To install R on Ubuntu, write the following in a terminal (keep the terminal window open, you'll be using it later):
sudo apt-get update
sudo apt-get install r-base-core

How to use

  1. Clean the tracks you want to average. Remember that they should essentially follow the same route, and that if one track has a detour that the other doesn’t have, the resulting “average” detour will not make sense....
  2. Download the R script from github.com, to the directory that you’ll be working in (where you have your input files).
  3. Convert the input files to csv: gpsbabel -i gpx -f mygpstrack1.gpx -o csv -F mygpstrack1.csv
  4. Run the script: R --no-save < average-tracks.R
    • The resulting "average track" is named averageTrack.csv
  5. Convert csv back to gpx: gpsbabel -i csv -f averageTrack.csv -x transform,trk=wpt,del -o gpx -F averageTrack.gpx

You’re done!

Further development

While an “average” filter for gpsbabel using a more sophisticated method could be the better way to go about this, in the short term it shouldn’t be hard to make Michiels script easier to use by wrapping it in a bash (or other) script that could handle files and possibly use gpsbabel to handle the gpx-csv-gpx conversion.

Please use github to report issues, and ask for features.


Screenshot from josm showing an average track. The red track is the average, the two on the sides are the source tracks.


See also