TransWikia.com

Specifying map service parameters when publishing with ArcPy?

Geographic Information Systems Asked on November 26, 2021

I publish map services on a ArcGIS server using the typical arcpy script below. I need to better specify some service parameters. For example, the service should be available in WMS instead of KML, with anti-aliasing and some specific pooling parameters.

Is there a way to specify these parameters using some arcpy functions?

connection6546543 = 'C:/blabla/arcgis on myserver.eu (admin).ags'
folder1116 = "Myfolder"

def publishService(mxdPath, service_name, summary, tags):
    sddraft = 'C:/services/' + service_name + '.sddraft'
    if os.path.isfile(sddraft): os.remove(sddraft)

    # Create service definition draft
    map = arcpy.mapping.MapDocument(mxdPath)
    arcpy.mapping.CreateMapSDDraft(map, sddraft, service_name, 'ARCGIS_SERVER', connection6546543, True, folder1116, 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'] == {}:
        print 'Create service definition...'
        sd = 'E:/services/' + service_name + '.sd'
        if os.path.isfile(sd): os.remove(sd)
        arcpy.StageService_server(sddraft, sd)

        print 'Publish service...'
        arcpy.UploadServiceDefinition_server(sd, connection6546543)
        print "Service successfully published"
        print 'Clean sd'
        if os.path.isfile(sd): os.remove(sd)
    else: 
        print "Service publication failed: Errors during analysis."

    print arcpy.GetMessages()

    print 'Clean sddraft'
    if os.path.isfile(sddraft): os.remove(sddraft)

Detailed solution

The detailed solution I developed with the answers is the code snippet below. It has to be inserted between the draft service creation and draft service analysis. It modifies some parameters (UsageTimeout, KML off and WMS on, etc.) but can easily be adapted to others.

import xml.dom.minidom as DOM 

(...)

# change service parameters
doc = DOM.parse(sddraft)
keys = doc.getElementsByTagName('Key')
for key in keys:
    if key.firstChild.data == 'UsageTimeout': key.nextSibling.firstChild.data = 6000
    if key.firstChild.data == 'WaitTimeout': key.nextSibling.firstChild.data = 60
    if key.firstChild.data == 'IdleTimeout': key.nextSibling.firstChild.data = 20000
    if key.firstChild.data == 'MinInstances': key.nextSibling.firstChild.data = 1
    if key.firstChild.data == 'MaxInstances': key.nextSibling.firstChild.data = 1
services___ = doc.getElementsByTagName('TypeName')
for service__ in services___:
    if service__.firstChild.data == 'KmlServer':
        service__.parentNode.getElementsByTagName('Enabled')[0].firstChild.data = 'false'
    if service__.firstChild.data == 'WMSServer':
        service__.parentNode.getElementsByTagName('Enabled')[0].firstChild.data = 'true'

# save changes
if os.path.exists(sddraft): os.remove(sddraft)
f = open(sddraft,"w")
doc.writexml(f)
f.close()

3 Answers

if key.firstChild.data == 'recycleStartTime': key.nextSibling.firstChild.data = "11:00" is giving an error (NoneType). You have to:

if  key.nextSibling.firstChild == None:
    x= self.sddraft_document.createTextNode("12:34")  
    key.nextSibling.appendChild(x)

also needed for recycleInterval and proberly for some other.

Answered by Huub van Amelsvoort on November 26, 2021

In addition to modifying the values "after" you publish per Alex, you can modify the values before publishing by opening up the SDDraft and modifying the XML. (not using arcpy, but xml manipulation through built in Python libraries)

There are a ton of samples here at the bottom of the page. I dont think there is one that does exactly what you want, but its not a far leap to modify them to suit your needs.

Answered by KHibma on November 26, 2021

No, there are currently not. You cannot edit these default settings while publishing the service using ArcPy; you can do this only afterwards with pure Python. The workflow for this would consist of two steps.

  1. Publish a service with default settings using ArcPy.
  2. Modify the service properties using pure Python.

To modify the service properties, refer to the Esri Help which has some ready-to-use examples. An example that would change #instances would include this code snippet (see my comments here):

# Deserialize response into Python object
dataObj = json.loads(data)
httpConn.close()

# Edit desired properties of the service
dataObj["minInstancesPerNode"] = 1
dataObj["maxInstancesPerNode"] = 10

newdict = dataObj["properties"]
newdict["maxRecordCount"] = 5000

# Serialize back into JSON
updatedSvcJson = json.dumps(dataObj)

If you want to do this in one operation format, use pure Python then directly from the beginning: look for this example here (you would need to prepare a huge JSON file with all the settings you need forehand):

Example: Publish a service with detailed parameters

Answered by Alex Tereshenkov on November 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP