一、功能介绍
对栅格裁剪工具的介绍,可以查看这篇博客:常用的Arcmap内置工具(一)
二、脚本代码
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import arcpy
from arcpy import env
raster_path = arcpy.GetParameterAsText(0)
mask = arcpy.GetParameterAsText(1)
d_dir_name = arcpy.GetParameterAsText(2)
def clip_batch(new_dir_name, old_prefix="Pr_", new_prefix="Clip_"):
# type: () -> object
env.workspace = raster_path
raster_List = arcpy.ListRasters("*", "tif")
for raster in raster_List:
if raster[:3] != old_prefix:
out = "\\" + new_dir_name + "\\" + new_prefix + raster[:]
elif raster[:3] == old_prefix:
out = "\\" + new_dir_name + "\\" + new_prefix + raster[3:]
arcpy.Clip_management(raster, "#", out, mask, "#", "ClippingGeometry")
arcpy.AddMessage("Step2:Clip_"+raster[:]+"has done.")
arcpy.SetProgressorPosition()
arcpy.AddMessage("Step2:Completed")
arcpy.ResetProgressor()
dir_name = d_dir_name
out_path = raster_path + "\\" + dir_name
ex = [2]
while True:
if os.path.exists(out_path):
dir_name = dir_name + "(" + str(ex[-1]) + ")"
out_path = raster_path + "\\" + dir_name
ex.append(ex[-1] + 1)
continue
else:
break
os.makedirs(out_path)
arcpy.AddMessage("Step1:Creating new folder named " + str(dir_name))
arcpy.AddMessage("Step1:Completed")
clip_batch(dir_name)
三、工具参数
四、工具界面