摘要
函数将 Result 对象和结果文件 (.rlt) 转换成服务定义草稿 (.sddraft) 文件。
说明
CreateGPSDDraft 是使用 ArcPy 将地理处理结果自动发布到 GIS Server 的第一步。通过 CreateGPSDDraft 创建的输出是服务定义草稿 (.sddraft) 文件。服务定义草稿由结果文件或 Result 对象、服务器信息和一组服务属性组合而成。可通过设置执行工具的变量,从而在 Python 脚本中创建 Result 对象,例如,以下缓冲结果保存至名为 result 的变量中:
import arcpy
result = arcpy.Buffer_analysis("inPts", "output.shp", "100 Meters")
服务器信息包括服务器连接,已发布的服务器的类型,正在发布的服务的类型,服务的元数据(项目信息)和数据参考(是否向服务器复制数据)。
服务属性包括服务的地理处理功能和附加功能,例如,网络处理服务 (WPS)。这些功能不会显示为参数。如果您想修改值(或其他未显示的参数),您需要首先发布 sddraft,并通过使用 XML 库(如 xml.dom.minidom)编辑 .sddraft 来修改草稿。有关库的用法,请参阅修改服务定义草稿示例。尽管示例来自地图服务草稿,但可以对 GP 服务使用同一个库和方法,因为它是一个 XML 文件。
此函数返回 Python 字典,其中包含应在创建服务定义文件之前解决的错误和其他潜在问题。
可在特定服务器连接信息未知的情况下编写服务定义草稿。在这种情况下,可以省略 connection_file_path 参数;但是,必须提供 server_type。可在使用上载服务定义工具发布服务定义草稿后提供服务器连接。
随后可使用过渡服务工具将服务定义草稿转换为完全合并的服务定义 (.sd) 文件。过渡过程会编译成功发布 GIS 资源所需的所有必要信息。如果未将数据注册到服务器,将在过渡服务定义草稿时添加这些数据。最后,可以使用上载服务定义工具上载服务定义文件并将其作为 GIS 服务发布到特定的 GIS 服务器。此步骤将获取服务定义文件、将其复制到服务器、提取所需信息并发布 GIS 资源。有关详细信息,请参阅发布工具集概述。
语法
CreateGPSDDraft (result, out_sddraft, service_name, {server_type}, {connection_file_path}, {copy_data_to_server}, {folder_name}, {summary}, {tags}, {executionType}, {resultMapServer}, {showMessages}, {maximumRecords}, {minInstances}, {maxInstances}, {maxUsageTime}, {maxWaitTime}, {maxIdleTime})
参数 | 说明 | 数据类型 |
result [result,...] | A reference to one or multiple Result objects or result files (.rlt) on disk. Multiple results must be supplied in a list format. The following example demonstrates multiple results as input to the CreateGPSDDraft function.
| Result |
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 result 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 result is registered with the server, then copy_data_to_server will always be False. Conversely, if the data in the result is not registered with the server, then copy_data_to_server will always be True. (默认值为 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. 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. 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 |
executionType | 同步和异步定义了客户端(使用任务的应用程序)如何与服务器进行交互以及如何获取任务结果。服务设置为同步时,客户端将等待任务完成。通常,同步任务的执行速度很快,大概需要 5 秒或更短。异步任务的执行往往花费的时间较长,客户端必须定期向服务器发出询问以确定任务是否已完成,如果完成,则获取结果。使用异步任务的 Web 应用程序必须添加函数逻辑来检查任务状态,并在执行完毕后处理结果。ArcGIS Desktop 客户端可以处理这两种执行类型。 (默认值为 Asynchronous) | String |
resultMapServer | When publishing a geoprocessing service, you can choose to view the result of all tasks with the service as a map (in addition to other results of your task). The map is created on the server using a Map Service for transport back to the client as an image (a .jpeg, for example). The symbology, labeling, transparency, and all other properties of the returned map are the same as the settings of your output layer. Remember, if you are creating result layers within the Python scripting environment (outside ArcMap), default symbologies will be used. To maintain control over symbology you will need to pre-create layer files with rich symbology and use them to modify the output symbology of your task. When you choose this option, a map service is automatically created on the server with the same name as your geoprocessing service. (默认值为 False) | Boolean |
showMessages | A string setting the message level for the geoprocessing service. The following is a list of valid message levels the service will return to the client.
(默认值为 None) | String |
maximumRecords | 服务可返回给客户端的最大结果数。将此值设成一个较大数值意味着您的 GIS 服务器可处理发送大量的个人记录或要素给客户端。如果您不希望返回任何要素,请将此值设为 O。通常情况下,只有当您启用通过地图服务查看结果时才将此值设为零。 (默认值为 1000) | Integer |
minInstances | An integer value representing the minimum number of instances a service will start and make available for use. For heavily used services you may want to increase this value. (默认值为 1) | Integer |
maxInstances | An integer value representing the maximum number of instances a service can start and make available for use. For heavily used services you may need to increase this value. Ensure the server has adequate hardware to support the maximum number of instances you will allow. (默认值为 2) | Integer |
maxUsageTime | 一个服务能被使用的最长时间,以秒为单位。您可能需要延长默认值 600 秒(10分钟),以服务于长时间运行的地理处理任务。或者,您可能需要减少此时间,以确保客户端不会滥用您的服务。 (默认值为 600) | Integer |
maxWaitTime | 超时之前客户端连接一个实例需要等待的最长时间(以秒计)。当所有实例都忙于处理请求时,随后的请求将进入排队状态。如果在实例可被使用之前超时,任务会失败。默认值为 60 秒(1 分钟)。 (默认值为 60) | Integer |
maxIdleTime | 池收缩之前一个实例能继续保持活跃的最长时间(以秒计)。对于所有超出最小实例数且未被使用的实例,一旦达到最长空闲时间,即会被关闭。 (默认值为 1800) | Integer |
返回值
数据类型 | 说明 |
Dictionary | 返回由信息性消息、警告和错误组成的 Python 字典。 |
代码示例
CreateGPSDDraft 示例 1
以下脚本演示了通过 Python 脚本进行地理处理服务的完整发布。地理处理服务的自动发布使用 ArcPy 函数和发布工具集中的地理处理工具。工作流首先执行要发布的地理处理任务。成功执行地理处理任务并保存结果文件后,使用 ArcPy 函数 CreateGPSDDraft 创建服务定义草稿。请注意,使用摘要和标签参数会覆盖输入地理处理结果的“项目描述”、“摘要”和“标签”。接下来,应使用 AnalyzeForSD 函数对服务定义草稿进行分析,以找出可能会阻止成功发布的问题。分析服务定义草稿并解决严重问题后,即可开始过渡服务定义。过渡操作可获取服务定义草稿并将发布服务所需的所有信息合并到完整的服务定义中。使用 Stage_Service 地理处理工具过渡服务定义。最后,使用 Upload_Service_Definition 地理处理工具将服务定义上载到服务器并发布地理处理服务。
import arcpy
result = "c:/gis/gp/Analysis.rlt"
connectionPath = "c:/gis/conections/myServer.ags"
sddraft = "c:/gis/gp/drafts/AnalysisDraft.sddraft"
sd = "c:/gis/gp/sd/AnalysisDraft.sd"
serviceName = "AnalysisService"
# Create service definition draft
arcpy.CreateGPSDDraft(
result, sddraft, serviceName, server_type="ARCGIS_SERVER",
connection_file_path=connectionPath, copy_data_to_server=True,
folder_name=None, summary="Analysis Service", tags="gp",
executionType="Synchronous", resultMapServer=False,
showMessages="INFO", maximumRecords=5000, minInstances=2,
maxInstances=3, maxUsageTime=100, maxWaitTime=10,
maxIdleTime=180)
# Analyze the service definition draft
analyzeMessages = arcpy.mapping.AnalyzeForSD(sddraft)
# Stage and upload the service if the sddraft analysis did not
# contain errors
if analyzeMessages['errors'] == {}:
# Execute StageService
arcpy.StageService_server(sddraft, sd)
# Execute UploadServiceDefinition
arcpy.UploadServiceDefinition_server(sd, connectionPath)
else:
# If the sddraft analysis contained errors, display them
print(analyzeMessages['errors'])
CreateGPSDDraft 示例 2
该示例演示了提取数据工作流的创建与发布,并保证数据已注册到服务器。提取数据任务使用输入图层,允许用户提取感兴趣的区域并转换至另一个输出类型,接收 zip 格式的输出文件。“提取数据”工具仅使用图层作为输入。该示例使用“创建要素图层工具”创建多个图层,运行工具,创建服务定义草稿,上传用于创建服务的服务定义。在工作流的 Python 会话中创建的任意图层都将视作最终服务中的可能输入,并可作为一个选项。
import os
import arcpy
connPath = "c:/gis/conections/myServer.ags"
sddraft = "c:/gis/gp/drafts/ExtractionDraft.sddraft"
sd = "c:/gis/gp/sd/AnalysisDraft.sd"
serviceName = "DataExtractor"
arcpy.env.workspace = "c:/gis/citydata"
aoi = "c:/gis/citydata/extract.shp"
# Create layers which will be available as input
arcpy.MakeFeatureLayer_management('Wiarton.gdb/places/Cityhall',
'CityHall')
arcpy.MakeFeatureLayer_management('Wiarton.gdb/places/Airport',
'Airport')
arcpy.MakeFeatureLayer_management('Wiarton.gdb/places/FireStations',
'FireStations')
# Run the extract data task and assign it to the 'result' variable
# only the cityhall layer was used as input, but the airport and
# firestation layers will be used in the service creation
result = arcpy.ExtractDataTask_server(
"CityHall", aoi, "File Geodatabase - GDB - .gdb", "ESRI GRID - GRID",
os.path.join(arcpy.env.scratchFolder, "output.zip"))
# Make sure the folder is registered with the server, if not, add
# it to the datastore
if arcpy.env.workspace not in [
i[2] for i in arcpy.ListDataStoreItems(connPath, 'FOLDER')]:
# both the client and server paths are the same
dsStatus = arcpy.AddDataStoreItem(
connPath, "FOLDER", "CityData", arcpy.env.workspace,
arcpy.env.workspace)
print("Data store : {0}".format(dsStatus))
# Create service definition draft
arcpy.CreateGPSDDraft(
result, sddraft, serviceName, server_type="ARCGIS_SERVER",
connection_file_path=connPath, copy_data_to_server=False,
folder_name=None, summary="Extraction Service",
tags="extract data, clip")
# Analyze the service definition draft
analyzeMessages = arcpy.mapping.AnalyzeForSD(sddraft)
# Stage and upload the service if the sddraft analysis did not
# contain errors
if analyzeMessages['errors'] == {}:
# Execute StageService
arcpy.StageService_server(sddraft, sd)
# Execute UploadServiceDefinition
upStatus = arcpy.UploadServiceDefinition_server(sd, connPath)
print("Completed upload")
else:
# If the sddraft analysis contained errors, display them
print(analyzeMessages['errors'])