arcpy批处理示例

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

  1. import arcpy
  2. #读取文件
  3. arcpy.env.workspace = r"C:\Users\Administrator\Desktop\H48G026039.gdb"
  4. #取出要素类
  5. fcs = arcpy.ListFeatureClasses()
  6. fcCount = len(fcs)
  7. for fc in fcs: #遍历要素类
  8. arcpy.RepairGeometry_management(fc)
  9. print fc
  10. print fcCount

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

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

地理处理工具

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

  1. import sys
  2. reload(sys)
  3. sys.setdefaultencoding('utf-8') #设置编码
  4. import arcpy
  5. #获取工作空间
  6. path = arcpy.GetParameter(0) #获取参数
  7. arcpy.env.workspace = path
  8. #获取该目录下的要素集
  9. fcs = arcpy.ListFeatureClasses()
  10. fcCount = len(fcs)
  11. for fc in fcs: #遍历要素集
  12. #设置过程中提示语
  13. arcpy.SetProgressorLabel("修复要素类:" + fc +"...")
  14. #要素修复
  15. arcpy.RepairGeometry_management(fc)
  16. print fc
  17. 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

  1. import time,os
  2. import arcpy
  3. print '程序开始:' + str(time.ctime())
  4. arcpy.env.workspace = r"C:\Users\ljb\Desktop\高程数据"
  5. #输入要素
  6. inFeatures = "TERLK_LN.shp"
  7. #添加一个字段用来标记高程是否可以整除10
  8. fieldName1 = "Mark"
  9. fieldPrecision = 2
  10. fieldAlias = "整除10标记"
  11. #列出所有字段
  12. fieldObjList = arcpy.ListFields(inFeatures)
  13. # Create an empty list that will be populated with field names
  14. fieldNameList = []
  15. # For each field in the object list, add the field name to the
  16. # name list. If the field is required, exclude it, to prevent errors
  17. for field in fieldObjList:
  18. if not field.required:
  19. fieldNameList.append(field.name)
  20. print fieldNameList
  21. if fieldName1 in fieldNameList:
  22. arcpy.DeleteField_management(inFeatures, fieldName1)
  23. print"删除已有字段"
  24. #添加字段函数
  25. arcpy.AddField_management(inFeatures, fieldName1, "LONG", fieldPrecision, "", "",
  26. fieldAlias, "NULLABLE")
  27. field1 = "Elev"
  28. field2 = "Mark"
  29. #更新查询
  30. cursor = arcpy.UpdateCursor(inFeatures)
  31. for row in cursor:
  32. # field2 will be equal to field1 multiplied by 3.0
  33. if((int)(row.getValue(field1))%10 == 0):
  34. row.setValue(field2, 1)
  35. else:
  36. row.setValue(field2, 0)
  37. cursor.updateRow(row)
  38. print '程序结束:' + str(time.ctime())

复制数据库

  1. import arcpy
  2. from arcpy import env
  3. import os
  4. # Allow for the overwriting of file geodatabases, if they already exist #
  5. env.overwriteOutput = True
  6. # Set workspace to folder containing personal geodatabases #
  7. env.workspace = arcpy.GetParameterAsText(0)
  8. # Identify personal geodatabases #
  9. for pgdb in arcpy.ListWorkspaces("*", "FileGDB"):
  10. # Set workspace to current personal geodatabase#
  11. print pgdb
  12. env.workspace = pgdb
  13. # Create file geodatabase based on personal geodatabase#
  14. fgdb = pgdb[:-4] + "2.gdb"
  15. arcpy.CreateFileGDB_management(os.path.dirname(fgdb), os.path.basename(fgdb))
  16. # Identify feature classes and copy to file gdb #
  17. for fc in arcpy.ListFeatureClasses():
  18. print "Copying feature class " + fc + " to " + fgdb
  19. arcpy.Copy_management(fc, fgdb + os.sep + fc)
  20. # Identify tables and copy to file gdb #
  21. for table in arcpy.ListTables():
  22. print "Copying table " + table + " to " + fgdb
  23. arcpy.Copy_management(table, fgdb + os.sep + table)
  24. # Identify datasets and copy to file gdb
  25. # Copy will include contents of datasets#
  26. for dataset in arcpy.ListDatasets():
  27. print "Copying dataset " + dataset + " to " + fgdb
  28. arcpy.Copy_management(dataset, fgdb + os.sep + dataset)

处理Excel数据生成多边形

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

  1. import xlrd
  2. import xlwt
  3. import arcpy
  4. xlsPath = r"C:\Users\Administrator\Desktop\Polygon.xls"
  5. data = xlrd.open_workbook(xlsPath)
  6. table = data.sheets()[0]#通过索引顺序获取
  7. cols = table.col_values(5)
  8. nrows = table.nrows
  9. point = arcpy.Point()
  10. array = arcpy.Array()
  11. polygonGeometryList = []
  12. for i in range(1,nrows):
  13. str = table.cell(i,5).value
  14. points = str.split(u';')
  15. for j in points:
  16. xy = j.split(',')
  17. print xy[0]
  18. print xy[1]
  19. print '\n'
  20. point.X = float(xy[0]);point.Y = float(xy[1])
  21. array.add(point)
  22. polygon = arcpy.Polygon(array)
  23. polygonGeometryList.append(polygon)
  24. array.removeAll()
  25. arcpy.CopyFeatures_management(polygonGeometryList, "d:\\polygon.shp")
  26. print 'over'

批量将hdf数据转换成tif数据

  1. arcpy.env.overwriteOupt = 1
  2. arcpy.CheckOutExtension("Spatial")
  3. inPath = r"D:/HDF/"
  4. outPath = r"D:/Output"
  5. env.workspace = inPath
  6. arcpy.env.scratchWorkspace = inPath
  7. hdfList = arcpy.ListRaster('*', 'HDF')
  8. for hdf in hdfList:
  9. name = hdf[8:16] + ".tif"
  10. data = arcpy.ExtractSubDataset_management(hdf, name, "1")