Python脚本实现自动发布地图服务


 最近根据用户的需求写了这个功能。

这里写图片描述

 我从官方文档中扒下来的代码,因为示例中没有连接server的部分,所以自己又添加了该部分,大家可以直接拷贝到本地,或者参考官方文档自己再扩展一下(比如注册文件夹什么的)。
https://enterprise.arcgis.com/zh-cn/server/latest/administer/windows/example-publish-a-map-service-from-a-map-document-mxd-.htm

主要流程如下:

  1. 使用 CreateGISServerConnectionFile 连接server ;
  2. 使用 CreateMapSDDraft 将 地图文档(.mxd) 转换为服务定义草稿(.sddraft) ;
  3. 使用arcpy.StageService_server() 将 服务定义草稿(.sddraft) 转换为 服务定义(.sd)文件;
  4. 使用 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()

转载自:https://blog.csdn.net/smss007/article/details/79629501

You may also like...