Exemple : inscrire un dossier de données à la publication
Cet exemple explique comment inscrire par programmation un dossier de données auprès d'ArcGIS Server. Il est possible d'inscrire des dossiers et des bases de données via l'API REST d'ArcGIS, mais il est généralement plus facile d'utiliser les fonctions arcpy.
Dans cet exemple, le dossier (référencé par la variable wrkspc) doit contenir les données source et MXD. Avant d'exécuter ce script, veillez à ce que le compte ArcGIS Server dispose d'autorisations d'accès au dossier. Le script appelle arcpy.ListDataStoreItems pour vérifier que les dossiers inscrits auprès d'ArcGIS Server présentent le même chemin que le dossier. Si aucune correspondance n'est trouvée, le script inscrit le dossier à l'aide d'arcpy.AddDataStoreItem.
Le reste du script utilise des fonctions arcpy pour analyser la carte et publier le service.
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()