[ArcPy] 代码汇总


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, time
path = r'D:\可行性分析' #文件夹路径
res = 100
print '程序开始:' + 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 mxd
print '程序结束:' + 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'  

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

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

转载自:https://blog.csdn.net/summer_dew/article/details/78066561

You may also like...