不知道從哪兒總結起,就按時間順序整理吧。python
關鍵詞:arcgis,python,批量,字段,地圖發佈服務器
1.給不一樣的要素添加不一樣的批量字段(例如:給「閥門」要素添加「本點號」、「點類型」、「狀態」等字段):app
1.1直接在arcmap的python窗口中寫入腳本逐個添加,一次性運行:函數
import arcpy,os工具
arcpy.AddField_management("閥門","本點號","TEXT","","","","","NULLABLE","NON_REQUIRED")ui
arcpy.AddField_management("閥門","點類型","TEXT","","","","","NULLABLE","NON_REQUIRED")編碼
arcpy.AddField_management("閥門","狀態","TEXT","","","","","NULLABLE","NON_REQUIRED")spa
1.2利用ARCToolbox工具,新建一個工具,設置好參數,而後導入腳本:server
參數設置:ci
打開界面:
腳本:
#導入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","") #例地圖文檔的名稱爲:test.mxd;聲明服務變量的名稱爲:test
mxdpath=os.path.join(folder,f) #聲明地圖文檔的全路徑變量名稱爲:r'C:\Users\46243\Desktop\PublishTest\test.mxd'
sddraft=mxdpath.replace(".mxd",".sddraft") #聲明草稿服務定義變量的名稱爲:r'C:\Users\46243\Desktop\PublishTest\test.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:\Users\46243\Desktop\PublishTest\test.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'])
未完待續......
(模型構建器裏的迭代器也是個實現批量處理的好東西,等下次再寫這個東西。)