CreateMapSDDraft (arcpy.mapping)
摘要
将地图文档 (.mxd) 文件转换为服务定义草稿 (.sddraft) 文件。
讨论
CreateMapSDDraft 是使用 ArcPy 将地图文档自动发布到 GIS Server 的第一步。通过 CreateMapSDDraft 创建的输出是服务定义草稿 (.sddraft) 文件。服务定义草稿由地图文档、有关服务器的信息和一组服务属性组合而成。
有关服务器的信息包括服务器连接或发布到的服务器类型,正在发布的服务的类型,服务的元数据(项目信息)和数据参考(是否将数据复制到服务器)。
服务属性包括服务是否支持缓存,如果支持,则还包括缓存设置。还包括服务的其他附加功能(如要素访问或 OGC 功能)以及所选功能的相应属性集。此方法最初使用一组默认的服务属性。用户可以使用标准的第三方 XML 编辑器对这些属性进行编辑。此外,用户可以使用第三方 XML 库(如 xml.dom.minidom 标准 Python 库)自动实现属性修改。请参见以下修改 SDDraft 示例。
草稿服务定义不包含数据。草稿服务不能单独用于发布服务。
与 AnalyzeForSD 函数相似,CreateSDDraft 也会返回一份包含错误和其他潜在问题的 Python 字典,创建服务定义文件之前应解决这些错误和问题。有关错误、警告和信息性消息的类型,以及如何访问它们的详细信息,请参阅 AnalyzeForSD。
可在特定服务器连接信息未知的情况下编写服务定义草稿。在这种情况下,可以省略 connection_file_path 参数;但是,必须提供 server_type。随后可在使用上载服务定义工具发布服务定义草稿时提供服务器连接。
紧接着使用过渡服务工具将服务定义草稿转换为完全合并的服务定义 (.sd) 文件。过渡过程会编译为成功发布 GIS 资源所需的所有必要信息。如果未使用服务器注册数据,则将在服务定义草稿阶段添加数据。最后,可以使用上载服务定义工具上载服务定义文件并将其作为 GIS 服务发布到特定的 GIS 服务器。此步骤获取服务定义文件,将其复制到服务器,提取所需信息并发布 GIS 资源。有关详细信息,请参阅发布工具集概述。
一旦 .sddraft 过渡并上载到服务器,缓存工具集中的工具即可用于为没有启用缓存的服务创建切片方案,如创建地图服务器缓存工具。此外,缓存工具集中的工具可用于修改已启用缓存的服务的缓存和切片属性。例如,管理地图服务器缓存比例工具可用于向缓存添加新比例或从缓存删除现有比例。ModifySDDraft 示例 7 对此进行了演示。切片方案也可以通过编辑使用第三方 XML 库的 .sddraft 来进行修改,如 xml.dom.minidom 标准 Python 库。但是,由于切片方案 XML 结构的复杂性,建议尽量使用缓存工具集。
ArcGIS for Server Administrator API 还可用于编写在服务器上执行常用操作的脚本。例如,停止和启动服务、编辑服务的属性(如实例的最大数量)、授予和撤消用户的服务权限等。
您还可以为地理处理和影像服务创建服务定义草稿文件。请参阅以下相关函数:
语法
参数 | 说明 | 数据类型 |
map_document |
A variable that references a MapDocument object. | MapDocument |
out_sddraft |
A string that represents the path and file name for the output Service Definition Draft (.sddraft) file. | String |
service_name |
A string that represents the name of the service. This is the name people will see and use to identify the service. The name can only contain alphanumeric characters and underscores. No spaces or special characters are allowed. The name cannot be more than 120 characters in length. | String |
server_type |
A string representing the server type. If a connection_file_path parameter is not supplied, then a server_type must be provided. If a connection_file_path parameter is supplied, then the server_type is taken from the connection file. In this case, you can choose FROM_CONNECTION_FILE or skip the parameter entirely.
(默认值为 ARCGIS_SERVER) | String |
connection_file_path |
A string that represents the path and file name to the ArcGIS for Server connection file (.ags). | String |
copy_data_to_server |
A Boolean that indicates whether the data referenced in the map document will be copied to the server or not. The copy_data_to_server parameter is only used if the server_type is ARCGIS_SERVER and the connection_file_path isn't specified. If the connection_file_path is specified, then the server's registered data stores are used. For example, if the data in the map_document is registered with the server, then copy_data_to_server will always be False. Conversely, if the data in the map_document is not registered with the server, then copy_data_to_server will always be True. When the server_type is set to SPATIAL_DATA_SERVER, copy_data_to_server will always be False. Spatial Data Server services always use registered data and therefore never copy data to the server. When the server_type is set to MY_HOSTED_SERVICES, copy_data_to_server will always be True. My Hosted Maps services always copy data to the server. (默认值为 False) | Boolean |
folder_name |
A string that represents a folder name to which you want to publish the service definition. If the folder does not currently exist, it will be created. The default folder is the server root level. (默认值为 None) | String |
summary | A string that represents the Item Description Summary. By default, the Summary from the ArcMap Map Properties dialog box or Catalog window Item Description dialog box for the map_document will be used. Use this parameter to override the user interface summary, or to provide a summary if one does not exist. The summary provided here will not be persisted in the map document. (默认值为 None) | String |
tags | A string that represents the Item Description Tags. By default, the Tags from the ArcMap Map Properties dialog box or Catalog window Item Description dialog box for the map_document will be used. Use this parameter to override the user interface tags, or to provide tags if they do not exist. The tags provided here will not be persisted in the map document. (默认值为 None) | String |
数据类型 | 说明 |
Dictionary |
返回由信息性消息、警告和错误组成的 Python 字典。 |
代码实例
以下脚本演示使用 arcpy.mapping 工作流发布地图服务的完整过程。通过将 arcpy.mapping 函数与发布工具集中的地理处理工具加以组合,可实现地图服务的自动发布。工作流开始于要发布的地图文档。首先,使用 arcpy.mapping 函数的 CreateMapSDDraft 创建服务定义草稿。请注意,使用 summary 和 tags 参数覆盖输入地图文档的“项目描述”、“摘要”和“标签”。接下来,应对服务定义草稿进行分析,以找出可能会阻止成功发布的问题。分析服务定义草稿并解决严重问题后,即可开始过渡服务定义。过渡操作获取服务定义草稿并将发布服务所需的所有信息合并到完整的服务定义中。使用 Stage_Service 地理处理工具过渡服务定义。最后,使用 Upload_Service_Definition 地理处理工具将服务定义上载到服务器并发布地图服务。
import arcpy
# define local variables
wrkspc = 'C:/Project/'
mapDoc = arcpy.mapping.MapDocument(wrkspc + 'counties.mxd')
con = 'GIS Servers/arcgis on MyServer_6080 (publisher).ags'
service = 'Counties'
sddraft = wrkspc + service + '.sddraft'
sd = wrkspc + service + '.sd'
summary = 'Population Density by County'
tags = 'county, counties, population, density, census'
# create service definition draft
analysis = arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER',
con, True, None, summary, tags)
# 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']
以下示例脚本通过地图文档 (.mxd) 创建服务定义草稿 (.sddraft) 文件。然后输出包含由 CreateSDDraft 函数返回的错误、警告和信息的 Python 字典。Python 字典中包含的分析信息有助于识别可能需要在创建服务定义 (.sd) 文件之前解决的潜在性瓶颈和地图错误。此脚本还演示了如何在不指定服务器连接信息的情况下创建服务定义草稿。
import arcpy
mapDoc = arcpy.mapping.MapDocument('C:/Project/counties.mxd')
service = 'Counties'
sddraft = 'C:/Project/' + service + r'.sddraft'
analysis = arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER')
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
以下示例脚本通过“我的托管服务”地图文档 (.mxd) 创建服务定义草稿 (.sddraft) 文件。如果服务定义草稿不包含分析错误,则使用过渡服务地理处理工具进行过渡。在使用上载服务定义地理处理工具最终发布到“我的托管服务”前,可使用登录门户登录到 ArcGIS.com。
import arcpy
mapDoc = arcpy.mapping.MapDocument('C:/Project/counties.mxd')
service = 'Counties'
sddraft = 'C:/Project/{}.sddraft'.format(service)
sd = 'C:/Project/{}.sd'.format(service)
# create service definition draft
analysis = arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'MY_HOSTED_SERVICES')
# stage and upload the service if the sddraft analysis did not contain errors
if analysis['errors'] == {}:
# create service definition
arcpy.StageService_server(sddraft, sd)
# if required, sign in to My Hosted Services
arcpy.SignInToPortal_server('username', 'password', 'http://www.arcgis.com/')
# publish to My Hosted Services
arcpy.UploadServiceDefinition_server(sd, 'My Hosted Services')
else:
# if the sddraft analysis contained errors, display them
print analysis['errors']
下列脚本使用 xml.dom.minidom 标准 Python 库修改“项目信息描述”元素。修改后的服务定义草稿 (.sddraft) 随后被保存到新文件中。最后,使用 AnalyzeForSD 函数分析新的 .sddraft 中是否包含错误。
import arcpy
import xml.dom.minidom as DOM
# the new description
newDesc = 'US Counties Map'
xml = r"C:\Project\Counties.sddraft"
doc = DOM.parse(xml)
# find the Item Information Description element
descriptions = doc.getElementsByTagName('Description')
for desc in descriptions:
if desc.parentNode.tagName == 'ItemInfo':
# modify the Description
if desc.hasChildNodes():
desc.firstChild.data = newDesc
else:
txt = doc.createTextNode(newDesc)
desc.appendChild(txt)
# output to a new sddraft
outXml = r"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
下列脚本使用 xml.dom.minidom 标准 python 库修改过渡设置 TextAntialiasingMode 元素。修改后的服务定义草稿 (.sddraft) 随后被保存到新文件中。最后,使用 AnalyzeForSD 函数分析新的 .sddraft 中是否包含错误。
import arcpy
import xml.dom.minidom as DOM
# the new TextAntiAliasingMode value
newTextAntialiasingMode = 'Normal'
xml = r"C:\Project\Counties.sddraft"
doc = DOM.parse(xml)
keys = doc.getElementsByTagName('Key')
for key in keys:
if key.hasChildNodes():
if key.firstChild.data == 'textAntialiasingMode':
# modify the TextAntiAliasingMode value
key.nextSibling.firstChild.data = newTextAntialiasingMode
# output to a new sddraft
outXml = r"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
以下示例脚本通过地图文档 (.mxd) 创建服务定义草稿 (.sddraft) 文件。然后启用 WMSServer 功能并通过修改使用 xml.dom.minidom 标准 Python 库的 .sddraft 来设置切片属性。修改后的 .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:
extention = typeName.parentNode
for extElement in extention.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
以下示例脚本通过地图文档 (.mxd) 创建服务定义草稿 (.sddraft) 文件。然后通过修改使用 xml.dom.minidom 标准 Python 库的 .sddraft 来禁用 KmlServer 功能。修改后的 .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')
# The Server Object Extention (SOE) to disable.
soe = 'KmlServer'
# 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 we want to disable.
if typeName.firstChild.data == soe:
extension = typeName.parentNode
for extElement in extension.childNodes:
# Disabled SOE.
if extElement.tagName == 'Enabled':
extElement.firstChild.data = 'false'
# 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
以下示例脚本通过地图文档 (.mxd) 创建 Spatial Data Server server_type 的服务定义草稿 (.sddraft) 文件。然后通过修改使用 xml.dom.minidom 标准 Python 库的 .sddraft 来更改服务的默认 Web 功能。修改后的 .sddraft 随后被保存到新文件中。最后,使用 AnalyzeForSD 函数分析新的 .sddraft 中是否包含错误。
import arcpy
import xml.dom.minidom as DOM
# Reference map document for CreateSDDraft function.
mapDoc = arcpy.mapping.MapDocument('C:/project/SDS.mxd')
# Create service and sddraft variables for CreateSDDraft function.
service = 'CountiesSDS'
sddraft = 'C:/Project/{}.sddraft'.format(service)
# Create sddraft.
arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'SPATIAL_DATA_SERVER')
# Read the sddraft xml.
doc = DOM.parse(sddraft)
# Get all the value tags.
values = doc.getElementsByTagName('Value')
for value in values:
if value.hasChildNodes():
# Change the default WebCapabilities from 'Query,Create,Update,Delete,Uploads,Editing' to just 'Query'.
if value.firstChild.data == 'Query,Create,Update,Delete,Uploads,Editing':
value.firstChild.data = 'Query'
# Output to a new sddraft.
outXml = "C:/Project/Output/CountiesForSDS.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
以下示例脚本通过地图文档 (.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/'
mapDoc = arcpy.mapping.MapDocument(wrkspc + 'counties.mxd')
con = 'GIS Servers\arcgis on MyServer_6080 (admin).ags'
service = 'Counties'
sddraft = 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 = "C:\Project\Output\CountiesForWeb.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
del f, doc, mapDoc
以下示例脚本通过地图文档 (.mxd) 创建 ARCGIS_SERVER server_type 的服务定义草稿 (.sddraft) 文件。然后通过修改使用 xml.dom.minidom 标准 Python 库的 .sddraft 来启用服务的缓存。修改后的 .sddraft 随后被保存到新文件中。然后,使用 AnalyzeForSD 函数分析新的 .sddraft 中是否包含错误。分析服务定义草稿后,即可开始过渡服务定义。使用 Stage_Service 地理处理工具过渡服务定义。然后,使用 Upload_Service_Definition 地理处理工具将服务定义上载到服务器并发布地图服务。一旦发布服务,脚本就会调用管理地图服务器缓存比例地理处理工具,更新现有缓存地图或映像服务的比例级别。使用此工具可添加新比例或从缓存中删除现有比例。最后,脚本会调用管理地图服务器缓存切片地理处理工具来创建地图服务缓存切片。
import arcpy
import xml.dom.minidom as DOM
import os
# define local variables
wrkspc = 'C:/Project/'
mapDoc = arcpy.mapping.MapDocument(wrkspc + 'counties.mxd')
con = "GIS Servers\\arcgis on MyServer_6080 (admin).ags"
service = 'Counties'
sddraft = 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 = "C:/Project/Output/CountiesForWeb.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, con)
# 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".format(con[:-4], service), scaleValues)
print "Creating tiles..."
arcpy.ManageMapServerCacheTiles_server("{}\\{}.MapServer".format(con[:-4], service), "100000000",
"Recreate All Tiles", "3")
del f, doc, mapDoc