赞
踩
运行如图:工作路径---选择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文件自动裁剪的问题,欢迎大家使用该程序,后续会继续写各种遥感数据处理中常见问题的处理程序!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。