赞
踩
时至今日,笔者已经总结了三种用Python语言结合ArcGIS10.2提供的接口去批量处理地理数据的方法。即:
1. 用IDLE、Python Tools for Visual Studio等去编写独立的py脚本文件;
2. 用Python提供的tKinter模块去构建可视化窗体,并将其打包成exe可执行文件;
3. 用Python脚本工具自定义工具箱。
下面对三种方法分别做介绍。
一、用IDLE、Python Tools for Visual Studio或PyCharm等去编写py文件;
arcpy.env.workspace = "F:\\GIMMS 3g\\3Mask"
rasters = arcpy.ListRasters("*", "tif")
for raster in rasters:
out= "F:\\GIMMS 3g\\4Project\\"+"pro_"+raster[4:12]+".tif"
arcpy.ProjectRaster_management(raster,out, "PROJCS['MyAlbers',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['central_meridian',105.0],PARAMETER['Standard_Parallel_1',25.0],PARAMETER['Standard_Parallel_2',47.0],PARAMETER['latitude_of_origin',0.0],UNIT['Meter',1.0]]", "NEAREST", "9660.84811813477", "", "", "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
print("pro_"+raster[4:12]+" has done !!!")
print("All done")
二、用Python提供的tKinter模块去构建可视化窗体,并将其打包成exe可执行文件;
1、说明
(1)先用ArcGIS或者ENVI软件中获取投影参数,将其保存在Pro.txt中;
(2)确定输入路径、投影文件和输出路径即可。
(3)投影文件格式如下所示:
2、效果
3、代码
#coding=utf-8
from Tkinter import *
from tkFileDialog import *
import Tkinter,tkMessageBox,arcpy
arcpy.CheckOutExtension("spatial")
arcpy.gp.overwriteOutput=1
root=Tk()
root.title("Project Raster")
def OpenData():
global RasterFolder
RasterFolder=askdirectory()
text1.delete(0,END)
text1.insert(0,RasterFolder)
def OpenProFile():
global ProFile
ProFile=askopenfilename()
f=open(ProFile,'r')
global ProContent
ProContent=f.read()
f.close()
text2.delete(0,END)
text2.insert(0,ProFile)
OutPath=""
def SavePath():
global OutPath
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=RasterFolder
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.ProjectRaster_management(Raster,OutPath+"/"+Raster,ProContent)
i=i+1
tkMessageBox.showinfo("Congratulations","All done")
exit()
frame1=Frame(root)
frame1.pack(side=TOP)
label1= Label(frame1,text="Input Raster:",width=10)
label1.pack(side=LEFT)
text1 = Entry(frame1,bd=1,width=60)
text1.pack(side=LEFT)
OpenDataButton = Tkinter.Button(frame1, text ="Choose", command =OpenData)
OpenDataButton.pack(side=LEFT)
frame2=Frame(root)
frame2.pack(side=TOP)
label2= Label(frame2,text="ProFile:",width=10)
label2.pack(side=LEFT)
text2 = Entry(frame2,bd=1,width=60)
text2.pack(side=LEFT)
OpenMaskButton = Tkinter.Button(frame2, text ="Choose",command=OpenProFile)
OpenMaskButton.pack(side=LEFT)
frame3=Frame(root)
frame3.pack(side=TOP)
label3= Label(frame3,text="OutPath:",width=10)
label3.pack(side=LEFT)
text3 = Entry(frame3,bd=1,width=60)
text3.pack(side=LEFT)
OpenPathButton = Tkinter.Button(frame3, text ="Choose",command=SavePath)
OpenPathButton.pack(side=LEFT)
frame4=Frame(root,bd=3)
frame4.pack(side=TOP)
RunButton=Tkinter.Button(frame4,text="Run",command=Run,width=20,bd=2)
RunButton.pack(side=RIGHT)
root.mainloop()
三、用Python脚本工具自定义工具箱
(1)新建一个test.py文件,内容可以为空;
(2)Catalog中找到My ToolBoxes→右键→new→Tool Box,命名为GeoStorm,在GeoStorm上右击→Add→Script,如下图所示:
(3)如上图工具箱所示,在Project Raster上右击→Edit,打开代码窗口,这个文件就是(1)新建的test.py,并写入如下核心代码,可以看出,跟前面两种方法的代码区别不是很大。
(4)双击Project Raster,就看到了我们熟悉的窗口了!!!,其中RasterPath文件中的是要进行投影转换的数据,格式可以使Grid,tif或者img;Projection是目标投影,可以选择,也可以自定义;OutPath是输出路径,输出文件的命名跟源文件一样,当然也可以打开edit test.py进行自定义输出文件命名格式。
(5)右击GeoStorm→Save As→10.0 ToolBox,保存,在别的地方使用。
(6)在菜单栏上打开ArcToolBox,右击→Add ToolBox,即可对(5)步保存的工具箱进行使用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。