Loading GPX data and getting it ready to process in Mathematica

First the easy part: load the GPX file itself. We’re only interested in the Data subset of it so we initially limit it to just that.

rawdata = Import["/Users/gburgyan/Dropbox/Blog/AdventureMapping/Adventure.GPX", "Data"];

Next, we want to gather the information that we’re interested in. In this case I’m really interested in the Geometry (which is the raw data points {latitude,longitude}), the PointElevation (in meters), and the PointTimestamp (which is stored as the exact right input for AbsoluteTime)

joined = MapThread[ { #1, #2, AbsoluteTime[#3] } &,
   {Flatten[Lookup[rawdata, "Geometry"][[1, 1, 1]], 1], 
    Flatten[Lookup[Lookup[rawdata, "LabeledData"], "PointElevation"]],  
    Flatten[Lookup[Lookup[rawdata, "LabeledData"], "PointTimestamp"], 3]}];

This is where it gets trickier. We want to figure out the speed along with the time deltas between points. The difference between two AbsoluteTime objects is, conveniently, seconds. In a slightly annoying twist, the return of the GeoDistance, is a Quanity object that has units. Given where I am it’s defaulting to feet. The reason it’s annoying is that computations with united quanities is slow. What we do with all the contortions is just convert it to meters and be done with it.

result = MapThread[
   Join[#1, {#2, #3, #3/#2}] &, {
    joined,
    Prepend[Differences[joined[[All, 3]]], 1],
    Prepend[
     QuantityMagnitude[
        UnitConvert[GeoDistance[#[[1]], #[[2]]], "m"]] & 
      /@ Partition[joined[[All, 1]], 2, 1], 0]
    }];

Finally, I just write it out to a file so I don’t have to do this again. :-)

result >> "/Users/gburgyan/Dropbox/Blog/AdventureMapping/preproc.m"

Here’s the file that you can play with yourself: Processor.nb