如何将tif格式的文件由30m重采样到100m?

在知乎上看到这个问题,记录一下解决的过程。

命令行的方式

如果你安装编译好gdal,可以使用以下命令:

gdalwarp -tr 100 100 input.tif output.tif

代码的方式

此外,你还可以用python代码的方式进行重采样,把影像重采样到100米。

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
# @Time : 2023/12/22 20:39 
# @File : resample_100.py

from osgeo import gdal

def resample_to_100m(input_path, output_path):
    """Resamples a raster dataset to 100 meters using GDAL.

    Args:
        input_path: Path to the input raster dataset.
        output_path: Path to the output raster dataset.
    """

    # Open the input raster dataset
    input_ds = gdal.Open(input_path)
    output_format = "GTiff"
    # Create the output raster dataset
    resampling_method = gdal.GRA_Bilinear
    output_resolution = [100/111000, 100/111000]

    # 进行重采样
    gdal.Warp(output_path, input_ds, dstSRS = input_ds.GetProjection(),format=output_format, xRes=output_resolution[0], yRes=output_resolution[1],
              resampleAlg=resampling_method)

    # Clean up
    input_ds = None
if __name__ == "__main__":
    input_path = 'path/to/input.tif'
    output_path = 'path/to/output.tif'
    resample_to_100m(input_path, output_path)

手头上没有30米的数据,现在用2米的数据来测试一下。