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

一、功能介绍功能介绍
对栅格裁剪工具的介绍,可以查看这篇博客:常用的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)

三、工具参数
3-13-2

四、工具界面4-1

  • 8
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Salierib

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值