【问题标题】:How can I get a shortest path on OpenStreetMap?如何在 OpenStreetMap 上获得最短路径?
【发布时间】:2020-09-27 10:51:12
【问题描述】:

我一直在努力寻找 OpenStreetMap 上的最短路径。但部分路线与实际道路不符。如图所示。 The routes do not match the actual roads.有什么办法可以解决这个问题吗?另外,这里是代码:

import osmnx as ox
import networkx as nx
import folium

lat = [-33.889606, -33.889927, -33.889155, -33.891134]
lon = [151.283306, 151.280497, 151.278007, 151.274453]

fmap = folium.Map(location=[-33.889606, 151.283306], zoom_start=15)
colors=["red", "yellow", "green"]

for i in range(3):
    G = ox.graph_from_point((-33.889606, 151.283306), distance=4000,network_type='drive')
    a = ox.get_nearest_node(G, (lat[i], lon[i]))
    b = ox.get_nearest_node(G, (lat[i+1], lon[i+1]))
    route = nx.shortest_path(G, a, b)
    gdf_nodes, gdf_edges = ox.graph_to_gdfs(G)
    latitude = gdf_nodes.loc[route].y
    longitude = gdf_nodes.loc[route].x

    latitude = latitude.values
    longitude = longitude.values

    latitude=latitude.tolist()
    longitude=longitude.tolist()

    coordinate=[]
    for j in range(len(latitude)):
        coordinate.append([latitude[j],longitude[j]])
    fmap.add_child(folium.PolyLine(locations=coordinate, weight=5, color=colors[i]))

fmap

【问题讨论】:

    标签: python openstreetmap shortest-path osmnx


    【解决方案1】:

    您可以使用OSMnx 执行此操作。以下代码 sn-p 使用 folium 可视化路线,同时保持弯曲的街道几何形状:

    import networkx as nx
    import osmnx as ox
    ox.config(use_cache=True, log_console=True)
    
    # get a graph
    G = ox.graph_from_place('Piedmont, California, USA', network_type='drive')
    
    # impute missing edge speed and add travel times
    G = ox.add_edge_speeds(G)
    G = ox.add_edge_travel_times(G)
    
    # calculate shortest path minimizing travel time
    orig, dest = list(G)[0], list(G)[-1]
    route = nx.shortest_path(G, orig, dest, 'travel_time')
    
    # create folium web map
    route_map = ox.plot_route_folium(G, route)
    route_map
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 2017-09-12
      • 2017-11-17
      • 1970-01-01
      相关资源
      最近更新 更多