如何在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

前者只对城市中的环形交叉口进行建模,然后查找所有弱连通图组件。每个离散构件都被视为唯一的环行交叉口。后者对交叉点进行聚类(拓扑合并(,然后检查哪些交叉点的缓冲区与环行交叉口边缘重叠。另请参阅有关consolidate_intersections函数的文档。

相关内容

最新更新