从地理数据帧导入图形时丢失图形连接
Loss of graph connectivity when importing graph from geodataframes
我目前正在尝试为不同的节点对寻找沿着街道网络的最短路径。
我使用 Overpass Turbo 提取了一个代表街道网络的 XML 文件,并使用 OSMNX graph_from_file 函数从该文件中制作了一个图表。我使用 OSMNX graph_to_gdfs 函数提取了节点和边缘信息,其中我得到了 2 个地理数据帧。因此,我编辑了边缘地理数据框 'highway' 列下的信息以满足我的需要。我没有做任何其他改变。然后我使用 OSMNX gdfs_to_graph 函数使用节点和边缘地理数据框制作了一个新图形,它应该类似于从 XML 文件中提取的图形,除非边缘数据发生一些变化。
现在,当我尝试在图中找到从一个节点到另一个节点的最短路径时,它显示连接错误。我尝试使用直接从 XML 文件导入的图形来找到同一节点对之间的最短路径,并且效果很好。在另一种情况下,我没有对地理数据框进行任何修改,而是经历了整个过程,但效果不佳。这意味着当从我无法理解的地理数据框中导入图形时,存在一些关于连接性的问题。有人可以解决这个问题吗?
G_multiDi = ox.graph_from_file('overpass_ped_PTstop_export.xml', bidirectional=False, simplify=True, retain_all=True, name='unnamed')
G_undi = G_multiDi.to_undirected()
fig, ax = ox.plot_graph(G_undi)
nodes = ox.graph_to_gdfs(G_undi, nodes=True, edges=False)
edges = ox.graph_to_gdfs(G_undi, nodes=False, edges=True)
In [43]: nx.shortest_path(G_undi,35524093,35365678,weight = 'length')
Out[43]: [35524093, 53132201, 35524331, 1994687832, 35365678]
#GEODATAFRAME TO GRAPH
gdf_nodes = nodes
gdf_edges = edges
graph2 = ox.gdfs_to_graph(gdf_nodes, gdf_edges)
nx.shortest_path(graph2,35524093,35365678,weight = 'length')
In [47]: nx.shortest_path(graph2,35524093,35365678,weight = 'length')
NetworkXNoPath: No path to 35365678.
所以,我通过转换由 graph2 = ox.gdfs_to_graph(gdf_nodes, gdf_edges) 产生的有向图 'graph2' 自己解决了这个问题
使用无向图
图 2 = graph2.to_undirected().
graph2 = ox.gdfs_to_graph(gdf_nodes, gdf_edges)
graph2 = graph2.to_undirected()
In [72]: nx.shortest_path(graph2,35524093,35365678,weight = 'length')
Out[72]: [35524093, 53132201, 35524331, 1994687832, 35365678]
我目前正在尝试为不同的节点对寻找沿着街道网络的最短路径。
我使用 Overpass Turbo 提取了一个代表街道网络的 XML 文件,并使用 OSMNX graph_from_file 函数从该文件中制作了一个图表。我使用 OSMNX graph_to_gdfs 函数提取了节点和边缘信息,其中我得到了 2 个地理数据帧。因此,我编辑了边缘地理数据框 'highway' 列下的信息以满足我的需要。我没有做任何其他改变。然后我使用 OSMNX gdfs_to_graph 函数使用节点和边缘地理数据框制作了一个新图形,它应该类似于从 XML 文件中提取的图形,除非边缘数据发生一些变化。
现在,当我尝试在图中找到从一个节点到另一个节点的最短路径时,它显示连接错误。我尝试使用直接从 XML 文件导入的图形来找到同一节点对之间的最短路径,并且效果很好。在另一种情况下,我没有对地理数据框进行任何修改,而是经历了整个过程,但效果不佳。这意味着当从我无法理解的地理数据框中导入图形时,存在一些关于连接性的问题。有人可以解决这个问题吗?
G_multiDi = ox.graph_from_file('overpass_ped_PTstop_export.xml', bidirectional=False, simplify=True, retain_all=True, name='unnamed')
G_undi = G_multiDi.to_undirected()
fig, ax = ox.plot_graph(G_undi)
nodes = ox.graph_to_gdfs(G_undi, nodes=True, edges=False)
edges = ox.graph_to_gdfs(G_undi, nodes=False, edges=True)
In [43]: nx.shortest_path(G_undi,35524093,35365678,weight = 'length')
Out[43]: [35524093, 53132201, 35524331, 1994687832, 35365678]
#GEODATAFRAME TO GRAPH
gdf_nodes = nodes
gdf_edges = edges
graph2 = ox.gdfs_to_graph(gdf_nodes, gdf_edges)
nx.shortest_path(graph2,35524093,35365678,weight = 'length')
In [47]: nx.shortest_path(graph2,35524093,35365678,weight = 'length')
NetworkXNoPath: No path to 35365678.
所以,我通过转换由 graph2 = ox.gdfs_to_graph(gdf_nodes, gdf_edges) 产生的有向图 'graph2' 自己解决了这个问题 使用无向图 图 2 = graph2.to_undirected().
graph2 = ox.gdfs_to_graph(gdf_nodes, gdf_edges)
graph2 = graph2.to_undirected()
In [72]: nx.shortest_path(graph2,35524093,35365678,weight = 'length')
Out[72]: [35524093, 53132201, 35524331, 1994687832, 35365678]