Skip to content

Instantly share code, notes, and snippets.

@rnewson
Last active Dec 31, 2020
Embed
What would you like to do?
Strava
Python script to insert missing trackpoints while paused
From https://support.strava.com/hc/en-us/articles/216919277-Auto-Pause:
"We will do server-side resting time calculations (per above)unless
there are manual pause events. If there are manual pause events, we
assume that you want to control the time and will calculate moving
time with respect to timer time instead."
This script detects these gaps and inserts trackpoints representing
you as stationary.
Also, from https://support.strava.com/hc/en-us/articles/115001188684-Moving-Time-Speed-and-Pace-Calculations:
"When uploading runs Strava relies on the recorded "timer time"
which is the time the app or device was unpaused to determine
moving time. As long as your device is recording conventional pauses,
Strava will respect any pauses recorded in the file whether your
device pauses automatically or you manually hit the pause button.
It's important to keep in mind is that if you choose to pause you
must do so consistently. If there are any pause events in your
activity file our server will not remove any additional resting
time. If you do not pause at all our server will calculate moving
time based on the recorded GPS data. Again, even though elapsed
time may be the same, Strava may calculate moving time differently
than other platforms."
#!/usr/bin/env python3
from lxml import objectify, etree
import sys
from dateutil.parser import parse as parse_date
from datetime import date, datetime, timedelta
from copy import deepcopy
def perdelta(start, end, delta):
curr = start + delta
while curr < end:
yield curr
curr += delta
namespace = 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2'
# Parse TCX file
tree = objectify.parse(sys.argv[1])
root = tree.getroot()
activity = root.Activities.Activity
# Insert missing trackpoints
for lap in activity.Lap:
prev_time = None
for tp in lap.Track.Trackpoint:
curr_time = parse_date(tp.Time.text)
if (prev_time):
for gap_time in perdelta(prev_time, curr_time, timedelta(seconds = 1)):
tmp = etree.Element('Trackpoint')
tmpt = etree.SubElement(tmp, 'Time')
tmpt.text = gap_time.strftime('%Y-%m-%dT%H:%M:%S.000Z')
tmp.append(deepcopy(tp.Position))
tmp.append(deepcopy(tp.AltitudeMeters))
tmp.append(deepcopy(tp.DistanceMeters))
tp.addprevious(tmp)
prev_time = curr_time
print('<?xml version="1.0" encoding="UTF-8"?>')
print(etree.tostring(root, pretty_print=True, encoding='unicode'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment