【ArcGIS自定义脚本工具】批量裁剪栅格脚本

一、功能介绍【ArcGIS自定义脚本工具】批量裁剪栅格脚本_第1张图片
对栅格裁剪工具的介绍,可以查看这篇博客:常用的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, "0", "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)

三、工具参数
【ArcGIS自定义脚本工具】批量裁剪栅格脚本_第2张图片【ArcGIS自定义脚本工具】批量裁剪栅格脚本_第3张图片

四、工具界面【ArcGIS自定义脚本工具】批量裁剪栅格脚本_第4张图片

你可能感兴趣的:(ArcGIS自定义脚本编程,ArcGIS,自定义脚本,批量裁剪栅格)