Created
January 11, 2015 14:39
-
-
Save weiznich/f940282fe1ff8e114595 to your computer and use it in GitHub Desktop.
Simple script to convert the fit files of a garmin forerunner to gpx files.
This file contains 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
#!/usr/bin/env python | |
from fitparse import FitFile | |
import srtm | |
import xml.dom.minidom as minidom | |
import moment | |
import sys | |
import xml.etree.cElementTree as ET | |
def prettify(elem): | |
"""Return a pretty-printed XML string for the Element. | |
""" | |
rough_string = ET.tostring(elem, 'utf-8') | |
reparsed = minidom.parseString(rough_string) | |
return reparsed.toprettyxml(indent="\t") | |
def print_record(record_data): | |
if record_data.units: | |
print " * %s: %s %s" % ( | |
record_data.name, record_data.value, record_data.units, | |
) | |
else: | |
print " * %s: %s" % (record_data.name, record_data.value) | |
if len(sys.argv) != 3: | |
print sys.argv[0]+" filename outputpath" | |
sys.exit() | |
fitfile = FitFile(sys.argv[1]) | |
factor =(180/pow(2.0,31)) | |
elevation_data = srtm.get_data() | |
output_file="" | |
root = ET.Element("gpx") | |
root.set("xmlns","http://www.topografix.com/GPX/1/1") | |
root.set("version","1.1") | |
track = ET.SubElement(root, "trk") | |
segment=ET.SubElement(track, "trkseg") | |
for record in fitfile.get_messages('record'): | |
longtitude=-999 | |
latitude=-999 | |
heart_rate=-999 | |
speed=-999 | |
timestamp="" | |
elevation=-999 | |
for record_data in record: | |
if record_data.name=="timestamp": | |
timestamp=record_data.value | |
continue | |
if not (type(record_data.value) is int or type(record_data.value) is float): | |
if not record_data.name=="cadence": | |
print_record(record_data) | |
continue | |
if record_data.name == "position_lat": | |
latitude=record_data.value*factor | |
continue | |
if record_data.name=="position_long": | |
longtitude=record_data.value*factor | |
continue | |
if record_data.name=="heart_rate": | |
heart_rate=record_data.value | |
continue | |
if record_data.name=="speed": | |
speed=record_data.value | |
continue | |
if record_data.name=="altitude": | |
elevation=record_data.value | |
continue | |
if record_data.name=="distance": | |
continue | |
print_record(record_data) | |
if not( longtitude==-999 or latitude==-999): | |
point=ET.SubElement(segment, "trkpt") | |
point.set("lat",str(latitude)) | |
point.set("lon",str(longtitude)) | |
if not heart_rate==-999: | |
ext=ET.SubElement(point,"extensions") | |
bpm=ET.SubElement(ext,"heartrate") | |
bpm.text=str(heart_rate) | |
if not speed==-999: | |
s=ET.SubElement(point, "speed") | |
s.text=str(speed) | |
altitude=ET.SubElement(point, "ele") | |
if not elevation==-999: | |
altitude.text=str(elevation) | |
else: | |
altitude.text=str(elevation_data.get_elevation(latitude, longtitude)) | |
if timestamp: | |
m = moment.date(str(timestamp), "%Y-%m-%d %H:%M:%S") | |
t=ET.SubElement(point, "time") | |
t.text=str(m.format("YYYY-M-DTH:m:sZ")) | |
output_file=str(m.format("YYYY-M-DTH:m:sZ")) | |
output_file=sys.argv[2]+"/"+output_file+".gpx" | |
f = open(output_file, 'w+') | |
f.write(prettify(root)) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment