Arcpy MXD发布服务

通过给定地图文档 (MXD) 路径和 ArcGIS Server 连接文件路径发布一个新的地图服务。连接文件的扩展名为 .ags,每次使用 ArcMap 中的目录 窗口连接到
ArcGIS Server 站点时都会创建一个连接文件。本脚本不需要管理员或发布者令牌,因为它使用的是连接文件。

__author__ = 'Administrator'
# 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

    # Define local variables
    wrkspc = 'D:/ServerConnect'
    mapDoc = arcpy.mapping.MapDocument(wrkspc + '/MXD/Test.mxd')


    # Provide path to connection file
    # To create this file, right-click a folder in the Catalog window and
    #  click New > ArcGIS Server Connection
    con = wrkspc + '/arcgis on localhost_6080.ags'


    # Provide other service details
    service = 'Test'
    sddraft = wrkspc + service + '.sddraft'
    sd = wrkspc + service + '.sd'
    summary = 'General reference map of the Test'
    tags = 'Test'

    # Create service definition draft
    arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER', con, 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)
        print "Service successfully published"
    else:
        print "Service could not be published because errors were found during analysis."

    print arcpy.GetMessages()


转载自:https://blog.csdn.net/qingyimengwu/article/details/43482531

You may also like...