繁体   English   中英

使用 python.networkx 获取最短路径几何

[英]Getting the shortest path geometry using python networkx

如何从 .networkx 获取路径作为几何图形?

下面是我的工作示例,它返回nx.shortest_path_length

import osmnx as ox
import networkx as nx
from shapely.geometry import Point

def get_network(centre_point, dist):
    G = ox.graph_from_point(centre_point, dist=dist, network_type='walk', simplify=False)
    G = ox.add_edge_speeds(G)
    G = ox.add_edge_travel_times(G)
    nodes, edges = ox.graph_to_gdfs(G, nodes=True, edges=True)
    return G, nodes, edges


point = (50.864595387190924, -2.153190840083006)
G, nodes, edges = get_network(centre_point=point, dist=1000)

a = nodes.iloc[4].name
b = nodes.iloc[20].name

nx.shortest_path_length(G, a, b, weight='length', method='dijkstra')

这给出了最短路径长度223.964 ,我如何获得这条最短路径的实际路径几何? 路径在edges ,但如何为这条路径提取正确的路径?

您可以使用shortest_path而不是shortest_path_length 例子:

nx.shortest_path(G, a, b, weight='length', method='dijkstra')

结果:

[29954468,
 1586097212,
 1586097440,
 1586097310,
 4500037088,
 2279573449,
 2279573451,
 2279573460,
 10081635099,
 1585877072,
 294588504,
 8515680389,
 294588511,
 294588514,
 8515680388,
 294588551]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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