ArcPy学习入门(二):创建Python脚本

本脚本提供了一种方法,可以批量裁剪指定文件夹内的多个要素类,并将裁剪后的要素类保存到地理数据库中。该脚本使用了ArcPy模块,能够接收输入工作空间、裁剪要素类、输出工作空间及XY容差等参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、创建新 Python 脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# coding=utf-8
"""-----------------------------------------------------------------------------
  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 modules
import arcpy
import os
 
# Set the input workspace
arcpy.env.workspace = arcpy.GetParameterAsText(0)
 
# Set the clip featureclass
clipFeatures = arcpy.GetParameterAsText(1)
 
# Set the output workspace
outWorkspace = arcpy.GetParameterAsText(2)
 
# Set the XY tolerance
clusterTolerance = arcpy.GetParameterAsText(3)
 
try:
    # Get a list of the featureclasses in the input folder
    fcs = arcpy.ListFeatureClasses()
 
    for fc in fcs:
        # 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.
        if fc != os.path.basename(clipFeatures):
            arcpy.Clip_analysis(fc, clipFeatures, outFeatureClass,
                                clusterTolerance)
 
except Exception as err:
    arcpy.AddError(err)
    print err

该脚本将采用以下四个参数,以便实现通用:

  • 用来定义待处理的那组要素类的输入工作空间

  • 通过裁剪工具对输入要素类进行裁剪的区域的要素类

  • 写入裁剪工具结果的输出工作空间

  • 裁剪工具使用的 XY 容差

         ValidateTableName() 函数用于确保输出名称对输出空间有效;os.path.basename()方法用于获取处理裁剪要素类的名称(不包含路径)。


     本文转自stock0991 51CTO博客,原文链接:http://blog.51cto.com/qing0991/1422785,如需转载请自行联系原作者









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值