如何简单地计算从一个点到另一个点的旅行时间? (无剧情)

How to simply compute the travel time from one point to an other? (Without a plot)

我花了很多时间阅读和测试 example notebooks of OSMnx 但我想不出一种方法来简单地计算从给定点(GPS 坐标)到另一个点的旅行时间。 我想估计,对于我列表中的每个点,到达特定点(有时 100 公里以外)需要多长时间。我不需要生成 graph/map/plot,因为我只需要每次旅行的持续时间(而且我认为 OSMnx 地图在城市范围内渲染得更好)。

我非常绝望,因为我找不到在不同 Python 库中执行此操作的简单方法...如果在国家比例地图中对 +-10k 点进行此计算要求太多来自 OSMnx,本地存储的国家/地区 pbf 文件是否有助于另一种解决方案?

当您想要对整个区域或整个国家等大型研究区域建模时,存在固有的权衡:1) 模型精度与 2) 面积大小与 3) memory/speed。您需要权衡这三者之一。

首先,您可以建模一个粗粒度的网络,例如仅 region/country 中的主要道路,而不是数百万个细粒度的住宅街道和路径。其次,您可以研究较小的区域。对于第三种,您可以提供一台具有大量内存的机器,然后让脚本 运行 等待一段时间以完成该过程。您的权衡将取决于您自己对此分析的需求。

在下面的示例代码中,我选择权衡 #1:我已经通过高速公路和 t运行k 条道路对该地区(西米德兰兹)进行了建模。给定一个不同的分析目标,您可以权衡其他事情。创建模型后,我使用多处理随机 sample 1000 origin and destination lat-long points, snap them to the nearest nodes in the graph, and solve the shortest paths by travel time (accounting for speed 限制)。

import osmnx as ox

# get boundaries of West Midlands region by its OSM ID
gdf = ox.geocode_to_gdf('R151283', by_osmid=True)
polygon = gdf.iloc[0]['geometry']

# get network of motorways and trunk roads, with speed and travel time
cf = '["highway"~"motorway|motorway_link|trunk|trunk_link"]'
G = ox.graph_from_polygon(polygon, network_type='drive', custom_filter=cf)
G = ox.add_edge_speeds(G)
G = ox.add_edge_travel_times(G)

# randomly sample lat-lng points across the graph
origin_points = ox.utils_geo.sample_points(ox.get_undirected(G), 1000)
origin_nodes = ox.nearest_nodes(G, origin_points.x, origin_points.y)
dest_points = ox.utils_geo.sample_points(ox.get_undirected(G), 1000)
dest_nodes = ox.nearest_nodes(G, dest_points.x, dest_points.y)

%%time
# solve 1000 shortest paths between origins and destinations
# minimizing travel time, using all available CPUs
paths = ox.shortest_path(G, origin_nodes, dest_nodes, weight='travel_time', cpus=None)
# elapsed time: 9.8 seconds

为了更快地建模,您可以从 .osm XML file instead of having to make numerous calls to the Overpass API. OSMnx by default divides your query area into 50km x 50km pieces, then queries Overpass for each piece one a time to not exceed the server's per-query memory limits. You can configure 这个 max_query_area_size 参数加载网络数据,以及服务器内存分配,如果您喜欢使用OSMnx 的 API 查询函数而不是其来自文件的功能。