How to modify the setting of python to publish arcgis server service to pit sawtooth effect

A little friend asked such a question before, when I use python to automatically publish services, how do I use the xml parameters of the slice scheme controlled by python? ?

The following is some official help I collected when I collected and published services before, I hope to inspire you a little bit;

Refer to the official website CreateMapSDDraft

The following script modifies the transition setting TextAntialiasingMode element using the xml.dom.minidom standard python library. The modified service definition draft (.sddraft) file is then saved to a new file. Finally, use the AnalyzeForSD function to analyze the new .sddraft file for errors.

import arcpy
import xml.dom.minidom as DOM 

# Reference map document for CreateSDDraft function.
mapDoc = arcpy.mapping.MapDocument('C:/project/counties.mxd') 
# Create service and sddraft variables for CreateSDDraft function.
service = 'Counties'
sddraft = 'C:/Project/' + service + r'.sddraft'
  
# Create sddraft.
arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER')

# These are the properties we will change in the sddraft xml.
soe = 'WMSServer'
soeProperty = 'title'
soePropertyValue = 'USACounties'

# Read the sddraft xml.
doc = DOM.parse(sddraft)
# Find all elements named TypeName. This is where the server object extension (SOE) names are defined.
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
    # Get the TypeName whose properties we want to modify.
    if typeName.firstChild.data == soe:
        extension = typeName.parentNode
        for extElement in extension.childNodes:
            # Enabled SOE.
            if extElement.tagName == 'Enabled':
                extElement.firstChild.data = 'true'
            # Modify SOE property. We have to drill down to the relevant property.
            if extElement.tagName == 'Props':
                for propArray in extElement.childNodes:
                    for propSet in propArray.childNodes:
                        for prop in propSet.childNodes:
                            if prop.tagName == "Key":
                                if prop.firstChild.data == soeProperty:
                                    if prop.nextSibling.hasChildNodes():
                                        prop.nextSibling.firstChild.data = soePropertyValue
                                    else:
                                        txt = doc.createTextNode(soePropertyValue)
                                        prop.nextSibling.appendChild(txt)
                                        
# Output to a new sddraft.
outXml = "C:/Project/Output/CountiesForWeb.sddraft"     
f = open(outXml, 'w')     
doc.writexml( f )     
f.close()       

# Analyze the new sddraft for errors.
analysis = arcpy.mapping.AnalyzeForSD(outXml)
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

CreateMapSDDraft
https://desktop.arcgis.com/zh-cn/arcmap/10.4/analyze/arcpy-mapping/createmapsddraft.htm
AnalyzeForSD
https://desktop.arcgis.com/zh-cn/arcmap/10.4/analyze/arcpy-mapping/analyzeforsd.htm
StageService_server
Upload_Service_Definition

Modify the SDDraft example:

The following example script creates a service definition draft (.sddraft) file for the ARCGIS_SERVER server_type from a map document (.mxd). Then, modify the .sddraft file to enable caching for the service using the xml.dom.minidom standard Python library. Save the .sddraft file to a new file. Next, use the AnalyzeForSD function to analyze the new .sddraft file for errors. After analyzing the draft service definition, you can start transitioning the service definition. Use the Transition Service geoprocessing tool to transition service definitions. Then, use the Upload Service Definition geoprocessing tool to upload the service definition to the server and publish the map service. After the service is published, the script then calls the Manage Map Server Cache Scale geoprocessing tool, which updates the scale level in an existing cached map or image service. Use this tool to add new scales or remove existing scales from the cache. Finally, the script calls the Manage Map Server Cache Tiles geoprocessing tool to create map service cache tiles.


import arcpy
import xml.dom.minidom as DOM 
import os

# define local variables
wrkspc = 'C:/Project'
systemFolder = 'C:/Users/<username>/AppData/Roaming/ESRI/Desktop10.2/ArcCatalog'
server = 'arcgis on MyServer_6080 (publisher)'
service = 'Counties'

# build paths to data
mapDoc = arcpy.mapping.MapDocument(os.path.join(wrkspc, 'counties.mxd'))
connection = os.path.join(systemFolder, server + '.ags')
mapServer = os.path.join(systemFolder, server, service + '.MapServer')
sddraft = os.path.join(wrkspc, service + '.sddraft')
sd = os.path.join(wrkspc, 'output', service + '.sd')

# create sddraft
if os.path.exists(sddraft): os.remove(sddraft)
arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER')

# read sddraft xml
doc = DOM.parse(sddraft)

# turn on caching in the configuration properties
configProps = doc.getElementsByTagName('ConfigurationProperties')[0]
propArray = configProps.firstChild
propSets = propArray.childNodes
for propSet in propSets:
    keyValues = propSet.childNodes
    for keyValue in keyValues:
        if keyValue.tagName == 'Key':
            if keyValue.firstChild.data == "isCached":
                # turn on caching
                keyValue.nextSibling.firstChild.data = "true"
                
# output to a new sddraft
outXml = os.path.join(wrkspc, 'output', service + '.sddraft')   
if os.path.exists(outXml): os.remove(outXml)
f = open(outXml, 'w')     
doc.writexml( f )     
f.close() 

# analyze new sddraft for errors
analysis = arcpy.mapping.AnalyzeForSD(outXml)

# print dictionary of messages, warnings and errors
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
    if os.path.exists(sd): os.remove(sd)
    arcpy.StageService_server(outXml, sd)
    # Execute UploadServiceDefinition
    print "Uploading Service Definition..."
    arcpy.UploadServiceDefinition_server(sd, connection)
    # Print messaging from UploadServiceDefinition
    x = 0
    while x < arcpy.GetMessageCount():
        arcpy.AddReturnMessage(x)
        x = x + 1
else: 
    print "{} contained errors. StageService and UploadServiceDefinition aborted.".format(sddraft)
    exit()
    
print "Updating cache scales..."
scaleValues = "100000000;10000000;5000000;1000000;500000;250000;125000;64000;5250"
arcpy.ManageMapServerCacheScales_server(mapServer, scaleValues)

print "Creating tiles..."
arcpy.ManageMapServerCacheTiles_server(mapServer, "100000000", "RECREATE_ALL_TILES", "3")

print "Uploaded service and created tiles."

About draft service:

https://enterprise.arcgis.com/zh-cn/server/10.4/publish-services/windows/about-draft-services.htm

Guess you like

Origin blog.csdn.net/weixin_40625478/article/details/115698918