1、需求:
自己选择本地的文件上传在gp服务中进行分析,例如实现这个需求:
2、遇到的困境
发布创建TIN工具时要输入值表,但是我这里选择了本地的SHP文件和高程值后,发布出去就是一个常量值了,没法自己选择文件上传。如图:
3、解决思路
arcpy制作一个gp工具来发成gp服务。我看了创建tin的arcpy语法,里面的demo这一块有参数写法的介绍,如果将输入要素变成变量那就可以灵活的用自己的数据了。
4、最终实现的python代码:
提示:使用的python2需要注意中文字符支持问题。参考资料:python2 中文编码问题小结_python 2 汉字编码_up酱的博客-CSDN博客
# -*- coding: UTF-8 -*-
import os,os.path
import arcpy
import sys
import traceback
import zipfile
from os.path import isdir, join, normpath, split
reload(sys)
sys.setdefaultencoding("utf-8")arcpy.env.overwriteOutput=True
#by gislaozhang---20230829
#基于要素创建TIN的类# Function to unzipping the contents of the zip file
#
def unzip(path, zip):# If the output location does not yet exist, create it## doneif not isdir(path.decode('utf-8')):os.makedirs(path.decode('utf-8'))for each in zip.namelist():# arcpy.AddMessage("Extracting " + os.path.basename(each) + " ...")print "Extracting " + os.path.basename(each) + " ..."# Check to see if the item was written to the zip file with an# archive name that includes a parent directory. If it does, create# the parent folder in the output workspace and then write the file,# otherwise, just write the file to the workspace.#if not each.endswith('/'):# print eachroot, name = split(each)# print rootdirectory = normpath(join(path, root))print directoryif not isdir(directory.decode('utf-8').encode('gb2312')):os.makedirs(directory.decode('utf-8').encode('gb2312'))f = file(join(directory.decode('utf-8').encode('gb2312'), name), 'wb')f.write(zip.read(each))def createTIN(inFeature,inHeightField,sr,outRaster,dataType,method,sampling,zfactor):try:arcpy.CheckOutExtension("3D")# Execute CreateTinarcpy.AddMessage("Creating TIN dataset...")tempData1 = arcpy.CreateScratchName(workspace=arcpy.env.scratchGDB) # 使用scratchGDB管理临时数据# print tempData1result = arcpy.CreateTin_3d(tempData1, sr, "{0} {1}" \.format(inFeature, inHeightField).decode("utf-8"), "Delaunay")arcpy.TinRaster_3d(result, outRaster.decode("utf-8"), dataType,method, sampling, zfactor)arcpy.AddMessage("Finished")arcpy.CheckInExtension("3D")except arcpy.ExecuteError:print arcpy.GetMessages()def copyFeature(unzip_dir):arcpy.env.workspace = unzip_dirlistfc = arcpy.ListFeatureClasses()tempData = arcpy.CreateScratchName(workspace=arcpy.env.scratchGDB)#使用scratchGDB管理临时数据for fc in listfc:print(fc)arcpy.CopyFeatures_management(fc, tempData)createTIN(tempData,inHeightField, sr, outRaster, dataType, method, sampling, zfactor)# Set local variables
# infile = r"E:\等高线纠正.zip"
tempData = arcpy.CreateScratchName(workspace=arcpy.env.scratchFolder) # Use scratchGDB environment to write intermediate datainHeightField = "Elevation" #创建tin高度字段
# sr = r"E:\等高线纠正.prj" #
# outTin = r"E:\CreateTin" # TIN created to delineate data area
# outRaster = r"E:\tinraster.tif"
# dataType = "FLOAT"
# method = "LINEAR"
# sampling = "OBSERVATIONS 250"
# zfactor = "1"infile = arcpy.GetParameterAsText(0) #输入zip压缩数据
# inHeightField = arcpy.GetParameterAsText(1) #创建tin高度字段
sr = arcpy.GetParameterAsText(1) #空间参考信息
outRaster =arcpy.GetParameterAsText(2) #tin转栅格的输出路径
dataType = arcpy.GetParameterAsText(3) #栅格数据类型
method = arcpy.GetParameterAsText(4) #创建栅格方法
sampling = arcpy.GetParameterAsText(5) #重采样方法
zfactor =arcpy.GetParameterAsText(6) #比例因子# Create the zipfile handle for reading and unzip it
#解压zip压缩包的shp数据到指定的文件夹
#
zip = zipfile.ZipFile(infile.decode('utf-8'), 'r')# methodsForTin=FeatureToTin(outfol)
unzip(tempData, zip)
zip.close()copyFeature(tempData)# Use scratchGDB environment to write intermediate data
# tempData = arcpy.CreateScratchName(workspace=arcpy.env.scratchFolder)
制作的模型和执行的结果:
链接:https://pan.baidu.com/s/1mqBtB92bGQrIHCqVytlmXA
提取码:iafl
5、将执行的结果发成gp服务
发布gp服务时勾选“upload”和“查看地图服务结果”选项
在server中执行该gp服务,执行gp服务时,首先将zip文件上传
然后在执行gp服务时这样传参
https://developers.arcgis.com/rest/services-reference/enterprise/gp-data-types.htm
{
"itemID":"i21a899a6-75a5-43c3-8b0a-29d871d82ab4"
}
最终执行结果