我正试图解决一个奇怪的问题。也许你们知道一些算法可以解决这个问题。
我有货运卡车的数据,想要提取一些数据。假设我有一个从GPS获得的分类点列表。那是那辆卡车的路线:
[
{
"lng": "-111.5373066",
"lat": "40.7231711",
"time": "1970-01-01T00:00:04Z",
"elev": "1942.1789265256325"
},
{
"lng": "-111.5372056",
"lat": "40.7228762",
"time": "1970-01-01T00:00:07Z",
"elev": "1942.109892409177"
}
]
现在,我想得到的是“最快的里程”列表。我会做一个例子:
鉴于要点:
A, B, C, D, E, F
从A点到B点的距离是1英里,货物需要10:32分钟。从B点到D点,我有另一英里,货物需要10分钟等等。所以,我需要一个按时间排序的清单。类似于:
B -> D: 10
A -> B: 10:32
D -> F: 11:02
你知道任何有效的算法让我计算出来吗?
谢谢大家。
PS:我正在使用Python。
修改
我有距离。我知道如何计算它并且有很多帖子可以做到这一点。我需要的是一种算法,按英里标记并从中获得速度。有一个距离函数是没有用的:
results = {}
for point in points:
aux_points = points.takeWhile(point>n) #This doesn't exist, just trying to be simple
for aux_point in aux_points:
d = distance(point, aux_point)
if d == 1_MILE:
time_elapsed = time(point, aux_point)
results[time_elapsed] = (point, aux_point)
我还在做一些非常低效的计算。
答案 0 :(得分:1)
如果您有获取位置数据的位置和时间戳,您可以执行以下操作:
def CalculateSpeeds(list_of_points_in_time_order):
"""Calculate a list of (average) speeds for a list of geographic points."""
points = list_of_points_in_time_order
segment_start = points[0]
speed_list = []
for segment_end in points[1:]:
dt = ElapsedTime(segment_start, segment_end)
# If you're looking at skipping points, with a slight risk of degraded data
# you could do something like "if dt < MIN_ELAPSED_TIME:" and indent
# the rest of the loop. However, you'd need to then check if the last point
# has been accounted for, as it might've been too close to the last considered
# point.
d = Distance(segment_start, segment_end)
speed_list.append(d/dt)
segment_start = segment_end
return speed_list
你已经说过(在评论中)你可以为一对做这个,所以你需要做的就是为所有连续对做。
答案 1 :(得分:0)
所以,如果你有n
个这样的积分,行程中会有n - 1
个“腿”。您可以通过以下方式形成该列表:
legs = []
for i in xrange(n - 1):
legs.append(build_leg(point[i], point[i + 1]))
假设point
是点列表,build_leg()
是一个接受两个点并计算距离和平均速度的函数。
以上循环将调用build_leg
第一个点0和1,然后是1和2,依此类推,直到n - 2
和n - 1
为最后两个点。
答案 2 :(得分:0)
我已经爱上了滑动窗口,在这里可能会有所帮助。与其他答案概念相同,只是略有不同的方法。
from itertools import islice
def window(seq, n=2):
"Returns a sliding window (of width n) over data from the iterable"
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
it = iter(seq)
result = tuple(islice(it, n))
if len(result) == n:
yield result
for elem in it:
result = result[1:] + (elem,)
yield result
results = {}
# presort your points in time if necessary
for point_a, point_b in window(points):
d = distance(point_a, point_b)
if d == 1_MILE:
time_elapsed = time(point_a, point_b)
results[time_elapsed] = (point_a, point_b)