Skip To Content


此示例 Python 脚本运行模型并将结果作为地理处理服务发布到 ArcGIS Server

脚本首先创建一些要素图层,然后从“服务器工具”工具箱中运行提取数据任务模型。 如果要素图层位置未注册到 ArcGIS Server,脚本将调用 arcpy.AddDataStoreItem 以注册数据库或文件夹。 接下来,脚本调用 arcpy.CreateGPSDDraft 以从模型结果创建服务定义 (SD) 草稿文件。

如果分析 SD 草稿后未发现任何错误,则脚本将开始发布服务。

如果使用 ArcGIS Pro 来发布到 ArcGIS Server,请参阅 ArcGIS Pro 帮助中的在 Python 脚本中发布地理处理服务以了解相关说明。

import arcpy
import os

connPath = "c:/gis/conections/myServer.ags"
sddraft = "c:/gis/gp/drafts/ExtractionDraft.sddraft"
sd = "c:/gis/gp/sd/"
serviceName = "DataExtractor"
arcpy.env.workspace = "c:/gis/citydata"

# 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
aoi = "c:/gis/citydata/extract.shp"
result = arcpy.ExtractDataTask_server("CityHall", aoi, "File Geodatabase - GDB - .gdb", "ESRI GRID - GRID", os.path.join(arcpy.env.scratchFolder, ""))

# 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 : " + str(dsStatus)

# create service definition draft
    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"
    # if the sddraft analysis contained errors, display them
    print analysis['errors']