Skip To Content

示例:在发布时注册数据文件夹

本示例说明如何以编程方式将数据文件夹注册到 ArcGIS Server。可以通过 ArcGIS REST API 注册文件夹和数据库,但通常情况下使用 arcpy 函数要更加简便。

在本例中,(由名为 wrkspc 的变量控制的)文件夹必须包含 MXD 和源数据。运行此脚本前,应确保 ArcGIS Server 帐户具有访问该文件夹的权限。该脚本调用 arcpy.ListDataStoreItems 来检查任何注册到 ArcGIS Server 的文件夹的路径是否与上述文件夹相同。如果未找到匹配项,该脚本将使用 arcpy.AddDataStoreItem 注册此文件夹。

该脚本的其余部分使用 arcpy 函数来分析地图并发布服务。

import arcpy
import os
arcpy.env.overwriteOutput = True

# Path to ArcGIS Server connection file
connPath = "C:/data/connections/myserver.ags"

# Folder containing data and MXD
wrkspc = "C:/data/Tulsa/"
mapName = "Parcels.mxd"

# Service metadata
service = "TulsaParcels"
summary = "Shows cadastral data in Tulsa"
tags = "Tulsa, cadastre, parcels"

# make sure the folder is registered with the server, if not, add it to the datastore
if wrkspc not in [i[2] for i in arcpy.ListDataStoreItems(connPath, 'FOLDER')]:
     # both the client and server paths are the same
     dsStatus = arcpy.AddDataStoreItem(connPath, "FOLDER", "Workspace for " + service, wrkspc, wrkspc)
     print "Data store : " + str(dsStatus)

# Provide other service details
mapDoc = arcpy.mapping.MapDocument(os.path.join(wrkspc, mapName))
sddraft = os.path.join(wrkspc, service + '.sddraft')
sd = os.path.join(wrkspc, service + '.sd')

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

print arcpy.GetMessages()