Thomas Fischer's Weblog

Life, Linux, LaTeX

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 , ,

7 Responses

Subscribe to comments with RSS.

  1. Hi, sorry to disturb. I’m looking for a forum or somewhere to ask a problem I’m having with KBibtex, but I couldn’t find it. I’ve tried to write an email, but they’re all returned (@unix-ag.uni-kl.de)

    Is there a way to comunicate with you?
    Thanks

    Alberto

    June 4, 2009 at 15:08

  2. Fantastic tool !!! It works like charm.
    Thanks,

    mpele

    June 8, 2009 at 11:00

  3. It works very nice if the zoom is less then 9. If the zoom is smaller then the error is great (it draws in other country).

    Pedja

    June 15, 2009 at 13:56

  4. Great tool. Just what I was looking for. I am using it to compare quality of traces of different GPS receivers.
    Thanks

    amm

    August 7, 2009 at 19:28

  5. Thanks, Thomas, this is a great script and has already come in handy with our work in Leipzig, visualizing GPS tracks (see link).

    Daniel Belasco Rogers

    June 1, 2010 at 19:03

  6. Thank you for the great script! I used it to place some photos onto a map within Paris and stumbled over the error that the photos didn’t appear on the right place. After some investigation I discovered that it’s not an error but simply lack of precision. For me the following change worked:

    sub exifgpscoordinates {
    […]
    my ( $londeg, $lonmin, $lonsec ) =
    $gpstext =~ m/Exif.GPSInfo.GPSLongitude\s+([-0-9]+)deg\s+([0-9.]+)’\s+([0-9.]+)”/;
    my ( $latdeg, $latmin, $latsec ) =
    $gpstext =~ m/Exif.GPSInfo.GPSLatitude\s+([-0-9]+)deg\s+([0-9.]+)’\s+([0-9.]+)”/;
    my $lon = $londeg + $lonmin / 60.0 + $lonsec / 3600.0;
    my $lat = $latdeg + $latmin / 60.0 + $latsec / 3600.0;
    […]

    I just added the extraction and calculation of the geographical seconds ($lonsec and $latsec).
    (I am not sure if all GPS-devices produce the seconds as output…)

    This might also solve the problem from Pedja (June 15, 2009 at 13:56)

    Cheers,
    Thomas

    Thomas Lenarz

    July 16, 2012 at 16:50

  7. Has anyone successfully taken the images generated by this script and overlayed them on top of a openstreetmap web map?

    Austin

    April 1, 2014 at 15:58


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: