文章目录
系列文章目录: ArcGIS自定义脚本编程
一、功能介绍
二、脚本代码
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import arcpy
from arcpy import env
import os
import time
import sys
reload(sys)
sys.setdefaultencoding('utf8')
tifs = arcpy.GetParameterAsText(0)
masks = arcpy.GetParameterAsText(1)
out_dir = arcpy.GetParameterAsText(2)
tifs = tifs.split(";")
masks = masks.split(";")
names = [os.path.splitext(os.path.basename(mask))[0] for mask in masks]
size = len(tifs) * len(masks)
num = 1
for i, mask in enumerate(masks):
# create a new folder named by mask's name
new_folder = out_dir + os.sep + names[i]
if not os.path.exists(new_folder):
os.mkdir(new_folder)
else:
arcpy.AddMessage("Folder {0} already exists. Please check it.".format(new_folder))
for tif in tifs:
s = time.time()
cliped_tif = os.path.join(new_folder, "{0}_{1}".format(names[i],os.path.split(tif)[1]))
if not os.path.exists(cliped_tif):
arcpy.Clip_management(tif, "#", cliped_tif, mask, "0", "ClippingGeometry")
e = time.time()
arcpy.AddMessage("{0}/{1} | {2} Completed, time used {3}s".format(num, size, cliped_tif, e - s))
else:
e = time.time()
arcpy.AddMessage("{0}/{1} | {2} already exists.".format(num, size, cliped_tif))
num += 1