Tuesday, 3 June 2014

GPX Traces of Long Distance Routes

This post is personal reminder of how to extract GPX Track data from OpenStreetMap.


The UK is full of long distance routes you might want to follow using your GPS.  That could be anything from walking trails like the Pennine Way; parts of the National Cycle Network; or even bus routes.

The most reliable way to follow any recreational route on a GPS is to use a GPX Track. That is just a series of dots joined up with straight lines and drawn over the top of any base map your GPS might have.  Utilising a track instead of a route means your GPS doesn't try to do any clever routing like a car GPS would.

Track data can be tricky to find. But OpenStreetMap can come to the rescue.

OSM Data. A quick primer: 

OpenStreetMap is a database of:
  • Waypoints - single positions with a precise location.
  • Ways - a series of Waypoints that make up any path, road etc. Note that a single road might be split into multiple Ways.
  • Relations - a series of Ways joined together to make a route. Eg Icknield Way. These would typically be over different roads.
  • Waypoints, Ways, and Relations all have key=value pairs to describe what they are.
What I am describing in this page is taking a relation (eg Pennine Way) and converting its collection of Ways (that contain Waypoints) to a single continuous set of Waypoints that can be saved as a Track in a GPX file. Your GPS can then draw those Waypoints/Track as a line over your map for you to follow.

Finding a Relation

If you see a route (Relation) on OpenStreetMap you need to find its database number.  Here's an example, The Pennine Way. Its OpenStreetMap Relation 63872.

A very useful source of Long Distance Path data is in OpenStreetMap's Wiki: UK Long Distance Paths. The Relation number is in the fourth column. Click it to view in OSM.

Another way is on OpenStreetMap itself. If you know a path has a route going over it - lets say you've found a part of the Peddars Way - zoom right in as far as you can (example).  Click the Layers icon on the right hand side. Tick checkbox Map Data, and wait for the lines to be highlighted. Click on line you were looking at. A Panel on the left appears telling you about the line. Scroll to the bottom, and there might be a "Part of Relation ..." listed.  The number is the Relation number you are hunting for.  In the example I gave, that line has two Relations. Peddars Way (walking route) 77958 and Peddars Cycle Way 3144936.

Conversion Process

The overview of the conversion process is:


  1. Get OSM data for one Relation (eg Peddars Way) with its Way and Waypoint data. 
  2. Join the Ways into a single line.
  3. Delete unnecessary key/value attribute data. Delete the Relation.
  4. Save data and convert to GPX Track.
I use JOSM, a powerful OpenStreetMap editor written in Java to do this job. 

Using JOSM

Start JOSM with no data loaded. 
Load your relation data. Choose File -> Download Object.  
In Object ID type in the Relation number you were hunting for.
I leave "Download Referrers" unticked.  
It is important to tick "Download Relation Members".
Click the Download Object Button.

You should now be in the Map view with the shape of your Relation visible.  This normally consists of a number of separate Ways.  You can zoom in and click a line to highlight a single Way.  Note they have a direction - some ways will be pointing in the opposite direction.  We are going to join them together into one single continuous Way.

Highlight everything.  Choose Selection->Select All, or easier press Control-A. Alternatively, zoom out and draw a box around everything.

To make it easy to join all the Ways together. We'll delete the data attributes.  On the right is a Panel titled Tags / Memberships.  The second column probably says <different>.  Delete all the Tags.  You can do this one at a time; highlight the first then shift-click the last; or highlight one, press Control-A to select all, then Delete.

Now we'll check the relation for gaps and fix. One the right, find the "Relations" panel. Highlight the route, and click the pencil button.  This view shows the Ways that are in the relation. Note they are not always in the right order.  Scroll through the list of Ways, most of them will have arrows in the 3rd column, look for any with red squares. You can also highlight in one Way, right click and choose Select Next Gap to make this easier.

If there are any gaps, right click the row, choose Zoom To. In the Main map view, choose the draw line tool and click the squares at each end of the gaps to join. Press escape to stop drawing.  Go back to the Relation editor and repeat.

If you've got this far, you should consider learning how to make a real fix to the data and submit it back, it's not much more work.

If you are certain you've got a contiguous line.  Delete the Relation, highlight it in the panel and Delete.  This leaves just the Ways on the Map view.

Back in the main map window, we'll now join the Ways into one. Make sure all Ways are highlighted (Control-A).  Now join all Ways together, by choosing Tools->Combine Way.  If the lines were contiguous, you'll probably be asked to Reverse and Combine. Do it.  

If this fails, your line is not contiguous. If you get asked complicated questions about tags you didn't delete them, or you may have not deleted the Relation they belonged to.

You might want to change the direction of your Track. See Tools->Reverse Way.

You should name your Way (Track) at this point. Highlight it, in the Tags panel click add.  Key=name Value=<something>

Now export to GPX.  File->Save As.  Name your file <something>.gpx.

If your map data was only that one Way and no relation data it should have exported as a GPX Track which you can use in your GPS.

I like to check my tracks in using OpenRouteConverter.  This program will show multiple tracks, allow you to edit them, join them; and convert from GPX routes to tracks. 

Alternative Export using OSM Route Manager

There is one online tool that looks like it attempts to automate the above process.  I've found in practice it doesn't do a complete job and you have to fix the results in another tool. I use OpenRouteConverter to fix up.

Check out OpenStreetMap Route Manager.

It's fairly obvious how to use it.  Click the plus icon(s) before exporting.

The relation you want might come in (unjoined) sections.

  • The sections are not nicely ordered so piece together in the right way. Each section might have a different direction. You can fix that in OpenRouteConverter.   
  • Each section might have a different direction. OpenRouteConverter can fix this.
  • You can use OpenRouteConverter to join the sections.
  • I've found Route Manager exports as GPX Route. You can use OpenRouteConverter to make this into a GPX Track. 


No comments:

Post a Comment