[英]is it possible to find the shortest path between an origin node and multiple destination nodes? Using Networkx or OSMnx
orig_node = ox.nearest_nodes(G, orig_x, orig_y)
dest_nodes = ox.nearest_nodes(G, dest_x, dest_y)
route = ox.distance.shortest_path(G, orig_node, dest_nodes, weight='length')
ValueError: orig and dest must either both be iterable or neither must be iterable
orig_node is a singular osm node id -> 1969350363 orig_node 是一个单一的 osm 节点 id -> 1969350363
dest_nodes is a list of osm node ids -> [2345488820, 2345484728, 3173262522, 1837961033] dest_nodes 是 osm 节点 ID 的列表 -> [2345488820、2345484728、3173262522、1837961033]
I want to find the shortest route between the orig_node and all of the dest_nodes我想找到 orig_node 和所有 dest_nodes 之间的最短路线
To find the shortest weighted path between pairs of origin and destination nodes, you just need to find the min
, using the path length as the key
.要找到源节点和目标节点对之间的最短加权路径,您只需找到
min
,使用路径长度作为key
。 Here's a minimal reproducible example:这是一个最小的可重现示例:
import osmnx as ox
G = ox.graph_from_place("Piedmont, CA, USA", network_type="drive")
# return shortest weighted path between sets of origins and destinations
def find_shortest(G, orig, dest, weight):
results = []
for o, d in zip(orig, dest):
path = ox.shortest_path(G, o, d, weight)
dist = sum(ox.utils_graph.get_route_edge_attributes(G, path, "length"))
results.append((o, d, path, dist))
return min(results, key=lambda x: x[3])
# pick some random origins and destinations
n = list(G.nodes)
dest = [n[20], n[30], n[40], n[50]]
orig = [n[0]] * len(dest)
# get the origin, destination, path, and distance of the shortest route
o, d, path, dist = find_shortest(G, orig, dest, "length")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.