Skip to content

Instantly share code, notes, and snippets.

@miparnisari
Created May 23, 2021 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miparnisari/f1ac72eb241f30f465fc0ac6b2cc6475 to your computer and use it in GitHub Desktop.
Save miparnisari/f1ac72eb241f30f465fc0ac6b2cc6475 to your computer and use it in GitHub Desktop.
Fix distances set to 0 in TCX using https://www.plotaroute.com
from xml.dom import minidom
import xml.etree.ElementTree as ET
import math
import decimal
# send the original TCX file to https://www.plotaroute.com/ and export it to get the right distances
fixed_doc = minidom.parse('fromplotaroute.tcx')
distances = []
for fixedElem in fixed_doc.getElementsByTagName('Trackpoint'):
pos = fixedElem.getElementsByTagName('Position')
latitude = pos[0].getElementsByTagName('LatitudeDegrees')[0].firstChild.data
longitude = pos[0].getElementsByTagName('LongitudeDegrees')[0].firstChild.data
distance = fixedElem.getElementsByTagName('DistanceMeters')[0].firstChild.data
elem = {
'lat': latitude,
'long': longitude,
'dist': distance
}
distances.append(elem)
# this is the original file created by Fitbit
original_doc = minidom.parse('fitbit.tcx')
i = 0
for elem in original_doc.getElementsByTagName('Trackpoint'):
if (i >= len(distances)):
elem.getElementsByTagName('DistanceMeters')[0].firstChild.data = 0
continue
pos = elem.getElementsByTagName('Position')
latitude = pos[0].getElementsByTagName('LatitudeDegrees')[0].firstChild.data
longitude = pos[0].getElementsByTagName('LongitudeDegrees')[0].firstChild.data
curr_distance = distances[i]
a = decimal.Decimal(curr_distance['lat'])
decimal_places_a = a.as_tuple().exponent
relative_tolerance_a = 10 ** decimal_places_a
b = decimal.Decimal(curr_distance['long'])
decimal_places_b = b.as_tuple().exponent
relative_tolerance_b = 10 ** decimal_places_b
if (math.isclose(a,float(latitude),rel_tol=relative_tolerance_a) and math.isclose(b, float(longitude),rel_tol=relative_tolerance_b)):
elem.getElementsByTagName('DistanceMeters')[0].firstChild.data = curr_distance['dist']
i += 1
else:
elem.getElementsByTagName('DistanceMeters')[0].firstChild.data = 0
# write results
with open("fixed.tcx", "w") as xml_file:
original_doc.writexml(xml_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment