导出骑记(iriding.cc)轨迹

导出骑记轨迹, 保存成.gpx格式. 方便各类GPS党制作路书, 或者导入到别的平台.

  • 依赖: gpxpy

  • 用法:

    • ./iriding2gpx.py ROUTEID
    • ROUTEID 是每个轨迹的ID
      • 比如这个链接: http://iriding.cc/user/route/207130.shtml, route id 就是207130
  • 代码: iriding2gpx.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
#!/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)

Gist

最后贴上我的骑记连接: http://iriding.cc/user/detail/72753.shtml, 欢迎互粉^_^

留言