如何在 OSMnx 中获取给定城市/区域的环岛数量?

How to get the number of roundabouts in a given city/ area in OSMnx?

我仍在努力弄清楚 OSM 和 OSMnx。例如,我想数一数巴黎有多少环形交叉路口。问题是许多环形交叉路口被存储为方式,但是是分段的。所以,如果我计算 junction=roundabout 处的所有标签,我会不止一次地计算一些回旋处。

我怎样才能避免这种情况并且只计算每个环岛一次?

# This is what I used to plot all the roundabouts in Paris
roundabouts = ox.graph_from_place('Paris, France', network_type = 'drive', 
              custom_filter = '["junction"~"roundabout"]', retain_all = True, simplify = False)
fig, ax = ox.plot_graph(roundabouts, node_size=0, bgcolor='k')
# This is what I tried to use to count the roundabouts
# 1st option
edges = ox.graph_to_gdfs(roundabouts, nodes=False, edges=True)
print('Roundabouts count:', edges.junction.count() )

# 2nd option, tried to group by OSM id and then count unique IDs
edges = ox.graph_to_gdfs(roundabouts, nodes=False, edges=True)
print('Roundabouts count:', len(edges[edges['junction']=='roundabout'].groupby('osmid').size()))

两者都是错误的,我想不出一个正确的方法来做到这一点。有人可以帮忙吗?

由于 OSM 标记这些元素的方式,没有简单直接的方法可以做到这一点。这里有两个选项可以产生类似的城市环岛数量估计值。两者都应该让您走上正确的轨道,但需要进一步的实验。

import networkx as nx
import osmnx as ox
ox.config(use_cache=True)
place = 'Berkeley, CA, USA'
nt = 'drive'

# OPTION ONE
cf = '["junction"="roundabout"]'
G = ox.graph_from_place(place, network_type=nt, custom_filter=cf, retain_all=True, simplify=False)
roundabouts = list(nx.weakly_connected_components(G))
len(roundabouts) #60


# OPTION TWO
tolerance = 15
G = ox.graph_from_place(place, network_type=nt)
Gp = ox.project_graph(G)
Gc = ox.consolidate_intersections(Gp, tolerance=tolerance)

edges = ox.graph_to_gdfs(Gp, nodes=False)
roundabouts = edges[edges['junction'] == 'roundabout']

nodes = ox.graph_to_gdfs(Gc, edges=False)
nodes_in_roundabouts = nodes[nodes.buffer(tolerance).intersects(roundabouts.unary_union)]
len(nodes_in_roundabouts) #59

前者只对城市中的环岛建模,然后寻找所有弱连接的图组件。每个离散组件都被认为是一个独特的环岛。后者集群(拓扑合并)交叉路口,然后检查哪些缓冲区与环岛边缘重叠。另见 the docs 关于 consolidate_intersections 函数。