import rasterio from rasterio.warp import calculate_default_transform, reproject, Resampling # 输入输出路径 src_path = r"F:\data.tif" dst_path =r"F:\data_mercator.tif" # 定义目标坐标系(Web墨卡托投影,EPSG:3857) dst_crs = "EPSG:3857" with rasterio.open(src_path) as src: # 计算目标投影的transform和尺寸 transform, width, height = calculate_default_transform( src.crs, dst_crs, src.width, src.height, *src.bounds ) # 创建输出文件元数据 kwargs = src.meta.copy() kwargs.update({ 'crs': dst_crs, 'transform': transform, 'width': width, 'height': height }) # 创建目标文件并执行重投影 with rasterio.open(dst_path, 'w', ** kwargs) as dst: # 注意此处 U+200B 字符 for i in range(1, src.count + 1): reproject( source=rasterio.band(src, i), destination=rasterio.band(dst, i), src_transform=src.transform, src_crs=src.crs, dst_transform=transform, dst_crs=dst_crs, resampling=Resampling.bilinear # 双线性插值保持数据平滑 )