例: 公開時のデータ フォルダの登録

次の例では、プログラムを使用してデータ フォルダを 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()
5/20/2014