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 fcprint 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': #文件名后三位为mxdmxd = 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 sysreload(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 fcprint 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,osimport arcpyprint '程序开始:' + str(time.ctime())arcpy.env.workspace = r"C:\Users\ljb\Desktop\高程数据"#输入要素inFeatures = "TERLK_LN.shp"#添加一个字段用来标记高程是否可以整除10fieldName1 = "Mark"fieldPrecision = 2fieldAlias = "整除10标记"#列出所有字段fieldObjList = arcpy.ListFields(inFeatures)# Create an empty list that will be populated with field namesfieldNameList = []# For each field in the object list, add the field name to the# name list. If the field is required, exclude it, to prevent errorsfor field in fieldObjList:if not field.required:fieldNameList.append(field.name)print fieldNameListif 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.0if((int)(row.getValue(field1))%10 == 0):row.setValue(field2, 1)else:row.setValue(field2, 0)cursor.updateRow(row)print '程序结束:' + str(time.ctime())
复制数据库
import arcpyfrom arcpy import envimport 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 pgdbenv.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 " + fgdbarcpy.Copy_management(fc, fgdb + os.sep + fc)# Identify tables and copy to file gdb #for table in arcpy.ListTables():print "Copying table " + table + " to " + fgdbarcpy.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 " + fgdbarcpy.Copy_management(dataset, fgdb + os.sep + dataset)
处理Excel数据生成多边形
Python对Excel读写xlrd库
原文:http://blog.csdn.net/sprintwater/article/details/40052675
import xlrdimport xlwtimport arcpyxlsPath = r"C:\Users\Administrator\Desktop\Polygon.xls"data = xlrd.open_workbook(xlsPath)table = data.sheets()[0]#通过索引顺序获取cols = table.col_values(5)nrows = table.nrowspoint = arcpy.Point()array = arcpy.Array()polygonGeometryList = []for i in range(1,nrows):str = table.cell(i,5).valuepoints = 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'
批量将hdf数据转换成tif数据
arcpy.env.overwriteOupt = 1arcpy.CheckOutExtension("Spatial")inPath = r"D:/HDF/"outPath = r"D:/Output"env.workspace = inPatharcpy.env.scratchWorkspace = inPathhdfList = arcpy.ListRaster('*', 'HDF')for hdf in hdfList:name = hdf[8:16] + ".tif"data = arcpy.ExtractSubDataset_management(hdf, name, "1")
