Save a route and conserve its curvature with Python OSMnx

橙三吉。 提交于 2019-12-13 04:43:19

问题


With OSMnx, I would like to be able to save a path with its curvature as the image below shows.

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. Is it possible to save the displayed red route in Google polyline format or something like that in order to conserve its curved shape?


回答1:


You can convert the route's edge geometries to a 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.



来源:https://stackoverflow.com/questions/54641004/save-a-route-and-conserve-its-curvature-with-python-osmnx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!