我不清楚这里的确切问题是什么,但至于:
我从 osmnx 和 networkx 开始。我创建了不同的路径,但我无法检查它们的距离。在文档中找不到任何内容。
此功能在 OSMnx 使用示例/文档以及 NetworkX 文档中都有说明:
import networkx as nx
import osmnx as ox
ox.config(use_cache=True, log_console=True)
# get a graph, an origin, and a destination
G = ox.graph_from_place('Piedmont, CA, USA', network_type='drive')
orig, dest = list(G)[0], list(G)[-10]
# calculate the shortest path from origin to destination
path = nx.shortest_path(G, orig, dest, weight='length')
# the length of each edge traversed along the path
lengths = ox.utils_graph.get_route_edge_attributes(G, path, 'length')
# the total length of the path
path_length = sum(lengths)
# or just directly calculate the shortest path's length from origin to destination
path_length = nx.shortest_path_length(G, orig, dest, weight='length')
如果您想找到至少 L 长度的最短路径,您可以使用 OSMnx 的 k_shortest_paths 函数然后遍历路径,直到找到长度 >= L 的路径(确定每个路径的长度,如上面的代码sn-p)。
L = 3400
paths = ox.k_shortest_paths(G, orig, dest, 1000, 'length')
for i, path in enumerate(paths):
length = sum(ox.utils_graph.get_route_edge_attributes(G, path, 'length'))
if length >= L:
break
i, length, path