Motorcycle riding leads to coding

Got a quick ride in today — 112 miles. Quickly coming up on the 1K mile mark on the odometer.

First time taking the new bike up highway 9. Must have had half a dozen riders signal to warn that the police were up ahead; this part of the ride was none too spirited.

152 from Watsonville over Hecker pass was loads of fun, even if the road was a bit rough in spots. After the left turn northward toward, Uvas road was also very interesting. I truly enjoy the ride up these “hidden” valley roads that most folks don’t realize exist so close to their suburbs.


Got tired of hand-editing the GPX files to merge the routes. Started to write a sed script to do it for me, but, just had all kinds of trouble trying to get the version of sed that ships with Mac OS X to insert a newline — which a quick search of ‘sed newline’ reveals that perhaps sed doesn’t much like to do this any case. So, scratched some rust off my once budding Python and decided to learn how to do a regular expression with it.

And for the first time with Python, it underwhelmed me. The simplicity of a regex is burdened by the fact that (I only now realized) Python doesn’t have any in-built regex abilities; I just assumed it did, and it would work like Perl. It most certainly does not:

#! /usr/bin/python
import sys
import re
if len(sys.argv) < 3:
  print "usage:"
  print "  gpx.py <gpx input file> <output file name>"
  sys.exit(1)
f_read  = open(sys.argv[1], 'r')
f_write = open(sys.argv[2], 'w')

for line in f_read:
  p = re.compile('<wpt.*/wpt>')
  line = p.sub('',line)
  p = re.compile('<trk')
  line = p.sub('\n<trk',line)
  p = re.compile('</trkseg')
  line = p.sub('\n</trkseg',line)
  p = re.compile('<time>.*</time>')
  line = p.sub('',line)
  p = re.compile('<trk.*37\.3[0,1,2].*-121.8[8,9].*/trkpt>\n')
  [line,count] = p.subn('',line)
  print "Removed", count," points from the track"
  f_write.write(line)
f_read.close()
f_write.close()
sys.exit(0)

Which does the following:

  1. Removes all waypoints from the file.
  2. Adds newlines in front of some key xml tags which makes it trivial to merge multiple tracks together using a text editor
  3. Removes the timestamp from each track data point
  4. Removes any data point that are near my house

All simple stuff, but tedious to do on the XML source file that has no line feeds as generated from the Garmin Nuvi GPS.

With the munged GPX file, this is uploaded to the very handy GPSVisualizer.com site that dumps the track into the map you see above.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>