Skip to content

Instantly share code, notes, and snippets.

@zonque
Created May 5, 2024 09:03
Show Gist options
  • Save zonque/d6f8772a648c0cd509a0bd4278b24049 to your computer and use it in GitHub Desktop.
Save zonque/d6f8772a648c0cd509a0bd4278b24049 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import xml.etree.ElementTree as ET
from dateutil import parser
def fixup_trackpoint_callback(trackpoint):
timeElement = trackpoint.find("{http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2}Time")
distanceElement = trackpoint.find("{http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2}DistanceMeters")
distance = float(distanceElement.text)
if parser.parse(timeElement.text) >= parser.parse("2024-05-04T07:44:49Z"):
distance -= 12604.8
distance += 50777.6
distance += 2200.0
distanceElement.text = str(distance)
return trackpoint
def process_trackpoints(xml_file):
tree = ET.parse(xml_file)
root = tree.getroot()
for trackpoint in tree.iter("{http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2}Trackpoint"):
fixup_trackpoint_callback(trackpoint)
return ET.tostring(root, encoding='utf-8', method='xml').decode('utf-8')
if __name__ == '__main__':
input_xml_path = 'merged.tcx'
modified_xml = process_trackpoints(input_xml_path)
with open('output.tcx', 'w', encoding='utf-8') as output_file:
output_file.write(modified_xml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment