Skip to content

Commit

Permalink
Modify GPX trans to support both of track and route
Browse files Browse the repository at this point in the history
  • Loading branch information
iascchen authored and liangqi committed Oct 8, 2013
1 parent 7d2d257 commit 28f4af7
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 21 deletions.
69 changes: 50 additions & 19 deletions devices/codoon.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ def justifyCityOffset(self , lat ,lon):

return realoffset[1]

def trans(self , route ):
'''
Type = "track" or "route"
'''
def trans(self , route , type = "track"):
name = route["data"]["start_time"]
points = route["data"]["points"]

# Calculate offset of start point
Expand All @@ -251,23 +255,47 @@ def trans(self , route ):
print realoffset

gpx = gpxpy.gpx.GPX()
# Create route in our GPX:
gpx_route = gpxpy.gpx.GPXRoute()

i = 1
for p in points:
tmpname = "#%5d" % i
tmptime = strptime( p["time_stamp"] , DATE_FORMAT )
lat = float(p["latitude"]) + float(realoffset[0])
lon = float(p["longitude"]) + float(realoffset[1])

gpx_point = gpxpy.gpx.GPXRoutePoint( name = tmpname , longitude = lon , latitude = lat ,
elevation = p["elevation"] , time = tmptime )
# print gpx_point
gpx_route.points.append( gpx_point )
i = i + 1

gpx.routes.append(gpx_route)
if type == "route":
# Create route in GPX Route Format:
rtname = "Route %s" % name
gpx_route = gpxpy.gpx.GPXRoute(name = rtname)

i = 1
for p in points:
tmpname = "#%5d" % i
tmptime = strptime( p["time_stamp"] , DATE_FORMAT )
lat = float(p["latitude"]) + float(realoffset[0])
lon = float(p["longitude"]) + float(realoffset[1])

gpx_point = gpxpy.gpx.GPXRoutePoint( name = tmpname , longitude = lon , latitude = lat ,
elevation = p["elevation"] , time = tmptime )
# print gpx_point
gpx_route.points.append( gpx_point )
i = i + 1

gpx.routes.append(gpx_route)
else:
# Create route in GPX Track Format:
trkname = "Track %s" % name
gpx_track = gpxpy.gpx.GPXTrack(name = trkname)
gpx_track_seg = gpxpy.gpx.GPXTrackSegment()

i = 1
for p in points:
tmpname = "#%5d" % i
tmptime = strptime( p["time_stamp"] , DATE_FORMAT )
lat = float(p["latitude"]) + float(realoffset[0])
lon = float(p["longitude"]) + float(realoffset[1])

gpx_point = gpxpy.gpx.GPXTrackPoint( name = tmpname , longitude = lon , latitude = lat ,
elevation = p["elevation"] , time = tmptime )
# print gpx_point
gpx_track_seg.points.append( gpx_point )
i = i + 1

gpx_track.segments.append(gpx_track_seg)
gpx.tracks.append(gpx_track)

# print 'Created GPX:', gpx.to_xml()
return gpx.to_xml()
Expand All @@ -286,6 +314,7 @@ def trans(self , route ):

# login
logined = device.get_users_login(account["email"], account["passwd"])
'''
print device.auth_info
device.saveJsonData( filename = "/users_token.json" , data = logined)
Expand Down Expand Up @@ -361,12 +390,14 @@ def trans(self , route ):
device.get_misc_mobile( )
wwwStatistic = device.get_user_statistic( )
device.saveJsonData( filename = "/user_statistic.json" , data = wwwStatistic)

'''
# Trans Codoon GPS Data to GPX format
routeId = "03e1cd1e-07b1-11e3-b50f-00163e020001"
trans = CodoonRoute2Gpx()
trans.loadCityOffset()
route = device.get_single_log( routeId = routeId )
device.saveJsonData( filename = "/single_log_20130817.json" , data = route)
gtx = trans.trans( route = route , type = "route" )
device.saveXmlData( filename = "/single_log_20130817_route.gpx" , data = gtx)
gtx = trans.trans( route = route )
device.saveXmlData( filename = "/single_log_20130817.gpx" , data = gtx)
device.saveXmlData( filename = "/single_log_20130817_track.gpx" , data = gtx)
78 changes: 76 additions & 2 deletions docs/codoon-route-to-gpx.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

取得的运动轨迹数据可以参考示例文件: [single_log_20130817.json](single_log_20130817.json)。这个轨迹是跟着绿野去长峪城腐败的轨迹。:D,摆两张照片放放毒。



### GPX文件输出 ###

Python 中有直接写 GPX 格式文件的包: [gpxpy](https://github.com/tkrajina/gpxpy) 。 用来创建 GPX 文件非常容易。函数中的参数 route 是 get\_single\_log 的结果。不过在使用中,如果将时间数据写入 GPX 文件时,gpxpy有个小Bug,需要对时间格式处理的部分做个小调整。
Expand Down Expand Up @@ -195,6 +197,76 @@ gpxpy Bug 修正, gpx.py 文件

![](imgs/gpx_noshift.png)

### Track 和 Route ###

新浪微博网友 @齐亮-Cavendish 发现如上文生成的 GPX 文件,没法导入 heiaheia.com , 经过检查,发现 heiaheia 只能够导入 Track 格式的 GPX 文件。

因此修改 GPX 输出代码如下,增加参数对导出格式的限定。

# Type 可以取值为 "track" or "route", 缺省为 "track"
def trans(self , route , type = "track"):
name = route["data"]["start_time"]
points = route["data"]["points"]

# Calculate offset of start point
lat = points[0]["latitude"]
lon = points[0]["longitude"]

realoffset = self.justifyCityOffset( float(lat) ,float(lon) )
print realoffset

gpx = gpxpy.gpx.GPX()

if type == "route":
# Create route in GPX Route Format:
rtname = "Route %s" % name
gpx_route = gpxpy.gpx.GPXRoute(name = rtname)

i = 1
for p in points:
tmpname = "#%5d" % i
tmptime = strptime( p["time_stamp"] , DATE_FORMAT )
lat = float(p["latitude"]) + float(realoffset[0])
lon = float(p["longitude"]) + float(realoffset[1])

gpx_point = gpxpy.gpx.GPXRoutePoint( name = tmpname , longitude = lon , latitude = lat ,
elevation = p["elevation"] , time = tmptime )
# print gpx_point
gpx_route.points.append( gpx_point )
i = i + 1

gpx.routes.append(gpx_route)
else:
# Create route in GPX Track Format:
trkname = "Track %s" % name
gpx_track = gpxpy.gpx.GPXTrack(name = trkname)
gpx_track_seg = gpxpy.gpx.GPXTrackSegment()

i = 1
for p in points:
tmpname = "#%5d" % i
tmptime = strptime( p["time_stamp"] , DATE_FORMAT )
lat = float(p["latitude"]) + float(realoffset[0])
lon = float(p["longitude"]) + float(realoffset[1])

gpx_point = gpxpy.gpx.GPXTrackPoint( name = tmpname , longitude = lon , latitude = lat ,
elevation = p["elevation"] , time = tmptime )
# print gpx_point
gpx_track_seg.points.append( gpx_point )
i = i + 1

gpx_track.segments.append(gpx_track_seg)
gpx.tracks.append(gpx_track)

# print 'Created GPX:', gpx.to_xml()
return gpx.to_xml()

使用 Track 格式的 GPX 文件,能够将数据导入 Heiaheia 网站,能够正确显示轨迹、里程、以及时长。

但是,还有一个问题,文件中Track记录为8月份的数据,并没有被自动放到8月份去,而是放在了当前 Walk 创建时所设定的日期:10月8日。

![](imgs/heiaheia.png)

### 调用 ###

account = { "email" : "your@email" , "passwd" : "yourpassword" }
Expand All @@ -215,8 +287,10 @@ gpxpy Bug 修正, gpx.py 文件
route = device.get_single_log( routeId = routeId )
device.saveJsonData( filename = "/single_log_20130817.json" , data = route)

gtx = trans.trans( route = route , type = "route" )
device.saveXmlData( filename = "/single_log_20130817_route.gpx" , data = gtx)
gtx = trans.trans( route = route )
device.saveXmlData( filename = "/single_log_20130817.gpx" , data = gtx)
device.saveXmlData( filename = "/single_log_20130817_track.gpx" , data = gtx)

---
## 代码地址 ##
Expand All @@ -233,7 +307,7 @@ gpxpy Bug 修正, gpx.py 文件

Author : iascchen(at)gmail(dot)com

Date : 2013-09-06
Date : 2013-10-08

新浪微博 : [@问天鼓](http://www.weibo.com/iascchen)

Expand Down
Binary file added docs/imgs/heiaheia.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 28f4af7

Please sign in to comment.