Converting GPS data to distance-time data
June 14, 2012 12:02 PM

I have some .GPX/.CSV MyTracks GPS files that I would like to use to create distance-time graphs. I can do this using GPS Visualizer but this creates the graphs for me and what I want is the raw data. Is there a website, program or script (Linux or Windows) that will allow me to do this?

Taking the .CSV files as an example, I would like to go from:
"Segment","Point","Latitude (deg)","Longitude (deg)","Altitude (m)","Bearing (deg)","Accuracy (m)","Speed (m/s)","Time","Power (W)","Cadence (rpm)","Heart rate (bpm)","Battery level (%)"
to:
"Time","Distance"

An an example, one line of data looks like this:
"1","1","52.36733","-1.220571","154.0","127.6171875","32","35.75","2012-06-14T15:26:01.000Z",,,,
posted by alby to Computers & Internet (6 answers total) 1 user marked this as a favorite
I don't know of an existing thing, but it would be a straightforward script to write. Are the lines already in order by time?
posted by hattifattener at 12:10 PM on June 14, 2012


Are the lines already in order by time?

Yes.
posted by alby at 12:11 PM on June 14, 2012


If you're comfortable grubbing with python, then this script should do it— the distance calculation is pretty simplistic, but it should be within a percent or so:
import csv
from math import *

toradians = pi / 180.0
Rearth = 6371.0

def distance( (lat1, lon1), (lat2, lon2) ):
    return hypot( lat2-lat1, sin(lat2) * (lon2-lon1) ) * Rearth 

lastpos=None
total=0
for row in csv.reader(open('inputfile.csv'), dialect='excel'):
    thispos = ( toradians * float(row[2]), toradians * float(row[3]) )
    if lastpos is not None:
        total += distance( lastpos, thispos )
    lastpos = thispos
    print '"%s","%s"' % ( row[8], total )

posted by hattifattener at 12:42 PM on June 14, 2012


hattifattener, is that the Haversine formula? It doesn't look as compicated as Vincenty's and considering the small distances the not-a-perfect-sphereness of the Earth will make a negligible difference.
posted by alby at 12:49 PM on June 14, 2012


You can do this yourself, but doing it correctly is remarkably difficult. For instance, I believe hattifatterner's code snippet up there doesn't work well if the two fixes are a short distance apart. The Haversine formula for doing that as well as a bunch of the other basic map is described on this web page and you could use it to write your own code.

But there's easier ways. Have you tried just importing your GPX files into Runkeeper and using their analysis? Works great if you only need a few tracks (and in a pinch, you could use their API). You could also pull the data into a proper GIS application like QGIS to do the calculations.

I don't know of a great script for doing this kind of work; see my StackExchange question for some discussion and other tools.
posted by Nelson at 12:51 PM on June 14, 2012


Nope, my formula isn't even the Haversine formula— it's the naïvest possible linearization around a point, what Nelson's linked page calls the equirectangular approximation; as the page says, though, the errors from the formula are probably small compared to the errors from approximating the Earth as a sphere. If you want more accurate answers (and are still using Python instead of a dedicated GIS program) the easiest thing to do is probably to find and install a Python module that does better calculations.

Oh, and I wrote sin where I should have written cos, which probably misled you. D'oh.
posted by hattifattener at 12:15 AM on June 15, 2012


« Older Why are Korean dramas so obsessed with first love?...   |   Credits for clip art Newer »
This thread is closed to new comments.