通過ArcPy編寫代碼實現自動發佈ArcGIS地圖服務

一.條件:

ArcGIS Desktop10.4/ArcGIS Server10.4

二.步驟

1.通過arcmap將矢量文件保存爲mxd文件;

2.創建.ags文件

創建完之後在connetions文件夾下就有了

3.執行代碼

import arcpy 
#定義一個局部變量
wrkspc="E:/data/mxd/"
#引入mxd文件路徑
mapDoc = arcpy.mapping.MapDocument(wrkspc + 'fw.mxd')
#提供一個ArcGIS Server連接文件路徑
con = wrkspc+'/connections/arcgis on localhost_6080 (admin).ags'
#設置服務名及其他細節
service = 'FW'
sddraft = wrkspc + service + '.sddraft'
sd = wrkspc + service + '.sd'
summary = 'fw'
tags = 'fw'
#創建一個服務定義草稿 服務定義 (.sd) 可將服務封裝成單個可轉移文件,選擇性地包含源 GIS 數據。
arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER', con, True, None, summary, tags)
#分析服務定義草稿
analysis = arcpy.mapping.AnalyzeForSD(sddraft)
print "The following information was returned during analysis of the MXD:"
for key in ('messages', 'warnings', 'errors'):
  print '----' + key.upper() + '---'
  vars = analysis[key]
  for ((message, code), layerlist) in vars.iteritems():
    print '    ', message, ' (CODE %i)' % code
    print '       applies to:',
    for layer in layerlist:
        print layer.name,
    print
#Stage and upload the service if the sddraft analysis did not contain errors
if analysis['errors'] == {}:
    # 執行StageService.創建一個服務定義。
    arcpy.StageService_server(sddraft, sd)
    #上傳服務定義工具會將服務定義上傳到服務器並進行發佈
    arcpy.UploadServiceDefinition_server(sd, con)
    print "Service successfully published"
else: 
    print "Service could not be published because errors were found during analysis."
print arcpy.GetMessages()

4.效果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章