Thomas Fischer's Weblog

Life, Linux, LaTeX

Archive for May 2009

Drawing GPS Traces on Map Tiles from OpenStreetMap

with 7 comments

Important note: I have created a page in the OpenStreeMap Wiki which gets updated along with updates in this script. Therefore the information on this blog page is partially outdated.

Ever wondered how to show your GPS traces to non-techie friends? This article may be the answer to your question.

Last weekend, I had the problem of visualizing a GPS trace to show a friend where I had been. Sending a .gpx file may be an answer to this question, but for someone not related to this whole map-making voodoo this file won’t help.

Well, given that a world map rendered as .png files is freely available from the OpenStreetMap project, isn’t there a way to draw GPS tracks on those images? The OpenStreetMap project allows you to download map tiles of size 256×256 pixel. To access those tiles (and to know which tile), you have to calculate the tile number depending on latitute, longitude, and zoom level. Using the coordinates from a .gpx file, all neccessary tiles can be retrieved and glued together to your own map. On this map, the tour as described by the .gpx file can be drawn.

Script

To simplify this process, I wrote a Perl script, which takes one or several .gpx files as input and generates a PNG image containing the map tiles and the GPS tracks as colored lines.

The script is available for download, released under the GPL version 3 or any later version.

The script’s usage is quite simle. Run

perl gpx2png.pl GPSTRACK.gpx

to generate a file called map.png which contains the track in a PNG file.

The output can be controlled by setting command line options:
-o FILENAME: Output filename of the image.
-z N: Zoom level, where N is a number between 1 (world) and 16 (most details) or “auto” (automatically determines a reasonable zoom level, default).
More command line options are shown when running the script with -h.

If you are familiar with Perl, you can change even more default settings (e.g. colors) at the start of the Perl script.
Perl hackers are welcome to send bug reports (I’m sure there are bugs) and suggestions for improvements.

Example

Below example visualizes two GPS tracks from Skara region (randomly selected) using the script’s default settings:

perl gpx2png.pl -o skara-default.png 369116.gpx 369119.gpx
Visualization of two GPS tracks close to Skara, Sweden

Visualization of two GPS tracks close to Skara, Sweden

Using custom settings such as done by running

perl gpx2png.pl -z 7 -o skara-zoom7.png 369116.gpx 369119.gpx

results in this image:

Two GPS tracks in the Skara region with zoom level 7

Two GPS tracks in the Skara region with zoom level 7

For reference, see how the original map looks like.

Update 20090623 I uploaded a new version which is more configurable and draws waypoints as small circles on the map.

Update 20090709 The new version improves the projection of coordinates on tile images (suggested by Pedja); tile sources can be selected via command line.

Important note: I have created a page in the OpenStreeMap Wiki which gets updated along with updates in this script. Therefore the information on this blog page is partially outdated.

Advertisements

Written by Thomas Fischer

May 4, 2009 at 21:33

Posted in openstreetmap

Tagged with , ,