使用Python OSMnx保存管线并保持其曲率

使用Python OSMnx保存管线并保持其曲率,python,polyline,google-polyline,osmnx,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

使用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)


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

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

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)

现在,您可以访问多重导线的
.wkt
属性并将该字符串保存到磁盘。

您可以将管线的边缘几何图形转换为多重导线:

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
属性并将该字符串保存到磁盘。

太多了,先生,正是我需要的删除
.iloc[0]
,否则“'LineString'对象没有属性'iloc'”太多了,先生,正是我需要的删除
.iloc[0]
,否则“'LineString'对象没有属性'iloc'”