简体   繁体   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 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.

相关问题 OSMNX Shortest path Nodes - 获取节点经过的时间 - OSMNX Shortest path Nodes - Get node traveled time 如何使用networkx图根据中间节点的属性值找到2个节点之间的路径? - How to find a path between 2 nodes based on in-between node's attribute's value using networkx graph? OSMnx:有没有办法找到两个坐标之间的准确最短路径? - OSMnx: Is there a way to find an accurate shortest path between 2 coordinates? 使用 R / igraph,有没有办法在考虑到唯一节点属性的计数的情况下找到节点之间的最短路径? - Using R / igraph, is there a way to find a shortest path between nodes taking the count of unique node attributes into account? 如何使用NetworkX获得一组路由中两个节点之间的最短路径? - How to get the shortest path between two nodes in a set of routes using NetworkX? Networkx:查找到Graph中多个节点之一的最短路径 - Networkx: Finding the shortest path to one of multiple nodes in Graph 查找 NetworkX 中所有节点对之间的所有最短路径 - Find all shortest paths between all pairs of nodes in NetworkX 源和目标网络列表的最短路径x - Shortest path of a list of Source and Destination networkx GraphViz,找到两个节点之间的最短路径 - GraphViz, find the shortest path between two nodes networkx中具有“关键”节点可访问的最短路径 - Shortest Path in networkx with 'key' nodes to visit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM