Skip To Content

Ejemplo: Registrar una carpeta de datos en el momento de la publicación

Este ejemplo muestra cómo se registra mediante programación una carpeta de datos en ArcGIS Server. El registro de nuevas carpetas y bases de datos se puede realizar mediante la API REST de ArcGIS, pero normalmente es más sencillo con las funciones arcpy.

En este ejemplo, la carpeta (almacenada en la variable wrkspc) debe contener el MXD y los datos de origen. Antes de ejecutar esta secuencia de comandos, asegúrese de que la cuenta de ArcGIS Server tenga permisos sobre la carpeta. La secuencia de comandos llama a arcpy.ListDataStoreItems para comprobar si hay alguna carpeta registrada en ArcGIS Server con la misma ruta. Si no se encuentra ninguna coincidencia, la secuencia de comandos registra la carpeta con arcpy.AddDataStoreItem.

El resto de la secuencia de comandos utiliza las funciones arcpy para analizar el mapa y publicar el servicio.

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()