批量将目录下的栅格数据重采样至不同分辨率 import os import rasterio from rasterio.warp import calculate_default_transform, reproject, Resampling # 仅修改路径即可 # 1. 输入文件夹原始TIFF所在路径 INPUT_DIR r/.../your/input/path/ # 2. 输出文件夹重采样后的文件保存路径 OUTPUT_DIR r/.../your/output/path/ # 3. 分辨率根据自己需要修改这里我设置0.01度 TARGET_RES 0.01 # 分辨率0.01度 RESAMPLE_METHOD Resampling.bilinear # def resample_tif(input_path: str, output_path: str): with rasterio.open(input_path) as src: transform, width, height calculate_default_transform( src.crs, src.crs, src.width, src.height, *src.bounds, resolutionTARGET_RES ) out_meta src.meta.copy() out_meta.update({ crs: src.crs, transform: transform, width: width, height: height, dtype: src.dtypes[0] }) with rasterio.open(output_path, w, **out_meta) as dst: for i in range(1, src.count 1): reproject( sourcerasterio.band(src, i), destinationrasterio.band(dst, i), src_transformsrc.transform, src_crssrc.crs, dst_transformtransform, dst_crssrc.crs, resamplingRESAMPLE_METHOD ) print(f✅ 完成{os.path.basename(output_path)}) if __name__ __main__: # 自动创建输出文件夹不存在则创建存在不报错 os.makedirs(OUTPUT_DIR, exist_okTrue) tif_files [f for f in os.listdir(INPUT_DIR) if f.lower().endswith(.tif)] if not tif_files: print(❌ 输入文件夹未找到TIFF文件) exit() # 批量处理所有.tif文件 print(f 开始处理共找到 {len(tif_files)} 个TIFF文件...) for tif_name in tif_files: # 拼接输入/输出完整路径 input_path os.path.join(INPUT_DIR, tif_name) # 输出文件名原文件名 后缀 out_name os.path.splitext(tif_name)[0] .tif output_path os.path.join(OUTPUT_DIR, out_name) # 执行重采样 resample_tif(input_path, output_path) print(f\n 所有文件重采样完成结果保存在{OUTPUT_DIR})