我正在寻找可以使用GPS坐标的Python库。比方说,我有一个坐标列表:
>>> gps = [
... (53.25012925, −6.24479338, 349.9, '2011-08-20T09:35:00Z'),
... (53.25028285, -6.24441800, 359.9, '2011-08-20T09:35:30Z'),
... (53.25049500, -6.24266032, 395.9, '2011-08-20T09:36:00Z'),
... # and so on...
... ]
>>>
我想计算平均速度,距离,获取最高点以及其他信息。我知道,计算它很简单,但我想知道是否有任何现有的代码(我不喜欢重新发明轮子)。
注意:stackoverflow(Which gps library would you recommend for python?)中也有类似的问题,但它与GPSD有关。我没有使用任何设备,我只是在文本文件中有GPS坐标。
答案 0 :(得分:3)
您仍然可以使用GPSD的数据部分,而不是从头开始编写内容。以下代码来自GPSD源,并有一个模块,用于从GPS数据流中制作路径(然后获取路径长度等等)
http://code.google.com/p/python-gpsd/source/browse/src/nmea/track.py
class Track(object):
def __init__(self, recordDelay=10, maxSize=3600, ignoreDuplicates=True, duplicateRange=0.0001):
""" Constructor
The default values allow for 10 hours worth of data recorded
at 10 second intervals.
recordDelay - Delay between recording data points
maxSize - Maximum number of points to record
ignoreDuplicates - Ignore entries that are very similar (ie moved a minimal distance)
duplicateRange - Varience range within a duplicate is detected (adjust to account for
subtle gps position drift)
"""
self.recordDelay = recordDelay
self.maxSize = maxSize
self.ignoreDuplicates = ignoreDuplicates
self.duplicateRange = duplicateRange
self.positions = []
self.latest = None
def append(self, position, heading, timeStamp=None):
""" Append position and heading information """
if timeStamp is None: timeStamp = datetime.utcnow()
self.latest = (timeStamp, position, heading)
if len(self.positions):
last = self.positions[0]
else:
last = None
# Make sure we re in range
if last is None or (timeStamp - last[0]).seconds >= self.recordDelay:
self.positions.insert(0, self.latest)
self.latest = None
# Clear extra data
if len(self.positions) > self.maxSize: pass
def clear(self):
""" Clear all items from track """
self.positions = []
self.latest = None
def __len__(self):
""" Return the length of the track """
if self.latest is None: return len(self.positions)
return len(self.positions) + 1
def __getslice__(self, i, j):
return self.positions[i:j]
def __getitem__(self, i):
return self.positions[i]
def get_latest(self):
if self.latest is None and len(self.positions) > 0:
return self.positions
def get_by_time(self, timeRange, now=datetime.utcnow()):
""" Returns the last n items within the time range """
result = []
if self.latest is not None: result.append(self.latest)
for position in self.positions:
if (now - position[0]).seconds > timeRange: break
result.append(position)
return result
答案 1 :(得分:2)
我找到了一个名为geopy的有趣库。它可以计算两个GPS点之间的距离(它使用大圆距离和Vincenty距离方法)。最重要的是, geopy 可以进行地理编码(它可以从地址获取GPS坐标)。
其他功能(平均速度,最高点等)我可以自己破解。