Step 1 - Producing basic GPX from multiple queries

GPSBabel is a very powerful commandline tool for managing the large variety of formats for GPS data. It also has the added benefit of providing some extremely useful filters and tools.

Over the following pages, I'm going to step you through the creation of a workflow that I have created that allows me to cache with no paper that draws heavily upon GPSBabel.

Broadly, I have a number of pocket queries from geocaching.com that are sent to me via email. I save the zip files to a directory, run a script, and a number of different GPX files are spat out after a few seconds that I can then use in Google Earth, GPSBabel+ and on my Pocket PC.

To accomplish this I have a variety of pocket queries set up. I need the following.

  • All caches in New Zealand - absolutely all of them, I'll remove ones that are unavailable or I've found later. This requires six queries currently.
  • Caches I have placed.
  • Caches I have found.
  • Caches that are currently unavailable.

All these queries are saved in a directory that contains a shell script that produces the GPX that I require. I'm going to step through this script file as it is at the heart of my caching workflow.

Firstly, I need to remove any files that were produced previously to ensure that no old data is mixed up with the newer pocket queries. Note that the echo statements are just there to tell be where the script is up to - a means of monitoring its progress. The first line identifies the 'shell' that is used to run the script.

I have used \ to signify where the line continues onto the next line - this is purely for display purposes. I'll make the script available for download with correct formatting.

#!/bin/csh
rm *.gpx
echo "Removed temporary files"

Next, I need to loop through a list of all of my queries, and remove the files from the zips that they arrived in.

foreach pocketquery ( 388021 854701 158139 158140 166136 \
    166137 410114 896898 905319 )
  if ( -e $pocketquery.zip ) then 
    unzip $pocketquery.zip >> /dev/null
    echo "Unzipped Pocket Query:$pocketquery"
  endif
end

This first line is a list of the pocket query zip files that come in via email. These don't change once you've set a query up. If you do delete a query and create a new one you need to update the number in this list.

It then tests to see if a zip file actually exists, and if so it extracts the GPX files inside.

Now, with the addition of more advanced waypoint functionality on the geocaching.com website, many pocket queries come with two files - the main .gpx and the -wpts.gpx that contains the additional newer waypoints. Of course I want these, so the first step is to combine these into one file.

First off is to produce a gpx file that contains all the waypoints of caches I have found. Note that because both of the input files are type gpx (-i gpx) I can just chain multiple gpx files together as input. In this case I do it with -f 388021.gpx -f 388021-wpts.gpx.

gpsbabel -i gpx -f 388021.gpx -f 388021-wpts.gpx \
  -o gpx -F caches-world-found.gpx
echo "Produced: Caches Found (World)"

Next I deal with my caches placed. Now, I could use a simple move or copy command, but since most of the other operations are GPSBabel - I'm keeping with the theme. If at some stage I was to have caches in multiple countries, as I had in the past, I would need to use GPSBabel to combine multiple pocket queries representing each country with hides in them.

gpsbabel -i gpx -f 854701.gpx -o gpx -F caches-nz-placed.gpx
echo "Produced: Caches Placed (NZ)"

The mother-of-all-mergers comes next as the six pocket queries (at time of writing) that cover all caches in New Zealand. Naturally, there are two gpx files per pocket query, so the command is quite long. At some point I will script this command to make it simpler to manage and scale.

gpsbabel -i gpx -f 158140.gpx -f 158139.gpx -f 166136.gpx -f 166137.gpx \
  -f 410114.gpx -f 896898.gpx -f 158140-wpts.gpx -f 158139-wpts.gpx  \
  -f 166136-wpts.gpx -f 166137-wpts.gpx -f 410114-wpts.gpx -f 896898-wpts.gpx \
  -o gpx -F caches-nz.gpx
echo "Produced: Caches (NZ)"

Very simply, a whole lot of gpx files are input, and then output into a single file - caches-nz.gpx. This file is then the basis for much of the later work.