【GDAL】如何利用GDAL批量进行等同于Arcmap中Mosaic To New Raster的操作

【GDAL】如何利用python中的GDAL批量进行等同于Arcmap中Mosaic To New Raster的操作

GDAL.BuildVRT

这里主要参考 gdal官方文件,生成VRT格式文件:GDAL.BuildVRT

# 最好保证所有栅格数据在同一坐标系下,且具有相同的分辨率
vrt = gdal.BuildVRT(f"{name}.vrt", tif_files_list) # tif_files_list 包含所有要Mosaic的栅格文件
vrt = None

VRT Pixelfunction

随后利用 lxml库读写VRT文件,并加入自定义的Pixel function以解决重叠栅格取值的问题,下面代码以获取最大值为例。如果对VRT文件的构成感兴趣可以参考: VRT

from lxml import etree


vrt_tree = etree.parse(f"{name}.vrt")
vrt_root = vrt_tree.getroot()
vrtband1 = vrt_root.findall(".//VRTRasterBand[@band='1']")[0]

vrtband1.set("subClass", "VRTDerivedRasterBand")
pixelFunctionType = etree.SubElement(vrtband1, 'PixelFunctionType')
pixelFunctionType.text = "find_max"
pixelFunctionLanguage = etree.SubElement(vrtband1, 'PixelFunctionLanguage')
pixelFunctionLanguage.text = "Python"
pixelFunctionCode = etree.SubElement(vrtband1, 'PixelFunctionCode')
pixelFunctionCode.text = etree.CDATA("""
import numpy as np

def find_max(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, buf_radius, gt, **kwargs):
	np.amin(in_ar, axis=0, initial=255, out=out_ar) 
""")

GDAL.Translate

最后利用gdal.Translate将VRT文件转换为TIF格式,同时可以利用该函数对TIF文件进行压缩:gdal.Translate

gdal.Translate(f"{name}.TIF", f"{name}.vrt")
                            
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值