解决GP服务产生的结果无法自动发布为地图服务的问题
在ArcGIS for Javascript API或REST API调用GP服务时,常常会遇到这样一种情况:GP服务运行以后,执行成功,也能够生成结果,然而结果并没有直接产生动态的地图服务供API调用(即使勾选了”view results with a map service”,如下图)。
那么产生这一现象的原因可能为:
1、 GP服务输出的结果路径存在人为干预,也就是生成的结果不在ArcGIS for Server管理的目录下(默认执行的结果会在JOB目录的Scratch中)。
2、 未勾选将GP服务的结果作为地图服务发布”view results with a map service”。
3、 GP服务生成的结果没有存储在gdb中(只是推断,还需进一步验证)。
那么,我们现在需要解决的是,在发布了GP服务以后,如何让GP执行后的结果自动发布为地图服务呢?
实现的思路是:
1、 制作GP工具。
2、 将GP生成的结果写入mxd文档。
3、 生成服务连接文件。
4、 发布地图文档为地图服务。
GP工具以buffer为例,整个python脚本如下:
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# PublishImageService.py
# Created on: 2015-10-16 15:49:27.00000
# (generated by HUIHUI)
# Usage:
# Description: python publish service
# ---------------------------------------------------------------------------
# Import arcpy module
import os as OS
import arcpy
#script auguments
wrkpc = arcpy.GetParameterAsText(0)
connectionfilename = arcpy.GetParameterAsText(1)
server_url = arcpy.GetParameterAsText(2)
username = arcpy.GetParameterAsText(3)
password = arcpy.GetParameterAsText(4)
servicename = arcpy.GetParameterAsText(5)
InputFC = arcpy.GetParameterAsText(6)
OutputFC = arcpy.GetParameterAsText(7)
distance = arcpy.GetParameterAsText(8)
#make a buffer shapefile
arcpy.Buffer_analysis(InputFC, OutputFC, distance, "", "", "", "")
#define a parh about mxdTemplate and symbologyLayer
mxdTemplate = OS.path.join(wrkpc,"Template.mxd")
symbologyLayer = OS.path.join(wrkpc,"buffer1.lyr")
# Make a layer from the feature class
outputLayer = OS.path.join(wrkpc, "buffer.lyr")
arcpy.MakeFeatureLayer_management(OutputFC,outputLayer)
arcpy.ApplySymbologyFromLayer_management (outputLayer, symbologyLayer)
# Create a Describe object from the feature layer.
desc = arcpy.Describe(symbologyLayer)
# Copy a map document template
newMxdPath = OS.path.join(wrkpc, "buffer.mxd")
arcpy.Copy_management(mxdTemplate, newMxdPath, "")
# Add layer into primary map document
mxd = arcpy.mapping.MapDocument(newMxdPath)
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
addLayer = arcpy.mapping.Layer(outputLayer)
arcpy.mapping.AddLayer(df, addLayer, "TOP")
mxd.title = "buffer test"
mxd.summary = "buffer test"
mxd.description = "buffer test"
mxd.tags = "buffer test"
mxd.save()
del mxd, df
# createGISServerConnectionFile,define local variable
out_folder_path = wrkpc
staging_folder_path = wrkpc
arcpy.mapping.CreateGISServerConnectionFile("PUBLISH_GIS_SERVICES",
out_folder_path,
connectionfilename,
server_url,
"ARCGIS_SERVER",
False,
staging_folder_path,
username,
password,
"SAVE_USERNAME")
# define local variables
mapDoc = arcpy.mapping.MapDocument(newMxdPath)
sddraft = OS.path.join(wrkpc,"buffer.sddraft")
sd = OS.path.join(wrkpc,"buffer.sd")
summary = "this is a test"
tags = "this is a test"
# creste service definition draft
analysis = arcpy.mapping.CreateMapSDDraft(mapDoc,
sddraft,
servicename,
"ARCGIS_SERVER",
connectionfilename,
False,
"WP_MapService",
summary,tags)
#stage and upload the service if the sddraft analysis didn't contain errors
if analysis['errors'] == {}:
# excute StageService
arcpy.StageService_server(sddraft,sd)
# excute UploadServiceDfinition
arcpy.UploadServiceDefinition_server(sd,connectionfilename)
else:
# if the sddraft analysis contained errors,display them
print analysis['errors']
将脚本添加到工具箱中,设置相关参数,执行界面如下:
执行成功,将这个工具发布为GP服务,测试GP服务执行成功与否。很明显,生成buffer以后的面图层,自动的发布为了地图服务,问题得到了解决。查看结果如下:
GP产生的结果自动发布为地图服务的图形展示:
转载自:https://blog.csdn.net/zhaohuihui6628/article/details/49183873