arcpy发布地图动态服务

目录

通过arcpy发布地图服务的思路

1. 调用arcpy.mapping中的CreateMapSDDraft()函数,将地图文档转换成服务定义草稿文件(.sddraft) 该文件由一个地图文档、服务器信息和一组服务属性组合而成。其中服务器信息包括服务器连接、 即将发布的服务类型、服务的元数据(项目信息)和数据参考(是否将数据复制到服务器),但是 服务定义草稿文件中不包含数据(不能单独用于发布服务)。CreateMapSDDraft()函数也会在 发布服务过程中生成包含错误和警告的python字典。
2. 调用StageServer Tool(.sd,过渡服务工具),编译能成功发布GIS资源所需的所有必要信息。
如果未将数据注册到服务器,将在过渡服务定义草稿时添加这些数据。
3. 使用Upload Service Definition Tool(上传服务定义工具)上传服务定义文件,并将其作为GIS服务 发布到指定的GIS服务器。该步骤获取服务定义文件,并将其复制到服务器中,提取所需信息并发布

代码

# -*- coding: utf-8 -*-

import arcpy, os, sys

# Modify as your workspace
##########################
workSpace = r"D:\testpublish"
gisServer = 'localhost'
gisPort = 6443
username = 'arcgis'
password = 'arcgis'
serverUrl = 'https://{0}:{1}/arcgis/admin'.format(gisServer, gisPort)
print(serverUrl)
outAGSName = 'arcgis_admin.ags'
###########################

# 创建连接ARCGIS Server服务器
def createAGSConnection(workSpace, outAGSName, serverUrl, username, password):
# Create ArcGIS Server Connection File
    path = workSpace + '\\' + outAGSName
    if os.path.exists(path):
        os.remove(path)
        print("Existing ArcGIS Server connection file deleted ... ")
    arcpy.mapping.CreateGISServerConnectionFile(connection_type="ADMINISTER_GIS_SERVICES",
                                                out_folder_path=workSpace,
                                                out_name=outAGSName,
                                                server_url=serverUrl,
                                                server_type="ARCGIS_SERVER",
                                                use_arcgis_desktop_staging_folder=False,
                                                staging_folder_path=workSpace,
                                                username=username,
                                                password=password,
                                                save_username_password=True)
    print('ArcGIS Server connection file created: {0}'.format(outAGSName))
    return


# 发布地图服务
def publishingMapService(workSpace, mxdFile, outAGSName):
    # define local variables
    mapDoc = arcpy.mapping.MapDocument(os.path.join(workSpace, mxdFile))
    print('Map Document: {0}'.format(mapDoc.filePath))
    # 服务名称为mxd的名称
    service = mxdFile.split(".")[0]
    sddraft = os.path.join(workSpace + '\\', service + '.sddraft')
    sd = os.path.join(workSpace + '\\', service + '.sd')
    summary = mapDoc.summary
    tags = mapDoc.tags

    # create service definition draft
    # test为需要发布的服务的目录
    analysis = arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER',
                                              outAGSName, True, "test", summary, tags)
    print('Service definition draft file created: {0}'.format(sddraft))

    # stage and upload the service if the sddraft analysis did not contain errors
    if analysis['errors'] == {}:
        if os.path.exists(sd):
            os.remove(sd)
            print("Existing service defination file deleted ... ")
        # Execute StageService
        arcpy.StageService_server(sddraft, sd)
        print('Service definition file created: {0}'.format(sd))
        # Execute UploadServiceDefinition
        print('Uploading service...')
        arcpy.UploadServiceDefinition_server(sd, outAGSName)
        print("Success!")
        # os.remove(sd)
        print ("sd file is deleteing.....")
    else:
        # if the sddraft analysis contained errors, display them
        print analysis['errors']


##### Main Script
if os.path.isdir(workSpace) == False:
    print "Not valid path..."
else:
    createAGSConnection(workSpace, outAGSName, serverUrl, username, password)
    files = os.listdir(workSpace)
    for f in files:
        if f.endswith(".mxd"):
            # mxdPath = os.path.join(workSpace, f)
            # 下面两句代码将中文解码,很重要不然发布中文的服务会有问题
            reload(sys)
            sys.setdefaultencoding('utf-8')

            print "publishing: " + f
            publishingMapService(workSpace, f, outAGSName)
        else:
            continue

上一篇:CF573D Bear and Cavalry


下一篇:【JAVA 学习笔记】HashMap 探究