Arcpy 多线程批量重采样脚本
import arcpy
import os
import multiprocessingdef resample_tifs(input_folder, output_folder, cell_size=0.05, resampling_type="BILINEAR"):"""将指定文件夹下的所有 TIFF 文件重采样到指定分辨率,并输出到新文件夹中。参数:input_folder (str): 输入 TIFF 文件所在的文件夹路径。output_folder (str): 重采样后的 TIFF 文件输出到的文件夹路径。cell_size (float): 重采样后的分辨率大小。默认为 symbol。resampling_type (str): 重采样方法。可选值包括 "NEAREST_NEIGHBOR", "BILINEAR", "CUBIC", "MAJORITY"。默认为 "BILINEAR"。"""if not os.path.exists(input_folder):raise FileNotFoundError(f"输入文件夹不存在: {input_folder}")if not os.path.exists(output_folder):os.makedirs(output_folder)print(f"已创建输出文件夹: {output_folder}")arcpy.env.workspace = input_foldertif_list = arcpy.ListRasters("*", "TIF")if not tif_list:print("没有找到 TIFF 文件。")returnprint(f"找到 {len(tif_list)} 个 TIFF 文件。开始重采样...")for tif in tif_list:input_path = os.path.join(input_folder, tif)output_path = os.path.join(output_folder, tif)try:print(f"正在处理: {tif}")arcpy.management.Resample(in_raster=input_path,out_raster=output_path,cell_size=cell_size,resampling_type=resampling_type)print(f"成功重采样并保存到: {output_path}")except arcpy.ExecuteError:print(f"ArcPy 错误处理文件 {tif}: {arcpy.GetMessages(2)}")except Exception as e:print(f"错误处理文件 {tif}: {e}")print("所有文件重采样完成。")def main():output_dir2 = r"./010_世界人口数据 _0.1/" symbol = 0.25 tasks = [("010_世界人口数据", output_dir2, symbol, "NEAREST"),] pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())results = []print(tasks)for input_dir11, output_dir22,cell_size ,resampling_type in tasks:result = pool.apply_async(resample_tifs, args=(input_dir11, output_dir22,cell_size ,resampling_type))results.append(result)pool.close()pool.join()print("所有任务已完成。")
if __name__ == "__main__":main()