GPX

From Wildsong
Jump to navigationJump to search

Overview

GPX is a format for exchanging GPS data.

The three main data types are waypoints, routes, and tracks.

Waypoint: a waypoint, a point of interest, or a named point on a map. Has all the fields need to represent a point collected with GPS including PDOP, type of fix (2d, 3d, dgps) sats in view, etc.

Route: an ordered list of waypoints representing turns

Track: an ordered list of points representing a path. The track segments are represented by waypoints.

What this means is that GPS can accomodate everything needed for professional work. There is no reason to throw out data like fix when creating a GPX file.

Merge GPX geocache files, removing duplicates

Use case: you have several Pocket Query files that overlap. You want to merge them together but also want to get rid of duplicate points.

Solution: use gpsbabel with a filter.

gpsbabel -i gpx -f 12371388_TableRock.gpx -f 12371368_AshlandMedford.gpx -f 12371320_AshlandCaveJunction.gpx -x position,distance=2m -o gpx -F AshMedCave.gpx

Use ogrinfo to check results. The three input files contain a total of but with duplicates removed... 500+384+1000 = 1834 but result file has 1630

for i in *.gpx; do echo -n $i " "; ogrinfo -so $i waypoints | grep Feature\ Count; done
12371320_AshlandCaveJunction.gpx  Feature Count: 500
12371328_CaveJunctionGoldBeach.gpx  Feature Count: 384
12371368_AshlandMedford.gpx  Feature Count: 1000
12371388_TableRock.gpx  Feature Count: 500
AshMedCave.gpx  Feature Count: 1630 

Shapefile to GPX route

ogr2ogr -f gpx bog_route.gpx bog_route.shp bog_route

Extensions

The GPX standard allows for arbitrary extensions. Basically anyone can add additional fields to the GPX format, and if the program you are using does not know about the extension, the data contained therein simply is not available.

An example of this is the Groundspeak format used for geocaching.

Loading GPX data into PostGIS

If you want to transfer the contents of a GPX file into a PostGIS database (and who doesn't!), you can use ogr2ogr. See the GPX driver notes for command options.

There is another way to load the GPX directly, you should really get the "PostGIS in Action" book. You might be able to read geocache files that way.

Get the GPX file from your handheld device. On a newer GPS this means connect a USB cable and copy the file. On an older one then I suggest using gpsbabel.

Then use commands similar to these

# View the contents of the file
ogrinfo 20111016_Garmin.gpx 
Had to open data source read-only.
INFO: Open of `20111016_Garmin.gpx'
      using driver `GPX' successful.
1: waypoints (Point)
2: routes (Line String)
3: tracks (Multi Line String)
4: route_points (Point)
5: track_points (Point)

# Load the GPX file into a database
ogr2ogr -f PostgreSQL PG:"host=localhost user=postgres dbname=gisdata active_schema=ci_corvallis_or" \
20111016_Garmin.gpx \
-overwrite -lco GEOMETRY_NAME=the_geom tracks track_points waypoints

That's it, you should now have tables in your gisdata database under the ci_corvallis_or schema. With a little effort you can script the whole process to streamline it.

This shows PostGIS output but you can write to any format supported by ogr2ogr.

External links

GDAL driver page: http://www.gdal.org/drv_gpx.html

Official GPX documentation: http://topografix.com/GPX/1/1/

(GPX supports extensions. There are extensions from various manufacturers, I should put more links here.)