OSMNX/networkx 投影断开节点

OSMNX/networkx projection disconnects nodes

我想为给定的国家创建等值线图。

我使用 OSMNX 获取了路线图:

cf = '["highway"~"motorway|motorway_link|trunk|primary|secondary"]'
G = ox.graph_from_place('Venezuela', network_type='drive', custom_filter=cf)
# project the graph to UTM
G_projected = ox.project_graph(G)

然后我从起点(纬度,经度)创建了给定半径内所有节点的子图:

node = ox.get_nearest_node(G, (8.284904228184445, -62.72239713960212))
subgraph = nx.ego_graph(G_projected, node, radius=10000, distance="length")

结果子图仅包含 1 个节点(无论使用什么半径)

list(subgraph.nodes())
[5098062996]

邻居列表为空:

list(G_projected.neighbors(5098062996))

但对于图 G(进行投影之前):

list(G.neighbors(5098062996))
[5098062995]

起点的CRS为EPSG:4326,WGS 84,project_graph将其投影到图质心所在的UTM带。但是结果 CRS 是什么,我猜在这种情况下,它也是 WGS 84,因为 G 和 G_projected 的结果在几何方面没有差异。是不是每次都要投影G到UTM,断开的节点呢,是不是投影运算的结果?

事实证明,以下方法对确定问题的帮助不大:

list(nx.isolates(G_projected))
list(nx.isolates(G))

两个return空列表。这意味着每个节点都有邻居(连接?)

给定节点也在图的连通分量列表中:

5098062996 in list(nx.connected_components(G_projected.to_undirected()))[0]
True

我们有矛盾的信息,节点已连接但没有邻居,结果子图仅包含起点。

我必须强调,对于不同的节点,一切都工作正常,我只遇到了那个特定节点的问题。对于没有投影的 G 它有效。我只使用了投影,因为我在 OSMNX github 页面的笔记本示例中看到了它。

我应该根本不使用投影,或者我应该使用它并且图形连接存在一些其他问题?

如有任何建议,我们将不胜感激。

我无法重现您的确切查询,因为获取整个委内瑞拉的图表需要 700 多次下载。但是我可以在那个点附近重现您感兴趣的区域,并且一切似乎都在正常工作:

import networkx as nx
import osmnx as ox
ox.config(use_cache=True, log_console=True)

pt = 8.28490, -62.72240
cf = '["highway"~"motorway|motorway_link|trunk|primary|secondary"]'
G = ox.graph_from_point(pt, dist=5000, network_type='drive', custom_filter=cf)
G_projected = ox.project_graph(G)

node = ox.get_nearest_node(G, pt)
print(list(G.neighbors(node))) #[]
print(list(G_projected.neighbors(node))) #[]
print(list(G.predecessors(node))) #[5098062995]
print(list(G_projected.predecessors(node))) #[5098062995]

x = G.nodes[node]['x']
y = G.nodes[node]['y']
bbox = ox.utils_geo.bbox_from_point((y, x), dist=200, project_utm=True)
nc = ['r' if n==node else 'w' for n in G.nodes()]
fig, ax = ox.plot_graph(G_projected, node_color=nc, node_size=50, bbox=bbox)

有问题的节点是图表中的 this one, which given the network types in your custom filter, has no successor nodes in your graph. For a MultiDiGraph, neighbors is equivalent to successors and thus in both the unprojected and projected graphs, you get an empty list (and an empty subgraph when induced on this node). This node does however have one predecessor,您可以使用 predecessors 方法在代码片段中看到它。