不知道从哪儿总结起,就按时间顺序整理吧。
关键词:arcgis,python,批量,字段,地图发布
1.给不同的要素添加不同的批量字段(例如:给“阀门”要素添加“本点号”、“点类型”、“状态”等字段):
1.1直接在arcmap的python窗口中写入脚本逐个添加,一次性运行:
import arcpy,os
arcpy.AddField_management("阀门","本点号","TEXT","","","","","NULLABLE","NON_REQUIRED")
arcpy.AddField_management("阀门","点类型","TEXT","","","","","NULLABLE","NON_REQUIRED")
arcpy.AddField_management("阀门","状态","TEXT","","","","","NULLABLE","NON_REQUIRED")
1.2利用ARCToolbox工具,新建一个工具,设置好参数,然后导入脚本:
参数设置:
打开界面:
脚本:
#导入arcpy和os模块
import arcpy,os
layerList=arcpy.GetParameterAsText(0).split(";")
for lyr in layerList:
arcpy.AddField_management(lyr,"本点号","TEXT","","","","","NULLABLE","NON_REQUIRED")
arcpy.AddField_management(lyr,"点类型","TEXT","","","","","NULLABLE","NON_REQUIRED")
arcpy.AddField_management(lyr,"口径","TEXT","","","","","NULLABLE","NON_REQUIRED")
arcpy.AddField_management(lyr,"状态","TEXT","","","","","NULLABLE","NON_REQUIRED")
2.多要素添加相同的批量字段:
2.1利用ARCToolbox工具,新建一个工具,设置好参数,然后导入脚本:
参数设置:
打开界面:
脚本:
import arcpy,os
#定义变量
#对应的第一个“输入表”的参数,将多个图层用“;”分开
layerList=arcpy.GetParameterAsText(0).split(";")
#对应的第二个“字段名”的参数,后面的依次类推
fieldName=arcpy.GetParameterAsText(1)
fieldType=arcpy.GetParameterAsText(2)
fieldPrecision=arcpy.GetParameterAsText(3)
fieldScale=arcpy.GetParameterAsText(4)
fieldLength=arcpy.GetParameterAsText(5)
fieldAlias=arcpy.GetParameterAsText(6)
fieldIsNullable=arcpy.GetParameterAsText(7)
fieldIsRequired=arcpy.GetParameterAsText(8)
fieldDomain=arcpy.GetParameterAsText(9)
#遍历每一个表,将传入工具箱中有关字段的参数传入AddField_management()函数中
for lyr in layerList:
arcpy.AddField_management(lyr,fieldName,fieldType,fieldPrecision,fieldScale,fieldLength,fieldAlias,fieldIsNullable,fieldIsRequired,fieldDomain)
3.批量发布*.mxd地图文档(发布之前确保地图文档符合发布的要求):
3.1将要发布的批量地图文档放在同一文件夹下,然后在arcpy的窗口中导入脚本:
脚本:
import arcpy.mapping as mapping
import os
#地图文档存放的路径
folder=r‘C:\Users\46243\Desktop\PublishTest‘
#遍历每一个地图文档
files=os.listdir(folder)
for f in files:
service=f.replace(".mxd","")
mxdpath=os.path.join(folder,f)
sddraft=mxdpath.replace(".mxd",".sddraft")
mxd=mapping.MapDocument(mxdpath)
mapping.CreateMapSDDraft(mxd,sddraft,service)
analysis=mapping.AnalyzeForSD(sddraft)
sd=mxdpath.replace(".mxd",".sd")
insever="GIS 服务器/myServerConnection"
if analysis[‘errors‘]=={}:
arcpy.StageService_server(sddraft,sd)
arcpy.UploadServiceDefinition_server(sd,insever)
else:
print(analysis[‘errors‘])
(注意地图文档用英文命名,用中文会报错,应该是字符编码导致的问题)
3.2利用ARCToolbox工具,新建一个工具,设置好参数,然后导入脚本:
参数设置:
打开界面:
脚本:
import arcpy.mapping as mapping
import os
#传入变量
mxdPaths=arcpy.GetParameterAsText(0).split(";")
insever=arcpy.GetParameterAsText(1)
for mxdpath in mxdPaths:
sddraft=mxdpath.replace(".mxd",".sddraft")
f=mxdpath.split("\\")[-1]
service=f.replace(".mxd","")
mxd=mapping.MapDocument(mxdpath)
mapping.CreateMapSDDraft(mxd,sddraft,service)
analysis=mapping.AnalyzeForSD(sddraft)
sd=mxdpath.replace(".mxd",".sd")
if analysis[‘errors‘]=={}:
arcpy.StageService_server(sddraft,sd)
arcpy.UploadServiceDefinition_server(sd,insever)
else:
print(analysis[‘errors‘])
未完待续......
(模型构建器里的迭代器也是个实现批量处理的好东西,等下次再写这个东西。)