转换为 LineString 时的最短路线错过了 OSMNX 中的路径

Shortest route when converted to LineString misses the path in OSMNX

我正在尝试将路径保存到我使用 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 中保存到磁盘的路径,请帮帮我。

这会将您的 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')