arcgis10.0及以上版本,使用arcpy加载在arctoolbox中批量添加同一图层

代码:

#coding=gbk
#author:杨勇
#qq:1874445373

#'''此脚本会报错,但是会正确的执行添加图层的操作。
#可以添加.lyr    .shp    geodatabase     唯独不能添加.dwg


import arcpy.mapping as mapping,os,arcpy,random,string
mxd_dir = arcpy.GetParameterAsText(0)		#MXD目录
add_layer = arcpy.GetParameterAsText(1)		#.lyr目录;eg:c\\yy\\yy.lyr    ;cad数据通过另存为lyr进行批量加载




word = 'new_mxd_y'
goal_path = os.path.join(mxd_dir,word)

x = random.randint(0,10000)
y = str(x)
goal_path_new = os.path.join(goal_path,y)
isExists = os.path.exists(goal_path)

if not isExists:
    os.mkdir(goal_path)
else:
    os.mkdir(goal_path_new)

for root,dirs,files in os.walk(r"%s"%mxd_dir):
	for name in files:
        	if name[-4:].lower() == '.mxd':
          		filename = os.path.join(root,name)
     			mxd =arcpy.mapping.MapDocument(r"%s"%filename)
    			df = arcpy.mapping.ListDataFrames(mxd)[0]
    			alayer = arcpy.mapping.Layer(r"%s"%add_layer)
    			mapping.AddLayer(df,alayer,"AUTO_ARRANGE")
    			if not isExists:
				    mxd.saveACopy(os.path.join(goal_path,name))
    			else:
    				mxd.saveACopy(os.path.join(goal_path_new,name))

转载自:https://blog.csdn.net/qq_35515661/article/details/80849135

You may also like...