Skip to content

Instantly share code, notes, and snippets.

@oneyoung
Created April 25, 2014 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oneyoung/11276837 to your computer and use it in GitHub Desktop.
Save oneyoung/11276837 to your computer and use it in GitHub Desktop.
Export iriding.cc route to .gpx file. Usage: python2 iriding2gpx.py ROUTE_ID
#!/usr/bin/env python2
import urllib
import json
def fetch_route(routeid):
''' fetch route detail from iriding.cc by routeid
return a list of record consist as below:
[
{
altitude: 442
distance: 0
id: 94046284
latitude: 30.429626993119694
longitude: 119.50979758485511
record_time: "2014-04-06 07:29:12.0"
route_id: 166132
speed: 7.7784157
},
{...},
...
]
'''
url = "http://iriding.cc/manage/route/routeDetailData.shtml"
post_data = urllib.urlencode({'id': routeid})
resp = urllib.urlopen(url, data=post_data).read()
j = json.loads(resp)
data = j.get('data')
return data
def route2gpx(route, fpath):
''' save a route list of points to gpx file format '''
import gpxpy.gpx
import datetime
# create gpx object
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
timeformat = "%Y-%m-%d %H:%M:%S.%f"
strptime = datetime.datetime.strptime
gpx_segment.points = [gpxpy.gpx.GPXTrackPoint(latitude=p['latitude'],
longitude=p['longitude'],
elevation=p['altitude'],
time=strptime(p['record_time'], timeformat),
speed=p['speed'])
for p in route]
# save to file
print 'saving to %s' % fpath
f = open(fpath, 'w')
f.write(gpx.to_xml())
f.close()
if __name__ == '__main__':
import sys
routeid = sys.argv[-1]
try:
int(routeid) # valid route id
except:
print 'Usage: %s ROUTE_ID' % __file__
exit(-1)
path = routeid + '.gpx'
route2gpx(fetch_route(routeid), path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment