[ArcPy] 初识ArcPy

来源:互联网 发布:discuz怎么调用数据 编辑:程序博客网 时间:2024/05/22 14:14

  • ArcPy
  • 使用ArcPy的方法
  • arcpy批处理示例
  • 地理处理工具
  • 工具输入框内自动选择图层和数据
  • 读取XML文件
  • 要素属性操作
  • 复制数据库
  • 处理Excel数据生成多边形

ArcPy

ArcPy是Python操作ArcGIS的一个库
弥补ArcGIS Desktop对工作空间(数据库或者文件目录)直接处理和批处理的功能性不足

使用ArcPy的方法

  1. ArcGIS Desktop自带Python开发环境
    IDLE
    例子
  2. ArcMap中Python命令行
    ArcMap中Python命令行
  3. 脚本文件
    编写python代码,修改后缀为.py,双击运行

arcpy批处理示例

示例1:对一个gdb数据库进行了遍历,并对遍历的每一个要素类图层进行要素修复

import arcpy  #读取文件arcpy.env.workspace = r"C:\Users\Administrator\Desktop\H48G026039.gdb"  #取出要素类fcs = arcpy.ListFeatureClasses()  fcCount = len(fcs)  for fc in fcs: #遍历要素类       arcpy.RepairGeometry_management(fc)    print fc  print fcCount

示例2:对一个文件夹下的mxd文件进行遍历,并导出为jpg文件

import arcpy, os, timepath = r'D:\可行性分析' #文件夹路径res = 100print '程序开始:' + str(time.ctime())for afile in os.listdir(path): #遍历文件夹里的文件  if afile[-3:].lower() == 'mxd': #文件名后三位为mxd    mxd = arcpy.mapping.MapDocument(os.path.join(path,afile))    arcpy.mapping.ExportToJPEG(mxd, os.path.join(path,afile[:-3] + 'jpg'), resolution = res)    del mxdprint '程序结束:' + str(time.ctime())

地理处理工具

编写.py脚本,并新建工具箱后即可使用
原文:http://blog.csdn.net/sprintwater/article/details/41078225

import sys   reload(sys)   sys.setdefaultencoding('utf-8') #设置编码   import arcpy#获取工作空间path = arcpy.GetParameter(0) #获取参数arcpy.env.workspace = path #获取该目录下的要素集fcs = arcpy.ListFeatureClasses() fcCount = len(fcs) for fc in fcs: #遍历要素集   #设置过程中提示语   arcpy.SetProgressorLabel("修复要素类:" + fc +"...")     #要素修复   arcpy.RepairGeometry_management(fc)   print fc print fcCount

工具输入框内自动选择图层和数据

http://blog.csdn.net/sprintwater/article/details/41146543

读取XML文件

http://blog.csdn.net/sprintwater/article/details/46851643

要素属性操作

ArcPy对几何对象属性的操作,包括查询、添加、更改、删除
原文:http://blog.csdn.net/sprintwater/article/details/48858427

import time,os  import arcpy  print '程序开始:' + str(time.ctime())  arcpy.env.workspace = r"C:\Users\ljb\Desktop\高程数据"  #输入要素  inFeatures = "TERLK_LN.shp"  #添加一个字段用来标记高程是否可以整除10  fieldName1 = "Mark"  fieldPrecision = 2  fieldAlias = "整除10标记"  #列出所有字段  fieldObjList = arcpy.ListFields(inFeatures)  # Create an empty list that will be populated with field names          fieldNameList = []  # For each field in the object list, add the field name to the  #  name list.  If the field is required, exclude it, to prevent errors  for field in fieldObjList:      if not field.required:          fieldNameList.append(field.name)  print fieldNameList  if fieldName1 in fieldNameList:      arcpy.DeleteField_management(inFeatures, fieldName1)      print"删除已有字段"  #添加字段函数  arcpy.AddField_management(inFeatures, fieldName1, "LONG", fieldPrecision, "", "",                            fieldAlias, "NULLABLE")  field1 = "Elev"  field2 = "Mark"  #更新查询  cursor = arcpy.UpdateCursor(inFeatures)  for row in cursor:      # field2 will be equal to field1 multiplied by 3.0      if((int)(row.getValue(field1))%10 == 0):          row.setValue(field2, 1)      else:          row.setValue(field2, 0)      cursor.updateRow(row)  print '程序结束:' + str(time.ctime())  

复制数据库

import arcpy  from arcpy import env  import os  # Allow for the overwriting of file geodatabases, if they already exist #  env.overwriteOutput = True  # Set workspace to folder containing personal geodatabases #  env.workspace = arcpy.GetParameterAsText(0)  # Identify personal geodatabases #  for pgdb in arcpy.ListWorkspaces("*", "FileGDB"):      # Set workspace to current personal geodatabase#      print pgdb      env.workspace = pgdb      # Create file geodatabase based on personal geodatabase#      fgdb = pgdb[:-4] + "2.gdb"      arcpy.CreateFileGDB_management(os.path.dirname(fgdb), os.path.basename(fgdb))      # Identify feature classes and copy to file gdb     #      for fc in arcpy.ListFeatureClasses():          print "Copying feature class " + fc + " to " + fgdb          arcpy.Copy_management(fc, fgdb + os.sep + fc)      # Identify tables and copy to file gdb #      for table in arcpy.ListTables():          print "Copying table " + table + " to " + fgdb          arcpy.Copy_management(table, fgdb + os.sep + table)      # Identify datasets and copy to file gdb      # Copy will include contents of datasets#      for dataset in arcpy.ListDatasets():          print "Copying dataset " + dataset + " to " + fgdb          arcpy.Copy_management(dataset, fgdb + os.sep + dataset)

处理Excel数据生成多边形

Python对Excel读写xlrd库
原文:http://blog.csdn.net/sprintwater/article/details/40052675

import xlrd  import xlwt  import arcpy  xlsPath = r"C:\Users\Administrator\Desktop\Polygon.xls"  data = xlrd.open_workbook(xlsPath)  table = data.sheets()[0]#通过索引顺序获取  cols = table.col_values(5)  nrows = table.nrows  point = arcpy.Point()  array = arcpy.Array()  polygonGeometryList = []  for i in range(1,nrows):      str = table.cell(i,5).value      points = str.split(u';')      for j in points:          xy = j.split(',')          print xy[0]          print xy[1]          print '\n'          point.X = float(xy[0]);point.Y = float(xy[1])          array.add(point)      polygon = arcpy.Polygon(array)      polygonGeometryList.append(polygon)      array.removeAll()  arcpy.CopyFeatures_management(polygonGeometryList, "d:\\polygon.shp")  print 'over'  
原创粉丝点击