繁体   English   中英

是否有可能找到一个起始节点和多个目的节点之间的最短路径? 使用 Networkx 或 OSMnx

[英]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 是一个单一的 osm 节点 id -> 1969350363

dest_nodes 是 osm 节点 ID 的列表 -> [2345488820、2345484728、3173262522、1837961033]

我想找到 orig_node 和所有 dest_nodes 之间的最短路线

要找到源节点和目标节点对之间的最短加权路径,您只需找到min ,使用路径长度作为key 这是一个最小的可重现示例:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM