Skip To Content

Ejemplo: Publicar un servicio de mapas desde un documento de mapa (MXD)

En este ejemplo se publica un nuevo servicio de mapas dada la ruta de acceso a un documento de mapa (MXD) y la ruta de acceso a un archivo de conexión de ArcGIS Server. El archivo de conexión tiene una extensión. ags y se crea cuando se conecta a un sitio de ArcGIS Server utilizando la ventana Catálogo en ArcMap. Esta secuencia de comandos no requiere un administrador o editor token porque se utiliza el archivo de conexión.

Para ejecutar este ejemplo, deberá reemplazar las variables locales en la parte superior de la secuencia de comandos con las rutas de acceso a su propio documento de mapa y el archivo de conexión. También puede cambiar el nombre, el resumen y las etiquetas asociadas con el servicio.

# 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

# Define local variables
wrkspc = 'C:/data'
mapDoc = arcpy.mapping.MapDocument(wrkspc + '/USA/USA.mxd')

# Provide path to connection file
# To create this file, right-click a folder in the Catalog window and
#  click New > ArcGIS Server Connection
con = wrkspc + '/connections/arcgis on myserver_6080 (publisher).ags'

# Provide other service details
service = 'USA'
sddraft = wrkspc + service + '.sddraft'
sd = wrkspc + service + '.sd'
summary = 'General reference map of the USA'
tags = 'USA'

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

print arcpy.GetMessages()