gpt4 book ai didi

python - 如何获取 OSMnx 中给定城市/区域的环形交叉路口数量?

转载 作者:行者123 更新时间:2023-12-04 09:27:55 27 4
gpt4 key购买 nike

我仍在试图弄清楚 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 docsconsolidate_intersections功能。

关于python - 如何获取 OSMnx 中给定城市/区域的环形交叉路口数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62944754/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com