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

2024-06-07 01:07:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我仍在试图弄清楚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()))

两者都是错误的,我想不出一个正确的方法来做这件事。有人能帮忙吗


Tags: tofalsetrueisosmcountthisgraph
1条回答
网友
1楼 · 发布于 2024-06-07 01:07:49

因为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函数的the docs

相关问题 更多 >