【问题标题】:Shortest route when converted to LineString misses the path in OSMNX转换为 LineString 时的最短路径错过了 OSMNX 中的路径
【发布时间】:2020-09-12 00:15:32
【问题描述】:

我正在尝试将路由保存到我使用 OSMNX 绘制的磁盘。 生成的路线遵循道路路径,但是,当我尝试将路径转换为可以轻松保存到磁盘的 LineString 时,路线会以不遵循道路的方式更改,您可以在两个图像之间进行比较。

ORIGIN_point = (13.013206, 77.670987)
DESTINATION_point= (12.821339, 77.678500)          

G = ox.graph_from_point(ORIGIN_point, distance=10000, distance_type='network', network_type='drive')      

ORIGIN_node  = ox.get_nearest_node(G, ORIGIN_point)
DESTINATION_node  = ox.get_nearest_node(G, DESTINATION_point)                   




# find the route between these nodes then plot it
route = nx.shortest_path(G, ORIGIN_node, DESTINATION_node, weight='length')

This route is plotted using osmnx for shortest path IMAGE-1

from shapely.geometry import LineString, Point

graph_proj = ox.project_graph(G)
nodes_proj, edges_proj = ox.graph_to_gdfs(graph_proj, nodes=True, edges=True)
route_nodes = nodes_proj.loc[route]

# Create a geometry for the shortest path
route_line = LineString(list(route_nodes.geometry.values))

route is now converted to lineStringIMAGE-2

我想在 shapefile 中保存到磁盘的路线,请帮帮我。

【问题讨论】:

    标签: python shapely osmnx


    【解决方案1】:

    这会将您的 OSMnx 最短路径路径保存为 shapefile 中的 LineString 几何:

    import geopandas as gpd
    import networkx as nx
    import osmnx as ox
    from shapely.geometry import LineString, MultiLineString
    ox.config(log_console=True, use_cache=True)
    
    orig = (13.013206, 77.670987)
    dest = (12.821339, 77.678500)
    G = ox.graph_from_point(orig, dist=10000, dist_type='network', network_type='drive')
    orig_node  = ox.get_nearest_node(G, orig)
    dest_node  = ox.get_nearest_node(G, dest)
    
    # impute edge speeds, add travel times, calculate shortest path
    G = ox.add_edge_speeds(G)
    G = ox.add_edge_travel_times(G)
    route = nx.shortest_path(G, orig_node, dest_node, weight='travel_time')
    
    # OPTION 1: save node-to-node straightline route as shapefile
    nodes, edges = ox.graph_to_gdfs(G)
    route_nodes = nodes.loc[route]
    route_line = LineString(route_nodes['geometry'].tolist())
    gdf1 = gpd.GeoDataFrame(geometry=[route_line], crs=ox.settings.default_crs)
    gdf1.to_file('route1')
    
    # OPTION 2: save route edge geometries as shapefile
    edges = edges.set_index(['u', 'v', 'key'])
    geoms = [edges.loc[(u, v, 0), 'geometry'] for u, v in zip(route[:-1], route[1:])]
    gdf2 = gpd.GeoDataFrame(geometry=[MultiLineString(geoms)], crs=ox.settings.default_crs)
    gdf2.to_file('route2')
    

    【讨论】:

    • 折线缺少代码的实际边缘。 prnt.sc/st8sha 谢谢!
    • 我已经编辑了答案以提供第二个选项,以保持沿线的所有边缘几何形状。
    猜你喜欢
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    相关资源
    最近更新 更多