Hack 51. Speak in Geotongues: GPSBabel to the Rescue

Hack 51 Speak in Geotongues GPSBabel to the Rescue

Too many formats! Use the Swiss Army Chainsaw of tracklog and waypoint tools to convert between different formats and to read and write to your GPS.

Your tracklog and waypoint data is available in too many darn formats. GPSBabel allows you to convert data to different formats and transfer it between different GPS units and your computer. You can also merge multiple files, while checking for duplicates, and filter waypoints based on their distance from a point or a route.

You can set up a route that you plan to travel and then have GPSBabel load your GPS with all of the geocaches that were near your planned route. A big use is to read waypoints and tracklogs from your GPS unit and save them in the GPX format that can be used by many of the hacks in this book.

GPSBabel is available for Macintosh, Windows, and *NIX at http://gpsbabel.sourceforge.net. There are binary packages available for Windows and Mac OS X. For Windows and OS X, download the package and decompress the files.

Under Linux, download the source package, decompress it, and run make.

5.7.1. Using the Graphical User Interface

There are GUI wrappers for GPSBabel for each platform.

  • Under Windows, run gpsbabelfront.
  • Under Mac OS X, run MacGPSBabel.
  • Under Linux, run guibabel. You must ensure that the gpsbabel program is in your path before starting guibabel.

Figure 5-4 shows the Windows version of GPSBabel, set up to read waypoints from a Garmin GPS via the serial port, com1, and save them to the GPX format file named waypoint.gpx.

Figure 5-4. GUI wrapper for GPSBabel, gpsbabelfront, under Microsoft Windows

Much of the power of GPSBabel is masked by the graphical user interfaces, however. As an example, only the Mac OS X GUI allows access to the Filter commands, and even it does not support the newer filter options.

5.7.2. Using GPSBabel from the Command Line

As with many powerful tools, the command line is where the interesting functionality lives. The trade-off is a small learning curve. The basic GPSBabel command looks like this:

gpsbabel [options] -i INTYPE -f INFILE -o OUTTYPE -F OUTFILE

INTYPE and OUTTYPE are formats that GPSBabel understands, and INFILE and OUTFILE are the locations of that data. That location can be either a file or the name of a physical device, such as com1 under Windows or /dev/ttyS0 under Linux.

Let's say you have latitude, longitude, and waypoint name in the file way.csv:

40.70175, -103.68998, First Waypoint 
39.28553, -123.79357, Another point
42.49638, -108.72995, And a third

Use this command to convert this file to the GPX-format file way.gpx:

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

Deconstructing the command line, -i csv indicates that the input file uses comma-separated values; -f way.csv (note that this is lowercase) specifies the name of the input file (or the name of the input serial port); -o gpx specifies the output is a GPX file; and -F way.gpx (uppercase "F") provides the name of the output file (or output serial port).

You can then convert that GPX file to an HTML file that provides a summary list of the waypoints, with references to extended descriptions of each point:

$ gpsbabel -i gpx -f way.gpx -o html -F way.html


5.7.3. Exchanging Waypoints and Tracklogs with a GPS

GPSBabel directly supports Garmin and Magellan GPS units. Connect your GPS to your serial cable, or see [Hack #50] if you lack a serial port. A Garmin serial GPS is specified with -i garmin, while a Magellan is indicated with -i magellan. Use the -f and -F parameters to specify the serial port for your GPS: use -f when you read from the GPS and -F to write to the GPS.

You can get more information by running gpsbabel -? and by reading the README file. Here is an example of a command that reads waypoints from a Garmin GPS and writes them to a GPX-formatted file named waypoint.gpx. Under Windows, assume the GPS is attached to the first serial port:

C:> gpsbabel -i garmin -f com1 -o gpx -F waypoint.gpx

Under Linux, assume the GPS is attached to the first serial port (you will need to run as a user who has rights to read the serial port, so you should probably run as root or with sudo).

$ gpsbabel -i garmin -f /dev/ttyS0 -o gpx -F waypoint.gpx

Under OS X, assume the GPS is attached to a serial-to-USB adapter:

$ gpsbabel -i garmin -f /dev/cu.usbserial0 -o gpx -F waypoint.gpx

To read tracklogs, just add a -t in front of the input (-i) specifier. So, to input tracklogs under Windows:

C:> gpsbabel -t -i garmin -f com1 -o gpx -F tracklog.gpx

In the same way, you can read or write routes with the -r parameter:

C:> gpsbabel -r -i garmin -f com1 -o gpx -F route.gpx

You can also write tracklogs, waypoints, and routes back to your GPS by swapping the input and output parameters. To write waypoints from waypoint.gpx to a Garmin unit under Windows:

C:> gpsbabel -i gpx -f waypoint.gpx -o garmin -F com1

GPSBabel uses the same command format for Windows, Linux, and OS X, but the name of the serial device varies with each of these platforms.

5.7.4. Using GPSBabel to Merge Files

You can specify more than one input file and more than one output file, so you can use GPSBabel to merge multiple files. This command will merge the GPX-format waypoint files way1.gpx and way2.gpx and put them into both the GPX file merge.gpx and the Geocaching loc file cache.loc:

$ gpsbabel -i gpx -f way1.gpx -f way2.gpx -o gpx -F merge.gpx -o geo -F cache.loc

This is useful when you have multiple files full of waypoints and you want to create one canonical list. This is especially useful when combined with the -x duplicate filter (discussed in "Using Filters") to remove waypoints that mark the same place.

5.7.5. Using Filters

GPSBabel also lets you filter your results. This command takes two GPX files as inputs and then outputs those waypoints within five miles of 40.75ºN, 119.25ºW into the GPX file merge.gpx:

$ gpsbabel -i gpx -f way_iii.gpx -f way_rino.gpx 
-x radius,distance=5M,lat=40.75,lon=-119.25 -o gpx -F merge.gpx

You can also filter waypoints by different criteria. This command searches two GPX files, way_iii.gpx and way_rino.gpx; outputs all the waypoints that are within 10 miles of the toll plaza of the Golden Gate Bridge; and then sorts that list by waypoint name. Since there is no output type (-o), GPSBabel sends the output to the terminal:

$ gpsbabel -i gpx -f way_iii.gpx -f way_rino.gpx 
-x radius,distance=10M,lat=37.81,lon=-122.47 -x sort 
37.768593N 122.470796W BRDTRE/CRTD 13:42 06-JAN-01
37.781870N 122.404310W CAACADSCNC/CAACADSCNC 19.000000
37.760660N 122.419305W CANCUN/CANCUN 11.000000

So you could load your GPS with San Francisco waypoints with this command:

C:> gpsbabel -i gpx -f way_iii.gpx -f way_rino.gpx 
-x radius,distance=10M,lat=37.81,lon=-122.47 -x sort -o garmin -f com1

You can also use GPSBabel to remove duplicates and nearby points. We can guarantee that we have duplicates to test by merging two copies of the same file:

C:> gpsbabel -i gpx -f way_iii.gpx -f way_iii.gpx 
-x radius,distance=10M,lat=37.81,lon=-122.47 -x sort -o gpx -F dupes.gpx

The file dupes.gpx now contains two copies of each of our waypoints that is within 10 miles of 37.81ºN, 122.47ºW.

We can verify that we now have duplicate waypoints:

C:> gpsbabel -i gpx -f dupes.gpx 
37.768593N 122.470796W BRDTRE/CRTD 13:42 06-JAN-01
37.768593N 122.470796W BRDTRE/CRTD 13:42 06-JAN-01

We can remove the dupes with the duplicate filter and show them to the screen by leaving off the -o output specification:

C:> gpsbabel -i gpx -f dupes.gpx -x duplicate,shortname,location
37.768593N 122.470796W BRDTRE/CRTD 13:42 06-JAN-01

We can use -x duplicate,shortname to remove duplicates based on name, use -x duplicate,location to remove duplicates based on location, and use -x duplicate,shortname,location to remove only those waypoints that share the same name and location.

There are certain problems with floating-point precision when transferring data to and from Garmin GPS units. For this reason, you may want to couple a duplicate removal with the -x position filter. This filter removes all waypoints that are too close to each other.

So assume that we have two waypoints that are just slightly different from each other:

37.7685N 122.470796W BRDTRE/CRTD 13:42 06-JAN-01
37.7686N 122.470796W BRDTRE/CRTD 13:42 06-JAN-01

Look closely at the latitudes: 37.7685 versus 37.7686. In this case, these two points are within about 35 feet of each other. Let's remove the duplicates and then chain the results to the next filter to remove close points:

C:> gpsbabel -i gpx -f dupes.gpx -x duplicate,shortname,location 
-x position,distance=36f 
37.768600N 122.470796W BRDTRE/CRTD 13:42 06-JAN-01
37.803703N 122.450373W EXPLRE/CRTD 16:54 04-MAR-01
37.807512N 122.476037W GGATE /CRTD 11:33 27-AUG-00

Note how we can chain together the -x filters. In this case, we first get rid of the duplicates and then we remove every waypoint that is within 36 feet of another one. The program walks through waypoints in order and rejects those that are within 36 feet of a point that has already been loaded.

As befits a Swiss Army Chainsaw, GPSBabel has a dozen other powerful features, but one final interesting feature is the -x arc filter. You can convert a tracklog to a GPSBabel arc file and then filter waypoints based on their proximity to that "arc," or route.

Let's look at our allwaypoints.gpx file using the Unix word-count filter:

$ gpsbabel -i gpx -f allwaypoints.gpx | wc
486 2563 23669
There are 486 waypoints in the file.

Then we create a GPSBabel arc filter file from a tracklog:

$ gpsbabel -i gpx -f track.gpx -o arc -F arcfile.txt

Then we apply the arc filter to include only those waypoints that are within one mile of that track:

$ gpsbabel -i gpx -f allwaypoints.gpx -x arc,file=arcfile.txt,distance=1M | wc
 61 322 3022

So 61 of our waypoints were within one mile of the path described in track.gpx.

This is interesting because as you fill your waypoint memory with points, it is easy to forget where those points are. When I ran this arc filter test, I was surprised when my waypoint "PEACE" appeared. I was sure that was the Peace Dive boat in Ventura, California. So I explored:

$ gpsbabel -i gpx -f allwaypoints.gpx | grep -i PEA
38.403327N 122.847930W PEACE /CRTD 15:52 15-SEP-00
34.241402N 119.263898W PEACEB/CRTD 17:25 16-OCT-01

Ah! Now I remember! "PEACE" is the Peace Park near my house. It was certainly within one mile of the track in the filter example. The Peace Dive boat was "PEACEB," for "Peace Boat." This sort of name collision is inevitable when you start seriously hoarding waypoints in a device with a six-character limit on names! Tools such as GPSBabel filters can help make some sense out of the whole mess.

Mapping Your Life

Mapping Your Neighborhood

Mapping Your World

Mapping (on) the Web

Mapping with Gadgets

Mapping on Your Desktop

Names and Places

Building the Geospatial Web

Mapping with Other People

Mapping Hacks
Mapping Hacks: Tips & Tools for Electronic Cartography
ISBN: 0596007035
EAN: 2147483647
Year: 2004
Pages: 172

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net