0
点赞
收藏
分享

微信扫一扫

基于python实现.tif格式遥感影像的批量裁剪

遥感影像:单波段

初始代码:​​https://zhuanlan.zhihu.com/p/157280273​​

加一段代码将多个待裁剪的.tif文件批量读取出来,再将裁剪操作代码放进读取批量.tif的循环当中,就可实现批量裁剪。

from osgeo import gdal
import os

input_shape = r"D:\cities\shapfiles\ZG_cities.shp"# tif输入路径,打开文件
inputfile_path = r"D:\cities\Inputimages"
outputfile_path = r'D:\cities\Erased_images\\'
list_files=os.listdir(inputfile_path)
targetFile = []
for i in list_files:
targetFile.append(i[:-13] + i[-4:])
targetFile=list(set(targetFile))
print(targetFile, "000")
for j in range(len(targetFile)):
erasedname = targetFile[j]
print(erasedname, "00")
name_str = erasedname[:-4]
if os.path.exists(erasedname):
os.remove(erasedname)
print(name_str, "0")
for readPath in list_files:
if readPath.startswith(name_str):
input_file = inputfile_path + "\\" + readPath
print(input_file, "1")
# output_file = outputfile_path + "\\" + name_str + ".tif"
output_file = outputfile_path + erasedname
print(output_file, "2")
# 开始裁剪,一行代码,爽的飞起
RasterErase = gdal.Warp(output_file,
input_file,
format='GTiff',
cutlineDSName=input_shape,
cutlineWhere="FIELD = 'whatever'",
dstNodata=0)


举报

相关推荐

0 条评论