-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertGpxToTcx.py
95 lines (67 loc) · 3.26 KB
/
ConvertGpxToTcx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# (c) 2018 Phillip Myburgh
# Distributed under the MIT Licence
import sys
import os
# To parse command line arguments
import argparse
from GpxLib import *
argumentParser = argparse.ArgumentParser(
description='Convert a .gpx file to a slope workout file (.tcx)')
argumentParser.add_argument('inputFilename', type=str, help='Input Filename')
argumentParser.add_argument('--apiKey', "-a", type=str, help='Google Maps API Key')
argumentParser.add_argument('--outputFilename', "-o", type=str, help='Output Filename (defaults to <inputFilename>.tcx')
argumentParser.add_argument('--interpolationResolution', "-r", type=int, help='Resolution of the interpolation in meter (defaults to 100m)')
argumentParser.add_argument('--startDistance', "-start", type=int, help='Trims everything before the start distance (default 0m)')
argumentParser.add_argument('--stopDistance', "-stop", type=int, help='Trims everything after the stop distance')
argumentParser.add_argument('--plot', "-p", action='store_true', help='Flag without a value to enable plotting')
commandlineArguments = argumentParser.parse_args()
inputFilename = commandlineArguments.inputFilename
outputFilename = commandlineArguments.outputFilename
apiKey = commandlineArguments.apiKey
interpolationResolution = commandlineArguments.interpolationResolution
startDistance = commandlineArguments.startDistance
stopDistance = commandlineArguments.stopDistance
plot = commandlineArguments.plot
inputFilenameNoExtension = os.path.splitext(inputFilename)[0]
# if no output filename is given, we use the input filename
if outputFilename is None:
outputFilename = inputFilenameNoExtension
# Strip the extension of the output filename, we only need the name
outputName = outputFilename.replace(".tcx", "")
# If the interpolation resolution was left out, set it to 100m
if interpolationResolution is None:
interpolationResolution = 100
if startDistance is None:
startDistance = 0
if stopDistance is None:
stopDistance = 0
print "Parsing ", inputFilename, "..."
gpxCourse = ParseGpxFile(inputFilename)
print "There are " + str(gpxCourse.GetNumberOfPoints()) + " gps points..."
print "Interpolating..."
gpxCourse = gpxCourse.InterpolateToGivenResolution(interpolationResolution)
if startDistance > 0 or stopDistance > 0:
print "Trimming..."
if stopDistance == 0:
stopDistance = sys.maxint
gpxCourse = gpxCourse.PruneDistance(stopDistance, startDistance)
print "There are " + str(gpxCourse.GetNumberOfPoints()) + " gps points after interpolation..."
if apiKey:
print "Getting all the elevation data from google..."
gpxCourse = gpxCourse.CorrectElevation(apiKey)
profile = gpxCourse.CreateEquidistantProfile(interpolationResolution)
if profile.GetElevationGain() == 0 and apiKey is None:
print "Elevation data seems to be missing. You would need an API key to retrieve this info from the internet."
if plot:
PlotProfile(profile, 0, "Course Profile")
slopeCourse = profile.CreateSlopeCourse()
if plot:
PlotSlope(slopeCourse, 0, "Course Slope")
# Remove the duplicates etc...
slopeCourse = slopeCourse.Compress()
print "Building output..."
GenerateTcxSlopeWorkout(slopeCourse, outputName)
print "Done!!!"
# Only wait for user input if plots are enabled
if plot:
raw_input("Press Enter to continue...")