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

使用Rasterio提取栅格像素值与QGIS结果不一致问题求助

解决Rasterio提取像素值与QGIS不一致的问题

核心问题原因

  1. 行列号计算逻辑差异:Rasterio的rasterio.transform.rowcol()默认以像素左上角为基准计算行列号,而QGIS中点击获取的坐标对应像素中心,直接使用默认参数会导致行列号偏移。
  2. Window参数顺序错误:你之前的代码中Window的参数顺序写反了,Window的正确参数顺序是(col_off, row_off, width, height),而非(row_off, col_off, ...)。
  3. 坐标验证参数缺失:用rasterio.transform.xy()验证时未指定center=True,导致返回的是像素左上角坐标,和原输入的中心坐标存在细微差异。

修正方案

方案1:直接使用sample()方法(推荐)

无需手动转换行列号,直接传入地理坐标提取对应像素值,逻辑与QGIS完全一致:

with rasterio.open(im_list[im]) as src:
    # 传入单个坐标对,返回生成器,取第一个值
    pixel_value = next(src.sample([(lon, lat)]))[0]

方案2:修正行列号计算与Window调用

如果坚持用窗口读取,需调整参数并修正顺序:

from rasterio.windows import Window

with rasterio.open(im_list[im]) as src:
    # 指定center=True,按像素中心计算行列号
    row, col = rasterio.transform.rowcol(src.transform, xs=lon, ys=lat, center=True)
    # 四舍五入取整(与QGIS取整逻辑一致)
    row_int, col_int = int(round(row)), int(round(col))
    # 按正确顺序传入Window参数:col_off, row_off, width, height
    w = src.read(1, window=Window(col_int, row_int, 1, 1))
    pixel_value = w[0][0]

验证方法

验证像素中心坐标是否匹配原输入,需对应加上center=True:

check_lon, check_lat = rasterio.transform.xy(src.transform, row_int, col_int, center=True)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 16:12:14