【问题标题】:Google Distance Matrix API very slow on PythonPython 上的 Google 距离矩阵 API 非常慢
【发布时间】:2020-04-05 06:48:21
【问题描述】:

我在 python 中使用谷歌地图距离矩阵 API 来计算两点之间的自行车距离,使用纬度和经度。我正在使用一个循环来计算一个学生项目的近 300,000 行数据(我正在使用 Python 学习数据科学)。我添加了一个调试行来输出每 10,000 行的行号和距离,但是在哼了一会儿没有结果之后,我停止了内核并将其更改为每 1000 行。就这样,大约5分钟后,它终于到了第1000行。一个多小时后,它才在第70,000行。逆天。我停止了执行,那天晚些时候,我收到了来自谷歌的一封电子邮件,说我的免费试用期已经用完了。所以它不仅工作得非常慢,而且我什至不能再将它用于学生项目而不会产生巨额费用。

所以我重写了代码以使用几何图形,并且只计算“乌鸦飞翔”的距离。不是我真正想要的,但没有任何选择,这是我唯一的选择。

有谁知道另一种(开源、免费)方法来计算距离以获得我想要的,或者如何更有效地使用谷歌距离矩阵 API?

谢谢,

所以这里有更多信息,正如我建议的那样,我发布了更多信息。我正在尝试计算“站”之间的距离,并给出了大约 300K 对的经纬度。我打算设置一个函数,然后将该函数应用于数据帧(请耐心等待,我还是 python 和数据帧的新手)——但现在我使用循环来遍历所有对。这是我的代码:

i = 0
while i < len(trip):
    from_coords = str(result.loc[i, 'from_lat']) + " " + str(result.loc[i, 'from_long'])
    to_coords =  str(result.loc[i, 'to_lat']) + " " + str(result.loc[i, 'to_long'])
    # now to get distances!!!
    distance = gmaps.distance_matrix([from_coords], #origin lat & long, formatted for gmaps
                                 [to_coords], #destination lat & long, formatted for gmaps
                                 mode='bicycling')['rows'][0]['elements'][0]  #mode=bicycling to use streets for cycling
    result['distance'] = distance['distance']['value']

    # added this bit to see how quickly/slowly the code is running
    # ... and btw it's running very slowly. had the debug line at 10000 and changed it to 1000 
    # ... and i am running on a with i9-9900K with 48GB ram
    # ... why so slow?
    if i % 1000 == 0:
        print(distance['distance']['value'])
    i += 1

【问题讨论】:

  • 在 Web API 的价值(成本/收益方面)方面听起来非常有用。您对替代方法进行了哪些研究?是 Python 的问题(其他语言明显更快?),还是 API 是应用程序执行时间的主要消耗者?
  • 您在这里所要求的将远远超出您从免费 API 获得的期望。我建议查看为OpenStreetMap 开发的不同路由引擎。您应该能够将其中一些设置为在本地运行,而不必依赖外部服务器。
  • 几乎没有关于您如何使用它的信息,很难评论您使用 API 的效率。
  • 我认为问题出在谷歌距离矩阵 api 上,尽管这可能是因为我正在运行循环而不是应用函数。正如我所说,当我将循环更改为使用欧几里得几何来计算“乌鸦飞”的距离时,它眨眼间就完成了所有 300K 行。我确实研究了其他 API,但免费/开源的 API 似乎在使用方面受到限制(每天或每月从 5K 到 10K 的请求......还不够。)所以对于这个项目,我可能只需要满足于不太理想的情况解决方案。

标签: python data-science google-distancematrix-api


【解决方案1】:

您可以使用半正弦距离来近似以 KM 为单位的距离。

在这里,我的距离为 lat/long 对,为 random_distances,形状为 (300000, 2),为 numpy 数组:

import numpy as np
from sklearn.neighbors import DistanceMetric


dist = DistanceMetric.get_metric('haversine')

random_distances = np.random.random( (300000,2) )

我们可以用这个来近似距离

distances = np.zeros( random_distances.shape[0] - 2 )

for idx in range(random_distances.shape[0]-2):
    distances[idx] = dist.pairwise(np.radians(random_distances[idx:idx+2]), np.radians(random_distances[idx:idx+2]) )[0][1]

distances *= 6371000/1000  # to get output as KM

distances 现在包含距离。

  • 速度上“还行”,但可以改进。例如,我们可以去掉 for 循环,也返回 2x2 距离并且只使用 1。
  • haversine 距离是一个很好的近似值,但并不准确,我认为 API 是这样的:

来自sklearn

由于地球几乎是球形的,因此半正弦公式可以很好地近似地球表面两点之间的距离,平均误差小于 1%。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 2012-03-27
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多