使用 Python OSMnx 保存路线并保留其曲率

Save a route and conserve its curvature with Python OSMnx

使用 OSMnx,我希望能够保存一条路径及其曲率,如下图所示。

import osmnx as ox
import networkx as nx

# Download the road network
G = ox.graph_from_place('Monterey, California', network_type='drive')

# Starting and ending point of a trip
start = [36.580665,-121.8297467]
end = [36.594319,-121.8727587]

# Retrieve nearest node
orig_node = ox.get_nearest_node(G, start)
dest_node = ox.get_nearest_node(G, end)

# Compute the path of the trip
route = nx.shortest_path(G, orig_node, dest_node, weight='length')

# Plot the trip
fig, ax = ox.plot_graph_route(G_projected,
                              route,edge_linewidth=1,
                              node_size=20,
                              fig_height=20,route_linewidth=10)

显然,我可以保存路线 python 列表,但我会丢失路径的曲率,因为路线列表包含的节点较少。是否可以将显示的红色路线保存为 Google 折线格式或类似格式以保留其曲线形状?

您可以将路线的边缘几何图形转换为 MultiLineString:

from shapely.geometry import MultiLineString
route_pairwise = zip(route[:-1], route[1:])
edges = ox.graph_to_gdfs(G, nodes=False).set_index(['u', 'v']).sort_index()
lines = [edges.loc[uv, 'geometry'].iloc[0] for uv in route_pairwise]
MultiLineString(lines)

现在您可以访问 MultiLineString 的 .wkt 属性并将该 Well-Known Text 字符串保存到磁盘。