Last active
May 9, 2020 21:10
-
-
Save d3d9/24c1388cbdb5944e8390675552ccd448 to your computer and use it in GitHub Desktop.
GPX or TCX to WKT (MULTILINESTRING M)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
from csv import writer | |
from datetime import datetime, timezone | |
from sys import argv | |
def parsefile(filename: str): | |
root = ET.parse(filename).getroot() | |
#wkt = "LINESTRING M (" | |
wkt = "MULTILINESTRING M ((" | |
_xym = "" | |
for _, pt in enumerate(root.findall('g:trk/g:trkseg/g:trkpt', {'g': 'http://www.topografix.com/GPX/1/1'})): | |
lon, lat = pt.attrib['lon'], pt.attrib['lat'] | |
_ts = pt.findtext('{http://www.topografix.com/GPX/1/1}time') | |
try: | |
_dt = datetime.strptime(_ts, '%Y-%m-%dT%H:%M:%S.%fZ') | |
except ValueError: | |
_dt = datetime.strptime(_ts, '%Y-%m-%dT%H:%M:%SZ') | |
timestamp = int(_dt.replace(tzinfo=timezone.utc).timestamp()) | |
if not _: | |
# starttime = timestamp | |
offset = timestamp | |
timestamp -= offset | |
endtime = timestamp | |
_xym += f"{lon} {lat} {timestamp}, " | |
wkt += _xym[:-2] + "))" | |
# return wkt, starttime, endtime | |
return wkt, 0, endtime | |
rows = [("filename", "wkt", "starttime", "endtime")] | |
for filename in argv[1:]: | |
rows.append((filename, *parsefile(filename))) | |
with open("./gpx-wkt-out.csv", 'w', encoding='utf-8') as outfile: | |
writer(outfile, delimiter=';', lineterminator='\n').writerows(rows) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
from csv import writer | |
from datetime import datetime, timezone | |
from sys import argv | |
def parsefile(filename: str): | |
root = ET.parse(filename).getroot() | |
#wkt = "LINESTRING M (" | |
wkt = "MULTILINESTRING M (" | |
offset = None | |
hb = None | |
for _ai, a in enumerate(root.findall('t:Activities/t:Activity', {'t': "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"})): | |
for _li, l in enumerate(a.findall('t:Lap', {'t': "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"})): | |
_xym = "" | |
for _pti, pt in enumerate(l.findall('t:Track/t:Trackpoint', {'t': "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"})): | |
_hb = pt.findtext('t:HeartRateBpm/t:Value', namespaces={'t': "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"}) | |
if _hb is not None: | |
hb = _hb | |
pos = pt.find('t:Position', {'t': "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"}) | |
if pos is None: | |
continue | |
timestamp = int(datetime.strptime(pt.findtext('{http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2}Time'), '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=timezone.utc).timestamp()) | |
lat = pos.findtext("t:LatitudeDegrees", namespaces={'t': "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"}) | |
lon = pos.findtext("t:LongitudeDegrees", namespaces={'t': "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"}) | |
if offset is None: | |
# starttime = timestamp | |
offset = timestamp | |
timestamp -= offset | |
endtime = timestamp | |
out = timestamp | |
# out = hb | |
_xym += f"{lon} {lat} {out}, " | |
if _xym: | |
wkt += f"({_xym[:-2]}), " | |
wkt = wkt[:-2] + ")" | |
# return wkt, starttime, endtime | |
return wkt, 0, endtime | |
rows = [("filename", "wkt", "starttime", "endtime")] | |
for filename in argv[1:]: | |
rows.append((filename, *parsefile(filename))) | |
with open("./tcx-wkt-out.csv", 'w', encoding='utf-8') as outfile: | |
writer(outfile, delimiter=';', lineterminator='\n').writerows(rows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment