Saturday, April 24, 2010

GPS Tracking


The other day I was wondering about tracking my bicycle routes, so I actually started to read the manual for my GPS reciever. Turns out, its really easy. Too easy, in fact, an interesting thing about Garmin GPS recievers is that they are always tracking you by default. Thats kind of disturbing, and you might want to turn it off for general use. But its kind of interesting to see your track sometimes.

With position logging on, your GPS track is located in a file called Current.gpx in the Garmin/GPX directory on the reciever. It looks like this..

....
<trkpt lat="45.483459" lon="-73.582859"><ele>3.02</ele><ti
me>2010-02-21T18:09:14Z</time></trkpt><trkpt lat="45.483878"
lon=""><ele>23.21</ele><time="">2010-02-21T18:09:48Z</t
ime></trkpt><trkpt lat="45.483880" lon="-73.583507"><ele>
44.84</ele><time>2010-02-21T18:10:02Z</time></trkpt>
<trkpt lat="45.483800" lon="-73.583487"><ele>40.99</ele><t
ime="">2010-02-21T18:10:07Z</t><trkpt lat="45.483653" lon="-73.5835
04">35.71</trkpt><time>2010-02-21T18</time></trkpt>
<trkpt lat="45.483607" lon="-73.583513"><ele>34.74</ele><t
ime>2010-02-21T18:10:17Z</time></trkpt>
....
A little work with the shell makes it easier to read
  sed 's/><tr/>\n<tr/g;' Current.pgx > Current_formatted.gpx
thus

....
<trkpt lat="45.483459" lon="-73.582859"><ele>3.02</ele><time>2010-02-21T18:09:14Z</time></trkpt>
<trkpt lat="45.483878" lon="-73.583719"><ele>23.21</ele><time>2010-02-21T18:09:48Z</time></trkpt>
<trkpt lat="45.483880" lon="-73.583507"><ele>44.84</ele><time>2010-02-21T18:10:02Z</time></trkpt>
<trkpt lat="45.483800" lon="-73.583487"><ele>40.99</ele><time>2010-02-21T18:10:07Z</time></trkpt>
....
and we can clean out the xml tags like so
  sed 's:[abcdefghijklmnopqrstuvwxyzZT=<>"/]: :g' work_morning.dat > work_morning_cleaned.dat    
giving

....
45.483765 -73.577330 53.01 2010-04-23 11:22:56
45.483493 -73.576993 46.28 2010-04-23 11:23:03
45.483415 -73.576909 44.84 2010-04-23 11:23:05
45.483381 -73.576872 44.36 2010-04-23 11:23:06
....
No, sadly, i dont go to work at 11... its in UTC. I wanted to see it on a map, but was too lazy to actually read the kml spec, so i got a working kml file from somewhere, put my data in longitude latitude order, and manually pasted in the place of the data that was originally there. Like this,
  awk 'NF>1 {print $2 ",",$1 ",",$3}' work_morning_cleaned.dat >work_morning_data.dat
to get

....
-73.574906, 45.482053, 39.55
-73.574535, 45.481752, 40.99
-73.574299, 45.481560, 42.44
-73.573888, 45.481230, 44.36
....
The picture shows part of a GPS track generated yesterday morning as i biked to work. If you have Google Earth, clicking on this link to the kml file should display the track in Earth, which is more interactive than this png.

Some of the apparently high-altitude cycling is real - the bridge is actually quite high. But it does seem that the altitude info from the GPS is pretty noisy and wierd. I also have the impression that the receiver is gluing me onto the street. I was actually on the bike path, and I definitely didnt go on autoroute Bonaventure (although i did go underneath it for a ways).

The data can also be converted to meters using the proj.4 tool, and the total distance calculated like so:
 awk 'NF>1 {print $2 ,$1 }' work_morning_cleaned.dat >work_ll.dat
proj +proj=utm work_ll.dat >work_utm.dat
to get:

....
-4558775.49 8004745.96
-4558785.90 8004720.21
-4558786.15 8004719.30
-4558787.05 8004716.93
-4558788.19 8004714.00
....
and then
 awk 'NR>1{dx=$1-lastx;dy=$2-lasty;dt=$4-lastt; dist=sqrt(dx*dx+dy*dy);  \
sum=sum+dist; } {lastx=$1;lasty=$2;lastt=$4}END {print "Total Distance:",sum}'\
work_data.dat
Total Distance: 27209.3
Twenty-seven km. That explains why my legs feel like jelly. Theres a few jumps and skips in the track and it appears to sometimes float off the ground, so it would seem that some more adjustments would be required before I would entirely trust this GPS track. But it does give me something to look at while my legs recover.