首页 > 解决方案 > 如何在 OSMnx 中获取给定城市/地区的环岛数量?

问题描述

我仍在尝试找出 OSM 和 OSMnx。例如,我想计算一下巴黎有多少个环形交叉路口。问题是许多环形交叉路口都是作为道路存储的,但是是分段存储的。所以,如果我计算所有的标签 where 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()))

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

标签: pythonpandasopenstreetmaposmnx

解决方案


由于 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


推荐阅读