4

我正在尝试解决一个奇怪的问题。也许你们知道一些处理这个问题的算法。

我有一辆货运卡车的数据,想提取一些数据。假设我有一个从 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)

我还在做一些非常低效的计算。

4

3 回答 3

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

你已经说过(在评论中)你可以对一对做这个,所以你需要做的就是对所有连续的对做这个。

于 2012-07-04T09:19:28.360 回答
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)
于 2012-07-04T12:26:34.143 回答
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两个点。

于 2012-07-04T09:20:04.930 回答