凶契帽 发表于 2025-6-9 08:32:22

Arcpy使用入门

2.7Python(目前ArcGIS使用)代码转化为3.5Python(目前ArcGIS Pro使用)代码----Analyze Tools For Pro (2to3命令)
基本操作

调用ArcToolbox的两种形式
#arcpy.ToolboxAlias.ToolName()
#arcpy.ToolName_ToolboxAlias()
#ToolboxAlias工具箱别名
#ToolName工具名称
arcpy.analysis.Buffer(in_features="bjboundary", out_feature_, buffer_distance_or_field="0.5 Unknown", line_side="FULL", line_end_type="ROUND", dissolve_option="NONE", dissolve_field="", method="PLANAR")编写的一般流程
#默认只捕捉最严重的错误
try:
        arcpy.Buffer_analysis()
except arcpy.ExecuteError:
        print(arcpy.GetMessages())
#捕捉警告
try:
        arcpy.SetSeverityLevel(1)
        arcpy.Buffer_analysis()
except arcpy.ExecuteError:
        print(arcpy.GetMessages())环境(Environment)设置
arcpy.env.workspace = “c:/data“#设置工作空间
arcpy.env.extent  = arcpy.Extent(0,0,100,100)#设置工作范围
arcpy.env.overwriteOutput = True#覆盖输出空间数据的描述
desc = arcpy.Describe(r"F:\map\region.shp")#读取文件描述
print (desc.DataType)
print (desc.ShapeType)
print (desc.ShapeFieldName)
print (desc.spatialReference.name)判断文件是否存在
# Set the current workspace
arcpy.env.workspace = r"E:\map"
 # Check for existence of data before deleting
if arcpy.Exists("roadbuffer"):
    arcpy.Delete_management("roadbuffer")显式指定文件夹中的所有要素文件
env.workspace = "F:/map"
fcs = arcpy.ListFeatureClasses("*","polygon")#寻找所有面矢量文件
for fc in fcs:
    print fc.encode("utf-8")#含中文输出UTF-8显示矢量文件中所有字符串类型的字段名
fds = arcpy.ListFields ('F:\\map\\region.shp', "s*","")#以列表形式获取shp文件中以s开头的字段名
for fd in fds:#for循环输出字段名
    print fd.name
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: Arcpy使用入门