You are reading a single comment by @NotThamesWater and its replies. Click here to read the full conversation.
  • @TW As discussed elsewhere (Running thread) Strava sometimes thinks you've stopped mid run when you haven't, and then calculates your pace based off this incorrect "moving time". It's due to the uploaded file containing trackpoints that don't have position information in them.

    If you've got a .fit file you'll need to convert it to .tcx first (either import it to something like GC that can export it as TCX) or use a tool like fit2tcx.

    OK, UNIX (should work with cygwin) command line to remove trackpoints from a TCX file (from fit2tcx) that don't contain position information:-

    cat in.tcx | sed -e 's/^  *//' -e 's/  *$//' | tr -d '\n' | sed -e 's/<\/Trackpoint>/&\n/g' -e 's/<Trackpoint>/\n&/g' | grep -v "^$" | grep -v "Time..Alti" > out.tcx
    

    This assumes that the correct lines don't have the Altitude tag following on from the closing Time tag, i.e.

    Example OK line:-

    <Trackpoint><Time>2015-01-21T12:48:55Z</Time><Position><LatitudeDegrees>51.4546127</LatitudeDegrees><LongitudeDegrees>-0.2336897235</LongitudeDegrees></Position><AltitudeMeters>50.2</AltitudeMeters><DistanceMeters>890.66</DistanceMeters><HeartRateBpm><Value>168</Value></HeartRateBpm><Extensions><TPX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2"><Speed>3.407</Speed></TPX></Extensions></Trackpoint>
    

    Bad line (no position tags):-

    <Trackpoint><Time>2015-01-21T12:48:48Z</Time><AltitudeMeters>51</AltitudeMeters><DistanceMeters>872.7</DistanceMeters><HeartRateBpm><Value>170</Value></HeartRateBpm><Extensions><TPX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2"><Speed>2.802</Speed></TPX></Extensions></Trackpoint>
    

    You may need to tweak the grep -v argument to something that only matches the lines that don't have position information in them.

    I'll follow up with a perl script to do the same (and rip out best distance segments) which will be a bit more robust, optionally do interpolation to add position tags on the trackpoints missing them, and won't rely so much on it being a specific xml format. I can't be arsed to use an XML parser so it's all hand hacked.

  • One line bash command. Like.

    Thanks for this.

    My regex is terrible - what dies this bit cover: /^ */

    Do you have one that automatically formats / indents xml files? tcx / gpx files on one line give me a headache.

  • 's/^ *//' replaces any spaces at the beginning of the line with nothing, essentially stripping off leading whitespace.

    ^ matches the beginning of the line
    the name space character matches at least one space
    space then kleene star (*) matches any number (including 0) spaces
    replace it with nothing.

    's/^ +//' would be cleaner (match beginning of line and then 1 or more spaces) but I've been bitten by lack of + support in the dim past and so I naturally avoid it.

    As for indentation, something like xmllint will do a better job, but for hackiness you could also just do something along the lines of:-

    cat a.gpx | tr -d '\n' | sed -e 's/> *</>\n</g' | perl -e '$in=0; while( <> ) { if( /<[^\/].*<\// ) { print " "x$i.$_; } elsif( /<[^\/]/ ) { print " "x$i.$_; $i++; } elsif( /<\// ) { $i--; print " "x$i.$_; } }'
    

    The sed separates tags onto a line by themselves and the perl does some rudimentary indentation.

    Expect the above to blow up (especially for strings that contain embedded > or < characters). It's 5 minutes' work whilst distracted on a conference call.

About