不知道从哪儿总结起,就按时间顺序整理吧。
关键词: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:Users46243DesktopPublishTest'
#遍历该路径下的每一个地图文档
files=os.listdir(folder)
for f in files:
service=f.replace(".mxd","") #例地图文档的名称为:test.mxd;声明服务变量的名称为:test
mxdpath=os.path.join(folder,f) #声明地图文档的全路径变量名称为:r'C:Users46243DesktopPublishTest est.mxd'
sddraft=mxdpath.replace(".mxd",".sddraft") #声明草稿服务定义变量的名称为:r'C:Users46243DesktopPublishTest est.sddraft'
mxd=mapping.MapDocument(mxdpath) #传入地图文档的全路径,引用该文档,并把该引用赋值给mxd变量;不要含有中文,不然会报错
mapping.CreateMapSDDraft(mxd,sddraft,service) #函数CreateMapSDDraft读取地图文档(MXD),并创建草稿服务定义(.sddraft);服务定义草稿包含:地图文档、服务器信息、一组服务属性
analysis=mapping.AnalyzeForSD(sddraft) #函数 AnalyzeForSD分析草稿服务定义,找出可能会阻止发布的错误。此函数会返回一个Python列表,包含错误、警告、消息性信息
sd=mxdpath.replace(".mxd",".sd") #声明服务定义变量的名称为:r'C:Users46243DesktopPublishTest est.sd';过渡的服务定义(.sd)包含GIS服务所需的全部信息,包括必须复制到服务器的数据
insever="GIS 服务器/myServerConnection" #地图文档要发布至的ArcGIS Server的名称,位于目录框里“GIS服务器”列表下,可重命名
if analysis['errors']=={}:
arcpy.StageService_server(sddraft,sd) #函数StageService_server将服务定义草稿(.sddraft)转换成服务定义;sd为生成的服务定义,默认情况下跟草稿服务定义写入同一目录;过渡完成后,输入的服务定义草稿即被删除
arcpy.UploadServiceDefinition_server(sd,insever) #函数UploadServiceDefinition_server根据服务定义文件上传发布GIS服务至特定的服务器
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'])
未完待续......
(模型构建器里的迭代器也是个实现批量处理的好东西,等下次再写这个东西。)