找回密码
 立即注册
首页 业界区 业界 Arcpy使用入门

Arcpy使用入门

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

调用ArcToolbox的两种形式
  1. #arcpy.ToolboxAlias.ToolName()
  2. #arcpy.ToolName_ToolboxAlias()
  3. #ToolboxAlias工具箱别名
  4. #ToolName工具名称
  5. 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")
复制代码
编写的一般流程
  1. #默认只捕捉最严重的错误
  2. try:
  3.         arcpy.Buffer_analysis()
  4. except arcpy.ExecuteError:
  5.         print(arcpy.GetMessages())
  6. #捕捉警告
  7. try:
  8.         arcpy.SetSeverityLevel(1)
  9.         arcpy.Buffer_analysis()
  10. except arcpy.ExecuteError:
  11.         print(arcpy.GetMessages())
复制代码
环境(Environment)设置
  1. arcpy.env.workspace = “c:/data“#设置工作空间
  2. arcpy.env.extent  = arcpy.Extent(0,0,100,100)#设置工作范围
  3. arcpy.env.overwriteOutput = True#覆盖输出
复制代码
空间数据的描述
  1. desc = arcpy.Describe(r"F:\map\region.shp")#读取文件描述
  2. print (desc.DataType)
  3. print (desc.ShapeType)
  4. print (desc.ShapeFieldName)
  5. print (desc.spatialReference.name)
复制代码
判断文件是否存在
  1. # Set the current workspace
  2. arcpy.env.workspace = r"E:\map"
  3.  # Check for existence of data before deleting
  4. if arcpy.Exists("roadbuffer"):
  5.     arcpy.Delete_management("roadbuffer")
复制代码
显式指定文件夹中的所有要素文件
  1. env.workspace = "F:/map"
  2. fcs = arcpy.ListFeatureClasses("*","polygon")#寻找所有面矢量文件
  3. for fc in fcs:
  4.     print fc.encode("utf-8")#含中文输出UTF-8
复制代码
显示矢量文件中所有字符串类型的字段名
  1. fds = arcpy.ListFields ('F:\\map\\region.shp', "s*","")#以列表形式获取shp文件中以s开头的字段名
  2. for fd in fds:#for循环输出字段名
  3.     print fd.name
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册