# Set the input workspacearcpy.env.workspace=arcpy.GetParameterAsText(0)# Set the clip featureclassclipFeatures=arcpy.GetParameterAsText(1)# Set the output workspaceoutWorkspace=arcpy.GetParameterAsText(2)# Set the XY toleranceclusterTolerance=arcpy.GetParameterAsText(3)
将以下错误处理语句和 ArcPy 的
ListFeatureClasses()
函数添加到脚本窗口中:
# Get a list of the featureclasses in the input folder
fcs = arcpy.ListFeatureClasses()
ListFeatureClasses()
函数用于返回当前工作空间中要素类名称的列表。如果未指定完整路径,该工作空间会定义数据的位置以及创建所有新数据的位置。该工作空间已被设置为第一个参数的值。
for
循环用于遍历列表中包含的各个要素类。
添加以下代码:
forfcinfcs:# Validate the new feature class name for the output workspace.featureClassName=arcpy.ValidateTableName(fc,outWorkspace)outFeatureClass=os.path.join(outWorkspace,featureClassName)# Clip each feature class in the list with the clip feature class.# Do not clip the clipFeatures, it may be in the same workspace.iffc!=os.path.basename(clipFeatures):arcpy.Clip_analysis(fc,clipFeatures,outFeatureClass,clusterTolerance)
当列表中不再有名称时,
for
循环结束。
ValidateTableName()
函数用于确保输出名称对输出空间有效。某些字符(如句点或虚线)在地理数据库中是不允许的,因此该方法返回的是用有效字符取代无效字符后的名称。此外,它返回的是唯一名称,因此不会覆盖任何现有数据。
"""----------------------------------------------------------------------------- Script Name: Clip Multiple Feature Classes Description: Clips one or more shapefiles from a folder and places the clipped feature classes into a geodatabase. Created By: Insert name here. Date: Insert date here.-----------------------------------------------------------------------------"""
保存脚本。
完整代码:
"""----------------------------------------------------------------------------- Script Name: Clip Multiple Feature Classes Description: Clips one or more shapefiles from a folder and places the clipped feature classes into a geodatabase. Created By: Insert name here. Date: Insert date here.-----------------------------------------------------------------------------"""# Import ArcPy site-package and os modulesimportarcpyimportos# Set the input workspacearcpy.env.workspace=arcpy.GetParameterAsText(0)# Set the clip featureclassclipFeatures=arcpy.GetParameterAsText(1)# Set the output workspaceoutWorkspace=arcpy.GetParameterAsText(2)# Set the XY toleranceclusterTolerance=arcpy.GetParameterAsText(3)try:# Get a list of the featureclasses in the input folderfcs=arcpy.ListFeatureClasses()forfcinfcs:# Validate the new feature class name for the output workspace.featureClassName=arcpy.ValidateTableName(fc,outWorkspace)outFeatureClass=os.path.join(outWorkspace,featureClassName)# Clip each feature class in the list with the clip feature class.# Do not clip the clipFeatures, it may be in the same workspace.iffc!=os.path.basename(clipFeatures):arcpy.Clip_analysis(fc,clipFeatures,outFeatureClass,clusterTolerance)exceptExceptionaserr:arcpy.AddError(err)printerr