Saturday, 17 December 2016

Overwrite existing map service in ArcGIS Server through ArcPy?


I am using ArcGIS Desktop and Server 10.1. I want to over write existing map service though ArcPy and going through this link and getting an errorRuntime error Traceback (most recent call last): File "", line 22, in NameError: name 'draftPath' is not defined and I am searching for this at my end.


I just want to know is there any alternative to overwrite map service automatically after 1 hour in ArcGIS Server 10.1 ?



Answer



Here is the sample code which customized from esri sample. This sample follows 3 steps,



  1. Create SDDraft from mxd document


  2. Set service type to esriServiceDefinitionType_Replacement of SDDraft

  3. Stage and upload the sddraft with overwriting existed service.


import arcpy
import xml.dom.minidom as DOM

#define local variables
# wrkspc mxd document directory
# mxdName mxd document name
# con ArcGIS Server Catalog path

# service service name (include service direcotry)
# summary service summary
# tags services tags

wrkspc = 'C:/test/'
mxdName = 'sample.mxd'
con = 'GIS Servers/arcgis on localhost_6080 (admin)'
service = 'MyMapService'
summary = 'Population Density by County'
tags = 'county, counties, population, density, census'


mapDoc = arcpy.mapping.MapDocument(wrkspc + mxdName)
sddraft = wrkspc + service + '.sddraft'
sd = wrkspc + service + '.sd'

# create service definition draft
analysis = arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER',
con, True, None, summary, tags)

# set service type to esriServiceDefinitionType_Replacement

newType = 'esriServiceDefinitionType_Replacement'
xml = sddraft
doc = DOM.parse(xml)
descriptions = doc.getElementsByTagName('Type')
for desc in descriptions:
if desc.parentNode.tagName == 'SVCManifest':
if desc.hasChildNodes():
desc.firstChild.data = newType
outXml = xml
f = open(outXml, 'w')

doc.writexml( f )
f.close()

# stage and upload the service if the sddraft analysis did not contain errors
if analysis['errors'] == {}:
# Execute StageService
arcpy.StageService_server(sddraft, sd)
# Execute UploadServiceDefinition
arcpy.UploadServiceDefinition_server(sd, con)
else:

# if the sddraft analysis contained errors, display them
print analysis['errors']

But make sure that if you are going to overwrite the service within directory such as "Maps/MyMapService", then you need to create "Maps" directory to mxd directory before run script.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...