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_class="G:/实习文件/map/buf_Boundary.shp", 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
posted @ 2023-12-04 12:16  疏长  阅读(84)  评论(0编辑  收藏  举报