简体   繁体   English

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

[英]Save a route and conserve its curvature with Python OSMnx

With OSMnx, I would like to be able to save a path with its curvature as the image below shows. 使用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)

在此处输入图片说明

Obviously, I can save the route python list but I will lost the curvature of the path since route list contains fewer nodes. 显然,我可以保存路径python列表,但是由于路径列表包含的节点较少,所以我将失去路径的曲率。 Is it possible to save the displayed red route in Google polyline format or something like that in order to conserve its curved shape? 是否可以将显示的红色路线保存为Google折线或类似格式,以保留其弯曲形状?

You can convert the route's edge geometries to a MultiLineString: 您可以将路线的边缘几何形状转换为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)

Now you can access the MultiLineString's .wkt attribute and save that Well-Known Text string to disk. 现在,您可以访问MultiLineString的.wkt属性并将该熟知文本字符串保存到磁盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM