曾經寫過《使用Python腳本批量裁切柵格》,但今天又遇到這個情況則發現了問題。我們遇到的實際問題往往是有一個需要裁剪的影像(大塊的),另外有一個矢量面,現在需要按矢量面每一個要素進行裁剪,無奈arcgis里的工具無法方便地做到。只能自己寫工具,這次使用了clip而不是ExtractByMask,因為ExtractByMask有很多限制!

下面是工具的操作示例:按每一個要素進行裁剪柵格,輸出柵格以選擇的字段命名,前提是字段的每個值是唯一的。

其中,輸出類型這個combox設置方法是:

下面是消息輸入和裁剪矢量表的屬性表:

下面是Python源代碼
# ---------------------------------------------------------------------------
# Purpose : ClipRasterByFeature
# Author :gisweis
# Date :2015.7.21
# Version : ArcGIS 10.1
# Email :liweis2014@hotmail.com
# Notes :
# ---------------------------------------------------------------------------
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import arcpy
import string
try:
raster = arcpy.GetParameterAsText(0) #clip raster
clip_feat = arcpy.GetParameterAsText(1) #clip featureclass
field = arcpy.GetParameterAsText(2) #name field
outworkspace = arcpy.GetParameterAsText(3) #output ws
outtype = arcpy.GetParameterAsText(4) #output ws
total = int(arcpy.GetCount_management(clip_feat).getOutput(0))
count= 1
for row in arcpy.SearchCursor(clip_feat):
mask=row.getValue("Shape")
extent=str(mask.extent.XMin)+" " +str(mask.extent.YMin)+" " +str(mask.extent.XMax)+" " +str(mask.extent.YMax)
outPath=outworkspace+"\\"+str(row.getValue(field)+outtype)
arcpy.AddMessage("chipping: " + str(row.getValue(field)) + "...count:"+str(total)+"\\"+str(count))
arcpy.Clip_management(raster,extent,outPath,mask,"0","ClippingGeometry")
count=count+1
except arcpy.ExecuteError:
print arcpy.GetMessages()
