搜索此博客

2017年9月27日星期三

利用shp矢量文件裁剪栅格Python代码(界面版)

代码如下:
#coding=UTF-8
from Tkinter import *
from tkFileDialog import *
import Tkinter,tkMessageBox,arcpy

arcpy.CheckOutExtension("spatial")
arcpy.gp.overwriteOutput=1
OutPath=""
MaskFile=""

root=Tk()
#root.geometry("600x400")
root.title("Extrack By Mask")

def OpenData():
    filefolder1=askdirectory()
    text1.delete(0,END)
    text1.insert(0,filefolder1)
def OpenMask():
    MaskFile=askopenfilename()
    text2.delete(0,END)
    text2.insert(0,MaskFile)
def SavePath():
    OutPath=askdirectory()
    text3.delete(0,END)
    text3.insert(0,OutPath)
def Run():
    if len(str(text1.get()))==0|len(str(text2.get()))==0|len(str(text3.get()))==0:
        tkMessageBox.showinfo("Warning","Please choose the data source!")
        return

    arcpy.env.workspace=text1.get()
    Rasters=arcpy.ListRasters()
    s=0
    for Raster in Rasters:
        s=s+1
    if s==0:
        tkMessageBox.showinfo("Error","No data!")
        return
    i=1
    RunButton.config(text="Running,please wait!")
    for Raster in Rasters:
        root.title("Processing "+Raster+" ("+str(i)+"/"+str(s)+")")
        arcpy.gp.ExtractByMask_sa(Raster, text2.get(), text3.get()+"/"+Raster)
        i=i+1
    tkMessageBox.showinfo("Congratulations","All done")
    exit()

frame1=Frame(root)
frame1.pack()
label1= Label(frame1,text="Input Raster:",width=10)
label1.pack(side=LEFT)
text1 = Entry(frame1,borderwidth=1,bg='green',width=70)
text1.pack(side=LEFT)
OpenDataButton = Tkinter.Button(frame1, text ="Choose", command=OpenData,width=29)
OpenDataButton.pack(side=LEFT)

frame2=Frame(root)
frame2.pack()
label2= Label(frame2,text="Mask File:",width=10)
label2.pack(side=LEFT)
name = StringVar()
text2 = Entry(frame2,bd=1,bg='white',width=70,relief='raised',textvariable=name)
# 设置掩模矢量文件均为ArcGIS自带的shp文件
name.set("D:\Program Files (x86)\ArcGIS\Global Coastline shp\ArcGlobeData\continent.shp")
text2.pack(side=LEFT)
OpenDataButton = Tkinter.Button(frame2, text ="Choose(默认ArcGIS自带矢量文件)",relief='raised',width=29,command=OpenMask)
OpenDataButton.pack(side=LEFT)

frame3=Frame(root)
frame3.pack()
label3= Label(frame3,text="OutPath:",width=10)
label3.pack(side=LEFT)
text3 = Entry(frame3,bd=1,bg='green',width=70)
text3.pack(side=LEFT)
OpenPathButton = Tkinter.Button(frame3, text ="Choose",command=SavePath,width=29)
OpenPathButton.pack(side=LEFT)

frame4=Frame(root,bd=3)
frame4.pack()

RunButton=Tkinter.Button(frame4,text="Run",command=Run,width=20,bd=2)
RunButton.pack(side=RIGHT)

root.mainloop()

界面如图所示。


没有评论:

发表评论

LibSVM Chinese Brief Infroduction

Reference: [1]  https://blog.csdn.net/v_july_v/article/details/7624837 [2]  https://wenku.baidu.com/view/c402e983336c1eb91b375d37.html?fr...

  • Word (2)