python---shp文件裁剪tif文件(批量自动化)

运行如图:工作路径---选择tif文件夹

                  输出路径---选择输出tif文件夹

                  选择shp文件

                  运行

具体代码如下:

from osgeo import gdal
import tkinter as tk
from tkinter import *
from tkinter import filedialog
import os


# 选择路径函数
def select_path1():
    global path_1
    path_1 = filedialog.askdirectory()
    print("工作路径:", path_1)
    return (path_1)


# shp file
def select_file():
    global file_shp
    root = tk.Tk()
    root.withdraw()
    file_shp = filedialog.askopenfilename()
    print("选择的文件:", file_shp)
    return file_shp


def select_path3():
    global path_3
    path_3 = filedialog.askdirectory()
    print("输出路径:", path_3)
    return (path_3)


def cut(x, y):
    folder_path = x  # 指定文件夹路径
    output_folder = y
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    file_path_list = []  # 存储文件路径的列表
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.endswith(".tif"):
                file_path = os.path.join(root, file)
                file_path_list.append(file_path)
    file_name_list = []  # 存储文件名的列表
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.endswith(".tif"):
                file_name_list.append(file)

    for i, n in zip(file_path_list, file_name_list):
        # 只需更换 dst(存放路径及文件名)
        # shp文件
        # 待裁剪的影像
        dst = output_folder + '/' + n
        shp = file_shp
        src = i.replace("\\", "/")
        ds = gdal.Warp(dst,  # 裁剪图像保存完整路径(包括文件名)
                       src,  # 待裁剪的影像
                       # warpMemoryLimit=500 内存大小M
                       format='GTiff',  # 保存图像的格式
                       cutlineDSName=shp,  # 矢量文件的完整路径
                       cropToCutline=True,
                       copyMetadata=True,
                       creationOptions=['COMPRESS=LZW', "TILED=True"])
        print("正在裁剪:", src)


# 创建窗口
window = tk.Tk()

# 设置长宽
window.geometry("400x400")
# 设置颜色
window.configure(background="white")
# 窗口标题
window.title("nc转tif")
LABLE = Label(window, bg="#8c52ff", fg="#ffffff", text="欢迎使用tif裁剪工具---Cuit", font=("Helvetica", 15, "bold"), pady=10)
LABLE.place(x=90, y=0)
# 工作路径
button1 = tk.Button(window, text="工作路径", command=None)
button1.place(x=55, y=60)
button1.configure(command=select_path1)
# 选择shp文件
button1 = tk.Button(window, text="选择shp文件", command=None)
button1.place(x=200, y=100)
button1.configure(command=select_file)
# 输出路径
button3 = tk.Button(window, text="输出路径", command=None)
button3.place(x=55, y=180)
button3.configure(command=select_path3)

# 运行
button4 = tk.Button(window, text="运行", command=None)
button4.place(x=200, y=200)
button4.configure(command=lambda: cut(path_1, path_3))

window.mainloop()

该程序解决了tif文件自动裁剪的问题,欢迎大家使用该程序,后续会继续写各种遥感数据处理中常见问题的处理程序!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值