之前有个小伙伴问个这样一个问题,我使用python自动发布服务的时候,我该如何使用python控制的切片方案的xml参数呢??
下面是我之前收集发布服务时候收集的一些官方帮助,希望对你有丝丝启发;
参考官网CreateMapSDDraft
下列脚本使用 xml.dom.minidom 标准 python 库修改过渡设置 TextAntialiasingMode 元素。修改后的服务定义草稿 (.sddraft) 文件随后被保存到新文件中。最后,使用 AnalyzeForSD 函数分析新的 .sddraft 文件中是否包含错误。
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
修改SDDraft示例:
以下示例脚本通过地图文档(.mxd)为ARCGIS_SERVER server_type创建服务定义草稿(.sddraft)文件。然后,使用xml.dom.minidom标准Python库修改.sddraft文件以启用服务的缓存功能。的.sddraft文件保存到新文件中。接下来,使用AnalyzeForSD函数分析新的.sddraft文件中是否包含错误。分析服务定义草稿后,即可开始过渡服务定义。使用过渡服务地理处理工具过渡服务定义。然后,使用上传服务定义地理处理工具将服务定义上传到服务器并发布地图服务。发布服务后,脚本随后调用管理地图服务器缓存比例地理处理工具,该工具在现有已缓存地图或影像服务中更新比例级别。使用此工具可添加新比例或从缓存中删除现有比例。最后,此脚本调用管理地图服务器缓存切片地理处理工具创建地图服务缓存切片。
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."
关于草稿服务:
https://enterprise.arcgis.com/zh-cn/server/10.4/publish-services/windows/about-draft-services.htm