Wednesday, May 23, 2012

Editing GPX files with Garmin extensions

During my outdoor activities I often (not to say always) carry a Garmin GPS device with me. Nowadays current Garmin devices record also heart rate, pedal cadence or even temperature and write these data into a GPX file as extensions.

After my last cycling trip I edited (like I normally do) my tracks in viking, when I realized that all the additional Garmin data are lost. I tried to find another workflow without losing the data in the GPX extensions. Finally OGR was once more my big saver. For the following workflow OGR is required at least in version 1.8.

The idea is to convert the GPX track to a Shapefile, edit it in a GIS and convert it back to GPX.
ogr2ogr -sql "SELECT track_fid,track_seg_id,track_seg_point_id,ele,CAST(time AS character(32)),gpxtpx_TrackPointExtension FROM track_points" track_points.shp 20120517_Cycling.gpx
It is important to name the new layer track_points to make sure that OGR converts the points back to a track again, see also the GPX driver documentation.

Then the track can be easily edited in any GIS:

Edit the Shapefile in QGIS

After editing the layer is converted back to a GPX file:
ogr2ogr -f GPX -dsco GPX_USE_EXTENSIONS=YES -dsco GPX_EXTENSIONS_NS="gpxtpx" -dsco GPX_EXTENSIONS_NS_URL="http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" -sql "SELECT track_fid,track_seg_ AS track_seg_id,track_se_1 AS track_seg_point_id,ele,time,gpxtpx_Tra AS 'gpxtpx:TrackPointExtension' FROM track_points" 20120517_Cyling_edited.gpx track_points.shp

Last but not least the GPX file can be formatted with xmlstarlet:
xmlstarlet fo 20120517_Cycling_edited.gpx


2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hey Adrian, this is exactly what I was looking for.

    I have been using a Garmin Forerunner 305 and sometimes I wanted to edit it, but I did not know how to do it.

    It seems that solve my problem but I am a newbie with some terms here and I would you ask for your help.

    My GPX file has the following structure.

    <?xml version="1.0" encoding="utf-8"?>
    <gpx xmlns:tc2="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tp1="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="TC2 to GPX11 XSLT stylesheet" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd">

    <trk>
    <name>2013-01-07T22:50:25Z</name>
    <trkseg>
    <trkpt lat="-25.4765223" lon="-49.2598176">
    <ele>892.2000122</ele>
    <time>2013-01-07T22:50:25Z</time>
    <extensions>
    <tp1:TrackPointExtension>
    <tp1:hr>153</tp1:hr>
    </tp1:TrackPointExtension>
    </extensions>
    </trkpt>

    What would be the right way to execute ogr2ogr because I tryed here with some errors.

    What I tryed is

    ogr2ogr -sql "SELECT track_fid,track_seg_id,track_seg_point_id,ele,CAST(time AS character(32)),gpxtpx_TrackPointExtension FROM track_points" track_points.shp myfile.gpx

    error message
    ERROR 1: SQL: Unrecognised field name TrackPointExtension.

    ReplyDelete