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

时间:2019-02-12 00:11:10

标签: python polyline google-polyline 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)

enter image description here

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

1 个答案:

答案 0 :(得分:1)

您可以将路线的边缘几何形状转换为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字符串保存到磁盘。