问题
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