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

安装rioxarray失败后rasterio调用affine报AttributeError如何解决

问题原因及修复方案

问题原因

该报错和rioxarray安装失败无关联,是rasterio版本迭代引发的API变更导致的:

  • 旧版本rasterio(1.0版本以下)使用.affine属性获取栅格的仿射变换参数
  • 1.0及以上版本的rasterio正式移除了.affine属性,替换为功能完全一致的.transform属性
    你之前运行正常是因为当时环境里的rasterio版本低于1.0,后续应该是环境内的rasterio被更新到了新版本,才会触发该属性不存在的报错。

修复方案

方案1(推荐,适配新版本rasterio)

仅需修改1行代码,将读取仿射变换参数的代码替换为新版API即可,修改后完整代码如下:

import json
import rasterio
from rasterstats import zonal_stats

with rasterio.open(f'{project_data}/ras.tif') as raster:
    array = raster.read(1)
    # 仅修改这一行,将.affine替换为.transform
    affine = raster.transform
    stat = zonal_stats(f'{project_data}/Lila.shp', array, affine=affine,
        stats=['median'], geojson_out=True)

    result = {"type": "FeatureCollection", "features": stat}

    outname = f'{project_data}/files/Lala_test.geojson'
    with open(outname, 'w') as outfile:
        json.dump(result, outfile)

方案2(不修改代码,降级rasterio)

如果不想改动现有代码,也可以在conda虚拟环境中执行命令降级rasterio到旧版本:
conda install -c conda-forge rasterio=0.9.9
该方案不推荐长期使用,旧版本rasterio后续会逐渐失去维护。

rioxarray安装失败补充处理

如果后续需要使用rioxarray,Windows10+Python3.8环境下优先使用conda-forge源安装即可,执行命令:
conda install -c conda-forge rioxarray
该操作和你当前的报错没有关联,按需操作即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 07:36:03