You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OSMnx计算方向秩序时遭遇KeyError: 'bearing'错误

问题:计算Khalifa City方向秩序时出现KeyError: 'bearing'

参考方向秩序计算方法,针对Khalifa City运行代码时触发KeyError: 'bearing'错误,相关代码及错误信息如下:

运行代码

ox.config(use_cache=True, log_console=True)

query = 'Khalifa City'
entropy_bins = 36

def reverse_bearing(x):
    return x + 180 if x < 180 else x - 180

def count_and_merge(n, bearings):
    n = n * 2
    bins = np.arange(n + 1) * 360 / n
    count, _ = np.histogram(bearings, bins=bins)
    count = np.roll(count, 1)
    return count[::2] + count[1::2]

Gu = ox.add_edge_bearings(ox.get_undirected(ox.graph_from_place(query, network_type='drive', simplify=True)))
b = pd.Series([d['bearing'] for u, v, k, d in Gu.edges(keys=True, data=True)])
bearings = pd.concat([b, b.map(reverse_bearing)]).reset_index(drop='True')
bin_counts = count_and_merge(entropy_bins, bearings)
orientation_entropy = stats.entropy(bin_counts)

perfect_grid = [1] * 4 + [0] * (entropy_bins - 4)
min_entropy = stats.entropy(perfect_grid)
max_entropy = np.log(entropy_bins)    
orientation_order = 1 - ((orientation_entropy - min_entropy) / (max_entropy - min_entropy)) ** 2
print(orientation_order)

错误信息

C:\Anaconda\envs\ox\Lib\site-packages\osmnx\utils.py:192: UserWarning: The `utils.config` function is deprecated and will be removed in a future release. Instead, use the `settings` module directly to configure a global setting's value. For example, `ox.settings.log_console=True`.
  warnings.warn(
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[62], line 17
     14     return count[::2] + count[1::2]
     16 Gu = ox.add_edge_bearings(ox.get_undirected(ox.graph_from_place(query, network_type='drive', simplify=True)))
---> 17 b = pd.Series([d['bearing'] for u, v, k, d in Gu.edges(keys=True, data=True)])
     18 bearings = pd.concat([b, b.map(reverse_bearing)]).reset_index(drop='True')
     19 bin_counts = count_and_merge(entropy_bins, bearings)

Cell In[62], line 17, in <listcomp>(.0)
     14     return count[::2] + count[1::2]
     16 Gu = ox.add_edge_bearings(ox.get_undirected(ox.graph_from_place(query, network_type='drive', simplify=True)))
---> 17 b = pd.Series([d['bearing'] for u, v, k, d in Gu.edges(keys=True, data=True)])
     18 bearings = pd.concat([b, b.map(reverse_bearing)]).reset_index(drop='True')
     19 bin_counts = count_and_merge(entropy_bins, bearings)

KeyError: 'bearing'

解决方案

  • 过滤无bearing字段的边:部分边因长度过短、拓扑异常等原因无法计算方位角,提取数据时跳过这些边即可:
    # 替换原代码中提取b的行
    b = pd.Series([d['bearing'] for u, v, k, d in Gu.edges(keys=True, data=True) if 'bearing' in d])
    
  • 适配OSMnx新版本:原代码中ox.config已被弃用,改用官方推荐的ox.settings配置:
    # 替换原代码第一行
    ox.settings.use_cache = True
    ox.settings.log_console = True
    
  • 关闭图形简化:简化图形可能丢失部分边的拓扑信息,尝试关闭simplify参数重新获取图形:
    Gu = ox.add_edge_bearings(ox.get_undirected(ox.graph_from_place(query, network_type='drive', simplify=False)))
    

内容的提问来源于stack exchange,提问作者kareem almuhtadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 13:13:11