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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验