Created
August 6, 2016 05:56
-
-
Save rcoh/ffe530cac090a3e87f3578a399e98889 to your computer and use it in GitHub Desktop.
Strava JSON to GPX converter
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
### pip install gpxpy | |
import sys | |
import gpxpy | |
import gpxpy.gpx | |
import json | |
import datetime | |
def convert(f): | |
gpx = gpxpy.gpx.GPX() | |
# Create first track in our GPX: | |
gpx_track = gpxpy.gpx.GPXTrack() | |
gpx.tracks.append(gpx_track) | |
# Create first segment in our GPX track: | |
gpx_segment = gpxpy.gpx.GPXTrackSegment() | |
gpx_track.segments.append(gpx_segment) | |
# Create points: | |
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(2.1234, 5.1234, elevation=1234)) | |
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(2.1235, 5.1235, elevation=1235)) | |
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(2.1236, 5.1236, elevation=1236)) | |
# You can add routes and waypoints, too... | |
with open(f) as json_data: | |
strava_json = json.load(json_data) | |
path_data = strava_json["data"] | |
for dp in path_data[0]["values"]: | |
time = dp[0] | |
pt = dp[1] | |
elevation = dp[2] | |
date = datetime.datetime.fromtimestamp(time) | |
point = gpxpy.gpx.GPXTrackPoint(latitude = pt[0], longitude = pt[1], time = date, elevation = elevation) | |
gpx_segment.points.append(point) | |
gpx_str = gpx.to_xml() | |
with open(f + ".gpx", "w") as target: | |
target.write(gpx_str) | |
print "Done!" | |
if __name__ == "__main__": | |
convert(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment