Skip to content

Instantly share code, notes, and snippets.

@grimpy
Created July 23, 2021 15:20
Show Gist options
  • Save grimpy/6bfb47e564b33a7cee67165a13ba9a91 to your computer and use it in GitHub Desktop.
Save grimpy/6bfb47e564b33a7cee67165a13ba9a91 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from xml.etree import ElementTree
from datetime import datetime, timedelta
def convert(filepath):
dom = ElementTree.parse(filepath)
header = dom.find("trackHeader")
startdate = datetime.strptime(header.find("TrackName").text, "%Y-%m-%d")
starttime = datetime.strptime(header.find("StartTime").text, "%H:%M:%S")
startdate = startdate.replace(hour=starttime.hour, minute=starttime.minute, second=starttime.second)
pointtime = startdate.replace(second=starttime.second)
gpx = ElementTree.Element("gpx")
gpx.attrib["version"] = "1.1"
trk = ElementTree.Element("trk")
name = ElementTree.Element("name")
name.text = str(startdate)
gpx.append(trk)
trk.append(name)
trkseg = ElementTree.Element("trkseg")
trk.append(trkseg)
print(startdate)
for point in dom.findall("trackLapPoints"):
lat = point.find("Latitude").text
long = point.find("Longitude").text
alt = point.find("Altitude").text
pointtime += timedelta(seconds=float(point.find("Interval_x0020_Time").text.replace(",", ".")))
point = ElementTree.Element("trkpt")
point.attrib["lat"] = lat
point.attrib["lon"] = long
ele = ElementTree.Element("ele")
ele.text = alt
point.append(ele)
time = ElementTree.Element("time")
time.text = pointtime.isoformat()
point.append(time)
trkseg.append(point)
print(ElementTree.tostring(gpx))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--file")
options = parser.parse_args()
convert(options.file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment