前段时间一个同事问我GIS批量裁剪的工具,GIS自带了影像裁剪工具,通过掩膜工具提取,或者栅格处理中的裁剪工具,同样提供了批处理方法,但一个包含了50个记录的shp文件,想分别裁剪影像并输出为50个栅格数据的话可以通过迭代图层文件分别输出,这样当数量超过一定的时候解放双手,刷刷视频岂不美哉。下面附上代码:
import arcpy
import sys
reload(sys)
import os
sys.setdefaultencoding('utf -8')
fcdom = arcpy.GetParameterAsText(0)
fcshp = arcpy.GetParameterAsText(1)
outputpath = arcpy.GetParameterAsText(2)
list_date=[]
lyr=arcpy.mapping.Layer(fcshp)
ext=lyr.getExtent()
extstring="{0} {1} {2} {3}".format(ext.XMin,ext.YMin,ext.XMax,ext.YMax)
with arcpy.da.SearchCursor(fcshp,['SHAPE@','OID@']) as cursor:
for row in cursor:
list_date.append(row)
for each in list_date:
print("{0} is strar!".format(each[1]))
arcpy.Clip_management(fcdom, extstring, r"{0}\\{1}.tif".format(outputpath,each[1]), each[0], "256", "ClippingGeometry", "NO_MAINTAIN_EXTENT")
print("{0} is done!".format(each[1]))