UK Map App

GPX Hacks for UK Map

As part of the celebration of UK Map's 10th birthday, and knowing that many users will be spending more time sat at home rather than being out in the countryside, I offer this page of "hacks" to create GPX files for use with UK Map in interesting ways. Some of this is specific to UK Map, while other ideas will also work with other apps that understand GPX.

These make use of various open-source tools and websites, including GPSBabel, GDAL, xsltproc, Open Route Service, Routino, and Overpass Turbo.

These ideas do generally require a fair level of technical skill, e.g. editing text files and using command-line programs; these are not step-by-step instructions. The target audience is perhaps people with some software development or GIS software experience. I hope you find it interesting!


Converting old GPS files using GPSBabel

If you've had a GPS device for a long time, you may have a collection of old tracks or waypoints. GPSBabel can translate from dozens of such formats to GPX.

For example I have an old Sony GPS logger which stores files in NMEA format. I can convert that to GPX as follows:

$ gpsbabel -i nmea -f example.nmea -o gpx -F example.gpx

Here's the result:


Exporting Waypoints to a Spreadsheet

Say you have a collection of waypoints that you've saved in UK Map, and you'd like to view them in a spreadsheet. You can do that by using GPSBabel to convert from GPX to CSV. Here are some of the campsites in the Lake District:

I can convert that to a CSV file as follows:

$ gpsbabel -i gpx -f campsites.gpx -o csv -F campsites.csv

And view the result in a spreadsheet:

GPSBabel actually has more than one CSV input/output mode; the universal CSV mode is probably more useful than the default csv mode as it includes more elements from the GPX:

You can edit the CSV file in your spreadsheet and then convert it back to GPX:

$ gpsbabel -i unicsv -f campsites.csv -o gpx -F campsites.gpx

Converting a Shapefile using GDAL

A shapefile, typically with the filename extension .shp, is a common format for exchanging geographic data. You'll often encounter it when receiving data produced on a professional GIS system. It's possible to convert it to GPX using GDAL's ogr2ogr utility.

GDAL is a powerful system for reading, writing and converting many geographic file formats. It's a collection of command-line tools that run on most operating systems. ogr2ogr is its vector file conversion utility.

As an example, the Environment Agency publishes data about areas that have suffered flooding. On that website you can select an area of interest and download a zipped shapefile.

Here's how to convert that to a GPX file that you can view with UK Map:

$ unzip EA_RecordedFloodOutlines_SHP.zip
$ cd data
$ ogr2ogr -f gpx \
> -s_srs EPSG:27700 -t_srs EPSG:4326 \
> -dsco GPX_USE_EXTENSIONS=YES \
> -nlt MULTILINESTRING \
> keswick_floods.gpx Recorded_Flood_Outlines.shp

Points to note:

Here's the result:


Modifying GPX files using XSLT

GPX files are XML; XSLT is a language for transforming XML files. Possible uses include filtering the contents of a file, modifying metadata, changing styles and so on.

As an example, in the previous section ogr2ogr has copied some attributes from the input Shapefile into a GPX <extensions> element in the XML namespace http://osgeo.org/gdal:

  <extensions>
    <ogr:event_code>1555</ogr:event_code>
    <ogr:outline_co>7103</ogr:outline_co>
    <ogr:start_date>1977/10/31</ogr:start_date>
    <ogr:end_date>1977/10/31</ogr:end_date>
    <ogr:bndry_src>visual</ogr:bndry_src>
    <ogr:flood_src>main river</ogr:flood_src>
    <ogr:flood_caus>channel capacity exceeded (no raised defences)</ogr:flood_caus>
    <ogr:fluvial_in>1.0</ogr:fluvial_in>
    <ogr:tidal_ind>0.0</ogr:tidal_ind>
    <ogr:coastal_in>0.0</ogr:coastal_in>
    <ogr:hfm_ind>2.0</ogr:hfm_ind>
    <ogr:shape_leng>5893.693613329999607</ogr:shape_leng>
    <ogr:shape_Le_1>5893.693613329999607</ogr:shape_Le_1>
    <ogr:shape_Area>375507.017599</ogr:shape_Area>
  </extensions>

It is possible to convert those into GPX metadata that UK Map will display; here is an example of how to do that using xsltproc:

$ cat flood_metadata_to_gpx.xslt
<?xml version="1.0"?>
<xsl:transform
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:gpx="http://www.topografix.com/GPX/1/1" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:ogr="http://osgeo.org/gdal"
>

  <!-- Copy gpx elements and attributes unchanged. -->
  <xsl:template match="gpx:*|@*">
    <xsl:copy>
      <xsl:apply-templates select="@*|*|text()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Except the extensions element. -->
  <xsl:template match="gpx:extensions">
    <xsl:apply-templates select="*"/>
  </xsl:template>

  <!-- Ignore the ogr elements, except as below.  -->
  <xsl:template match="ogr:*"/>

  <!-- Replace ogr:flood_caus with a GPX description. -->
  <xsl:template match="ogr:flood_caus">
    <gpx:desc>
      <xsl:text>Flood Cause: </xsl:text>
      <xsl:value-of select="."/>
    </gpx:desc>
  </xsl:template>

</xsl:transform>


$ xsltproc flood_metadata_to_gpx.xslt keswick_floods.gpx > keswick_floods_out.gpx

XSLT is rather verbose and has a lot of boilerplate, but once you have a basic transformation done then it can be quick to add more features.


Custom Symbols

It's possible to use your own custom symbols for waypoints, using a UK Map extension to the GPX file format. Though the method is not exactly straightforward it should be familiar to people familiar with web technologies i.e. HTML and CSS.

Here's an example: say you want a special marker for your favourite coffee shops. First, create your symbol. I like Inkscape:

Aim to make the symbol about 150-200 pixels in its larger dimension. You want a colour PNG file, with a transparent background, with 8 bits per channel. Do check that you haven't created a 16-bits-per-channel PNG instead:

$ file coffee.png
coffee.png: PNG image data, 200 x 171, 8-bit/color RGBA, non-interlaced

Now you need to encode the PNG data into a base-64 string:

$ base64 coffee.png > coffee.png.b64

Now prepare an empty GPX file. One option is to create the empty file in UK Map and export it. Or use this template:

<?xml version="1.0"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
     xmlns:gs="http://ukmapapp.com/GPX_STYLESHEET/v1"
     version="1.1" creator="UK Map 4.2">
  <metadata>
    <name>coffee</name>
    <desc>Favourite coffee shops</desc>
    <time>2020-05-23T16:30:18+01</time>
    <bounds minlat="64.087677" minlon="19.449671" maxlat="49.766136" maxlon="-7.555903"/>
  </metadata>
</gpx>

Now add a style element to this. It should be a child of the root <gpx> element, for example immediately after the <metadata> element. It is in the gs: xml namespace, which is defined in the gpx element.

The content of the style element uses a syntax based on CSS. You need to define a style rule that matches elements of class "coffee", which sets the attribute marker-image:

<gs:style>
.coffee {
  marker-image: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAACrCAYAA......
.......
.......
....... ";
}
</gs:style>

The value that you supply for marker image should be a data URI containing the base64-encoded PNG image that you created earlier. It will need to span multiple lines. Make sure that the entire URI is enclosed in "".

If you now import the file back into UK Map and choose "New Waypoint" in this file, you should now see the new symbol as one of the choices:

You can now add waypoints to the map using this symbol:

Points to note:


Route Planning using Open Route Service or Routino

Open Route Service and Routino are both online route planning services that use Open Street Map data, and that can export their routes as GPX files. So you can plan a route - driving, cycling or walking, but not public transport - using one of these sites, and then view and follow the route in UK Map.

Open Route Service

Initially you need to type in place names to search for the start and end of the route. Once you've done this once, however, the A and B markers on the map can be dragged around. You can also drag the route itself to add intermediate points.

The "round trip" feature allows you to select a single start/finish point and a distance, and it will suggest a circular route.

You can now click on the download button (a small arrow pointing downwards to a rectangle) to receive a GPX file, which you can view in UK Map:

Routino

Routino is similar; you can select start and end points from the map, find the shortest or quickest route, and download a GPX file. You can choose a GPX route, which has straight lines between road or path junctions, or a track, which has all the intermediate points.

Unfortunately it's difficult to open this file because although it is a GPX file it is not described as such by the server. If you do eventually manage to persuade iOS to open the file in UK Map, you'll see something like this:


Getting Bus Routes from Overpass Turbo

Overpass Turbo is a service that extracts data from Open Street Map based on queries written in its own language. It can export the results as GPX files which UK Map can view.

There is lots of information in Open Street Map that's not shown on the default maps. For example, they have bus route information for some cities. Here's an example of how to extract that.

Type in the query, and press Run to show the results. Then press Export, and choose "download as GPX". You can view that in UK Map:

This clearly needs some tidying up to be useful, but it does give some idea of what is possible with Overpass Turbo.

Thanks for reading.