python重采样tif影像,自定义空间分辨率

import warnings

import netCDF4
warnings.filterwarnings('ignore')
warnings.filterwarnings('ignore', category=DeprecationWarning)
import netCDF4
import pandas as pd
import numpy as np
from osgeo import gdal
import matplotlib.pyplot as plt
import math
import h5py
from osgeo import osr
import numpy as np

from os import listdir
import re

def raster_resamping(inputfile, outputfile, resolution, resampling_way='average'):
    '''
        inputfile       :   输入要采样的栅格文件名
        outputfile      :   输出重采样后的栅格文件地址
        resolution      :   重采样后的分辨率,以元组形式传入,单位m
        resampling_way  :   重采样方式
    '''

    import numpy as np
    import rasterio as rio
    from rasterio.warp import calculate_default_transform, reproject
    from rasterio.enums import Resampling
    from rasterio import crs

    # 重采样方式定义:
    ways = dict([
        ['nearest', Resampling.nearest],
        ['bilinear', Resampling.bilinear],
        ['cubic', Resampling.cubic],
        ['cubic_spline', Resampling.cubic_spline],
        ['lanczos', Resampling.lanczos],
        ['average', Resampling.average],
        ['mode', Resampling.mode],
        ['max', Resampling.max],
        ['min', Resampling.min],
        ['med', Resampling.med],
        ['sum', Resampling.sum],
        ['rms', Resampling.rms],
        ['q1', Resampling.q1],
        ['q3', Resampling.q3]
    ])

    src_ds = rio.open(inputfile)
    dst_transform, dst_width, dst_height = calculate_default_transform(src_ds.crs,  # 输入坐标系
                                                                       src_ds.crs,  # 输出坐标系
                                                                       src_ds.width,  # 输入图像宽
                                                                       src_ds.height,  # 输入图像高
                                                                       resolution=resolution,  # 输出图像分辨率,
                                                                       *src_ds.bounds)  # 输入数据源的图像范围
    # 更新数据集的元数据信息
    profile = src_ds.meta.copy()
    profile.update({
        'crs': src_ds.crs,
        'transform': dst_transform,
        'width': dst_width,
        'height': dst_height
    })

    # 重投影并写入数据
    with rio.open(outputfile, 'w', **profile) as dst_ds:
        for i in range(1, src_ds.count + 1):  # 遍历每个图层,通常只需要第一层即可
            src_array = src_ds.read(i)
            dst_array = np.empty((dst_height, dst_width), dtype=profile['dtype'])  # 初始化输出图像数据

            # 重投影
            reproject(
                source=src_array,
                src_crs=src_ds.crs,
                src_transform=src_ds.transform,
                destination=dst_array,
                dst_transform=dst_transform,
                dst_crs=src_ds.crs,
                resampling=ways.get(resampling_way),
                num_threads=2)
            # 写入图像
            dst_ds.write(dst_array, i)

    print('{:-^30}'.format('重采样完成'))

temp_input = '.tif'
temp_output ='.tif'
raster_resamping(temp_input, temp_output, (0.1, 0.1), resampling_way='nearest')

 

posted @ 2023-08-15 14:46  揪你小辫子  阅读(309)  评论(0编辑  收藏  举报