批量为shp文件加字段并赋值文件名

import sys, string, os, arcgisscripting


# Create the Geoprocessor object
gp = arcgisscripting.create()


# Load required toolboxes...
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
gp.OverWriteOutput = 1


# Local variables...
inputWorkspace=sys.argv[1]
gp.workspace=inputWorkspace
feaclasses=gp.ListFeatureClasses()
fea=feaclasses.Next()
i=0
while fea:
	try:
		gp.AddField_management(fea, "NAME", "TEXT", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
		fileName=os.path.split(fea)[1]
		gp.CalculateField_management(fea, "NAME", "\""+fileName[0:-4] +"\"", "VB", "")
		i=i+1
		gp.AddMessage("第"+str(i)+"个文件:"+fea+"操作成功")
		fea=feaclasses.Next()
	except:	
		gp.AddMessage(fea+"操作失败")
del fea
del	feaclasses

功能:可以为一个文件夹下的所有地图分幅文件加NAME字段,并赋值该Shp文件的名字

举例:比如你有500个地图分幅文件shp格式,你想为这500个文件统一加一个name字段,并赋值地图分幅号,如果一个一个操作比较费时,这时就体现出脚本的价值了,类似工作可参考此脚本

版本:arcgis9.3,用10.0之后的版本需要参考修改

原创转载请挂链接!!!!!!!!!!!!!!

转载自:https://blog.csdn.net/yh0503/article/details/26095839

You may also like...

退出移动版