python 实现mxd文档发布地图服务

具体可以查看帮助文档
# -*- coding: cp936 -*-
# 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
# Provide path to connection file
wrkspc = 'E:/fishery project/测试/新版本/'
mapDoc = arcpy.mapping.MapDocument(wrkspc + 'temp_time.mxd')


# 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 = "http://47.98.229.222:6080/arcgis/admin"
staging_folder_path = wrkspc
username = "admin"
password = "1234"

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 = 'temp_time'
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()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 Python 发布 ArcGIS Server 服务,您可以使用 ArcPy 模块中的 PublishServiceDefinition 工具。以下是一个简单的代码示例: ```python import arcpy # 定义服务定义文件路径 sddraft = r"C:\path\to\service.sddraft" sd = r"C:\path\to\service.sd" # 定义服务名称、类型和端口号 serviceName = "MyService" serviceType = "MapServer" port = "6080" # 创建服务定义草稿 arcpy.mapping.CreateMapSDDraft("C:/data/World.mxd", sddraft, serviceName, serviceType, "ARCGIS_SERVER", "http://localhost:"+port, True, None, "MyFolder") # 读取服务定义草稿 doc = DOM.parse(sddraft) # 修改服务属性 tagsType = doc.getElementsByTagName('Type') for tagType in tagsType: if tagType.parentNode.tagName == 'SVCManifest': if tagType.hasChildNodes(): tagType.firstChild.data = serviceType tagsConfigProps = doc.getElementsByTagName('ConfigurationProperties')[0] tagsProps = tagsConfigProps.getElementsByTagName('PropertyArray')[0] tagsItems = tagsProps.getElementsByTagName('PropertySetProperty') for tagItem in tagsItems: if tagItem.firstChild.data == 'filePath': tagItem.lastChild.firstChild.data = r"C:\data\World.mxd" # 保存修改后的服务定义草稿 f = open(sddraft, 'w') doc.writexml(f) f.close() # 将服务定义文件发布服务 arcpy.StageService_server(sddraft, sd) arcpy.UploadServiceDefinition_server(sd, "My Hosted Services") ``` 此代码将以“ARCGIS_SERVER”类型为例,发布名为“MyService”的MapServer服务,端口号为6080,并将地图文档“World.mxd”作为服务的数据源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值