OSMnx报错:'DataFrame'对象无'crs'属性,求技术支持
解决OSMnx运行报错及Shapely警告问题
核心原因
- 版本不兼容:OSMnx旧版本依赖的geopandas、Shapely API已被新版本弃用,导致
AttributeError: 'DataFrame' object has no attribute 'crs'错误及Shapely弃用警告。
解决步骤
1. 安装兼容版本组合(推荐)
卸载现有冲突包,安装经过验证的适配版本,支持Python3.7/3.9:
# 卸载现有冲突包 pip uninstall -y osmnx geopandas pandas shapely # 安装兼容版本 pip install osmnx==1.5.1 geopandas==0.12.2 pandas==1.5.3 shapely==2.0.1
该版本组合可直接运行官方示例代码,避免报错和警告。
2. 修改代码适配新版API(不降级方案)
若坚持使用最新版OSMnx,需调整代码适配API变更:
import osmnx as ox import warnings from shapely.errors import ShapelyDeprecationWarning # 屏蔽Shapely弃用警告 warnings.filterwarnings("ignore", category=ShapelyDeprecationWarning) # 加载图数据 G = ox.graph_from_bbox(37.79, 37.78, -122.41, -122.43, network_type='drive') # 新版OSMnx中,显式指定投影坐标系 G_projected = ox.project_graph(G, to_crs=ox.utils_geo.bbox_to_utm_crs(ox.graph_to_gdfs(G)[0].total_bounds)) # 绘图 ox.plot_graph(G_projected)
3. 单独屏蔽Shapely警告
若仅需消除警告,可在代码开头添加:
import warnings from shapely.errors import ShapelyDeprecationWarning warnings.filterwarnings("ignore", category=ShapelyDeprecationWarning)
内容的提问来源于stack exchange,提问作者andiburgr
相关产品推荐
相关产品推荐

