首页 > 编程语言 >python重采样tif影像,自定义空间分辨率

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

时间:2023-08-15 14:56:31浏览次数:42  
标签:src 自定义 crs python dst Resampling import tif ds

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')

 

标签:src,自定义,crs,python,dst,Resampling,import,tif,ds
From: https://www.cnblogs.com/19981127yjr/p/17631269.html

相关文章

  • 某公司笔试题 - 密码验证合格程序(附python代码)
    #密码要求#1.长度超过8位;2.包括大小写字母,数字,其它符号,以上四种至少三种;3.不能有长度大于2的包含公共元素的字串重复(其他符号不含空格或换行)#数据范围:输入的字符串长度满足1<=n<=100#检测输入密码defcheckpassword(psw):iflen(psw)<=8orlen(psw)>100:r......
  • python 面向对象 继承
     https://cloud.tencent.com/developer/article/1915788修改代码以解决报错classClass_1():  var_1=1    def__init__(self):    self.var_2=var_1classClass_2(Class_1):    def__init__(self):    self.var_4=self.var......
  • Python运算符全解析:技巧与案例探究
    在Python编程中,运算符是强大的工具,能够使我们在数据处理和逻辑判断方面更加灵活。本篇博客将全面探讨Python中常用的运算符,包括算术、比较、逻辑、赋值、位、成员和身份运算符,通过实际案例为你展示如何妙用运算符解决问题。算术运算符Python提供了一系列用于数值运算的算术运算符,如......
  • Python中对文件进行操作
    对于Python来说,文件处理绝对是一个常见的处理,读取文件、写入文件、生成文件……文件操作贯穿python变成始终。本篇文章将总结一下在平时编程过程中,常用的文件操作。以下将按照增删改查的顺序,对文件以及目录操作进行总结。新建文件和目录importos#新建文件new_file_path......
  • python 项目部署相关
    git代码管理和同步git本地上传全局配置gitconfig--globaluser.name"aaa"gitconfig--globaluser.email"[email protected]"进入项目目录初始化gitinit配置远程地址gitremoteaddoriginhttps://gitee.com/wupeiqi/xxxxx.git本地版本提交gitadd.g......
  • python 解决Could not import the lzma module. Your installed Python is incomplete
    python安装好pandas后import报错如下图:原因分析在执行./configure和makeinstall的时候出现错误提示,被忽略了,如下图:解决方法安装yuminstall-yxz-devel然后重新安装python,执行./configure和makeinstall......
  • iOS 根据utsname获取的Identifier获取设备名称
    在线jsonhttps://api.ipsw.me/v4/devices获取设备名称+(NSString*)getDeviceName{staticNSString*_deviceName;if(_deviceName){return_deviceName;}structutsnamesystemInfo;uname(&systemInfo);//获取设备标识Identifier......
  • C++ 调用 Python 接口 Mat转Numpy
    参考网站:https://blog.csdn.net/qq7835144/article/details/106073110?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-1&spm=1001.2101.3001.4242https://blog.csdn.net/weixin_46400740/article/details/116711323?spm=1001.2014.3001.5501 1、配......
  • esXGray开发笔记:基于直线检测的文本倾斜自动校正算法实现(python+opencv)
    昨日采用最小面积矩形的方式实现文本倾斜自动校正,但后面的角度有点麻烦,于是改用基本直线检测的算法。算法简介:检测直线,自动调节参数,至少获取11条直线(直线条数调节)计算每条直线与x轴夹角从返回的角度中找到出现次数较多的直线角度平均值并返回作为图片倾斜角度检测到角度后,就......
  • python代码中取消运行中的warning
    在Python中,可以使用warnings模块来管理和控制警告的显示。你可以通过设置警告过滤器来控制是否显示特定类型的警告。以下是一些常见的方法来处理警告:过滤警告: 可以使用warnings.filterwarnings()函数来设置警告过滤器,从而控制是否显示特定类型的警告。比如,可以使用warnings.fil......