OSMnx plot_figure_ground中新增street_widths的highway值未显示问题
OSMnx中street_widths配置不生效的问题排查与解决
问题描述
在使用osmnx.plot.plot_figure_ground函数时,为street_widths字典添加的highway键值对未在生成的地图中生效。例如已配置"residential": 0.25,但OSM中标记为residential的道路(红线标注)在输出地图中并未应用该宽度设置。
配置的street_widths字典
street_widths = { "motorway": 0.75, "motorway_link": 0.75, "trunk": 0.5, "trunk_link": 0.5, "primary": 0.5, "primary_link": 0.5, "secondary": 0.25, "secondary_link": 0.25, "tertiary": 0.25, "tertiary_link": 0.25, "unclassified": 0.25, "residential": 0.25, "living_street": 0.25, "service": 0.1, "pedestrian": 0.1, "track": 0.1, "footway": 0.1, "steps": 0.1, "path": 0.1, }
输出与验证
- 地图输出结果:

- OSM道路类型查询:红线标注道路为residential类型

测试代码
import osmnx as ox ox.settings.log_console=True ox.settings.use_cache=False street_widths = { "motorway": 0.75, "motorway_link": 0.75, "trunk": 0.5, "trunk_link": 0.5, "primary": 0.5, "primary_link": 0.5, "secondary": 0.25, "secondary_link": 0.25, "tertiary": 0.25, "tertiary_link": 0.25, "unclassified": 0.25, "residential": 0.25, "living_street": 0.25, "service": 0.1, "pedestrian": 0.1, "track": 0.1, "footway": 0.1, "steps": 0.1, "path": 0.1, } land_tags = { 'building': True } bgcolor="#343434" edge_color="#FFFFFF" bldg_color="#FF0000" place="sd" point=(32.7362, -117.1129) dist=8000 dpi=1000 # Helper function to get square street networks, building footprints, and plot them def make_plot(place, point, dist=dist, dpi=dpi, network_type='all', default_width=0.25, street_widths=street_widths): # Get bounding box bbox = ox.utils_geo.bbox_from_point(point, dist=dist) # Get land features using land_tags dictionary land = ox.geometries.geometries_from_point(center_point=point, tags=land_tags, dist=dist) # Plot figure-ground diadram of streets fig, ax = ox.plot.plot_figure_ground(point=point, dist=dist, network_type=network_type, default_width=default_width, street_widths=street_widths, edge_color=edge_color, save=False, show=False, close=True, bgcolor=bgcolor) # Plot land footprint fig, ax = ox.plot.plot_footprints(land, ax=ax, color=bldg_color, bbox=bbox, dpi=dpi, save=True, show=False, close=True, filepath="{}-dist{}-dpi{}.png".format(place,dist,dpi)) make_plot(place, point, dist)
原因分析与解决方法
1. 复合highway标签不匹配
OSM中部分道路的highway属性是列表类型(例如['residential', 'service']),而street_widths仅匹配单个字符串类型的键,导致这类道路会 fallback 到default_width。
解决方法:预处理路网,将复合标签转为单个主类型:
def make_plot(place, point, dist=dist, dpi=dpi, network_type='all', default_width=0.25, street_widths=street_widths): # 先获取路网并处理复合highway标签 G = ox.graph_from_point(point, dist=dist, network_type=network_type) for u, v, k, data in G.edges(data=True, keys=True): if isinstance(data['highway'], list): # 取列表第一个元素作为主道路类型 data['highway'] = data['highway'][0] # 使用处理后的路网绘图,替代plot_figure_ground的默认获取逻辑 fig, ax = ox.plot.plot_graph(G, node_size=0, edge_color=edge_color, edge_width=ox.plot.get_edge_widths(G, street_widths=street_widths, default_width=default_width), bgcolor=bgcolor, save=False, show=False, close=True) # 后续绘制建筑代码不变 bbox = ox.utils_geo.bbox_from_point(point, dist=dist) land = ox.geometries.geometries_from_point(center_point=point, tags=land_tags, dist=dist) fig, ax = ox.plot.plot_footprints(land, ax=ax, color=bldg_color, bbox=bbox, dpi=dpi, save=True, show=False, close=True, filepath="{}-dist{}-dpi{}.png".format(place,dist,dpi))
2. 验证道路类型是否匹配
先打印路网中实际存在的highway类型,确认street_widths是否覆盖所有需要的类型:
# 在make_plot函数中添加 G = ox.graph_from_point(point, dist=dist, network_type=network_type) edges = ox.utils_graph.get_edges(G) print("实际道路类型:", edges['highway'].unique())
如果输出中有未在street_widths中定义的类型(包括复合字符串如residential;service),需要将这些类型也添加到字典中。
3. 版本兼容性问题
你使用的是OSMnx v0.16.0的示例代码,旧版本对street_widths的匹配逻辑可能存在局限。建议升级OSMnx到最新稳定版,或查看对应版本的plot_figure_ground源码确认宽度匹配逻辑。
4. 参数传递确认
确保street_widths参数正确传递到plot_figure_ground或get_edge_widths函数中,避免因变量作用域问题导致配置未生效。
内容的提问来源于stack exchange,提问作者tykom
相关产品推荐
相关产品推荐

