Python指令碼實現自動釋出地圖服務
阿新 • • 發佈:2019-02-04
最近根據使用者的需求寫了這個功能。
主要流程如下:
- 使用 CreateGISServerConnectionFile 連線server ;
- 使用 CreateMapSDDraft 將 地圖文件(.mxd) 轉換為服務定義草稿(.sddraft) ;
- 使用arcpy.StageService_server() 將 服務定義草稿(.sddraft) 轉換為 服務定義(.sd)檔案;
- 使用 arcpy.UploadServiceDefinition_server() 將 服務定義 (.sd)檔案上傳到server。
P.S:
wrkspc儘量用英文路徑;
server_url 要書寫正確;
生成的sd檔案和sddraft檔案不會自動刪除。
# Publishes a service to machine myserver using USA.mxd
# A connection to ArcGIS Server must be established in the
# Catalog window of ArcMap before running this script
# Import arcpy module
import arcpy
# Define local variables
wrkspc = 'E:/demo/'
mapDoc = arcpy.mapping.MapDocument(wrkspc + '1.mxd' )
# Provide path to connection file
# To create this file, right-click a folder in the Catalog window and
# click New > ArcGIS Server Connection
out_folder_path = wrkspc
con_Filename = "connection.ags"
server_url = "https://sms.esrichina.com:6443/arcgis/admin"
staging_folder_path = wrkspc
username = "arcgis"
password = "Super123"
arcpy.mapping.CreateGISServerConnectionFile("ADMINISTER_GIS_SERVICES" ,
out_folder_path,
con_Filename,
server_url,
"ARCGIS_SERVER",
False,
staging_folder_path,
username,
password,
"SAVE_USERNAME")
# Provide other service details
serviceName = 'pythonTest'
sddraft = wrkspc + serviceName + '.sddraft'
sd = wrkspc + serviceName + '.sd'
summary = 'summary'
tags = 'tags'
# Create service definition draft
arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, serviceName, 'ARCGIS_SERVER', con_Filename, True, None, summary, tags)
# Analyze the service definition draft
analysis = arcpy.mapping.AnalyzeForSD(sddraft)
# Print errors, warnings, and messages returned from the analysis
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'] == {}:
# Execute StageService. This creates the service definition.
arcpy.StageService_server(sddraft, sd)
# Execute UploadServiceDefinition. This uploads the service definition and publishes the service.
arcpy.UploadServiceDefinition_server(sd, con_Filename)
print "Service successfully published"
else:
print "Service could not be published because errors were found during analysis."
print arcpy.GetMessages()