arcpy发布要素服务

目录

思路

  1. 通过arcgis server发布,连接arcgis server
  2. 发布要素服务,需要先创建草稿文件
  3. 创建草稿文件之后,需要修改草稿文件的配置,将要素服务开启以及去掉创建功能
  4. 分析草稿文件
  5. 编译资源
  6. 发布服务

代码

# -*- coding: utf-8 -*-
import arcpy, sys
import xml.dom.minidom as DOM
import os

# batch publishing feature service

# 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'
###########################

# Create ArcGIS Server Connection File Function
# 创建连接GIS服务器
def createAGSConnection(workSpace, outAGSName, serverUrl, username, password):
    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


# Publishing Feature Servcie
# 发布要素服务
def publishingFeatureService(workSpace, mxdFile, outAGSName):
    # define local variables
    mapDoc = arcpy.mapping.MapDocument(os.path.join(workSpace, mxdFile))
    print('Map Document: {0}'.format(mapDoc.filePath))
    service = mxdFile.split(".")[0]
    sddraft = os.path.join(workSpace + '\\', service + '.sddraft')
    sd = os.path.join(workSpace + '\\', service + '.sd')
    summary = mapDoc.summary
    tags = mapDoc.tags
    # 创建一个修改要发布要素服务的sddraft文件
    newSDdraft = os.path.join(workSpace + '\\', service + 'FS.sddraft')

    # create service definition draft
    analysis = arcpy.mapping.CreateMapSDDraft(map_document=mapDoc,
                                              out_sddraft=sddraft,
                                              service_name=service,
                                              server_type='ARCGIS_SERVER',
                                              connection_file_path=outAGSName,
                                              copy_data_to_server=False,
                                              folder_name='test',
                                              summary=summary,
                                              tags=tags)

    # Modify the SDDraft from a MapService to a FeatureService.
    # 修改sddraft文件
    doc = DOM.parse(sddraft)
    tagsType = doc.getElementsByTagName('Type')
    for tagType in tagsType:
        if tagType.parentNode.tagName == 'SVCManifest':
            if tagType.hasChildNodes():
                tagType.firstChild.data = "esriServiceDefinitionType_Replacement"
    tagsState = doc.getElementsByTagName('State')
    for tagState in tagsState:
        if tagState.parentNode.tagName == 'SVCManifest':
            if tagState.hasChildNodes():
                tagState.firstChild.data = "esriSDState_Published"

    # Turn on feature access capabilities
    services___ = doc.getElementsByTagName('TypeName')
    for service__ in services___:
        if service__.firstChild.data == "FeatureServer":
            service__.parentNode.getElementsByTagName('Enabled')[0].firstChild.data = 'true'

    # 修改功能,将create功能去掉,不去掉的话可能会报错
    typeNames = doc.getElementsByTagName('TypeName')
    for typeName in typeNames:
        if typeName.firstChild.data == 'FeatureServer':
            typeName.parentNode.getElementsByTagName('Value')[1].firstChild.data = 'Query,Update,Delete,Uploads,Editing'
            print typeName.parentNode.getElementsByTagName('Value')[1].firstChild.data

    # Write the new draft to disk
    # 将读取出来的doc文档写入到newSddraft文件中
    f = open(newSDdraft, 'w')
    doc.writexml(f)
    f.close()
    # Analyze the service
    analysis = arcpy.mapping.AnalyzeForSD(newSDdraft)
    if analysis['errors'] == {}:
        # Stage the service
        if os.path.exists(sd):
            os.remove(sd)
            print("{0} already exists!  Deleted! ".format(sd))
        arcpy.StageService_server(newSDdraft, sd)
        # Upload the service.
        # arcpy.UploadServiceDefinition_server(sd, outAGSName, service)
        arcpy.UploadServiceDefinition_server(sd, outAGSName)

        print "Uploaded and overwrote feature service success"
    else:
        print '----------------------'
        # If the sddraft analysis contained errors, display them and quit.
        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
            publishingFeatureService(workSpace, f, outAGSName)
        else:
            continue

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值