ArcGIS API For Javascript如何加载shp数据作为GP服务的输入参数进行调用
对于这个问题,网上有好多解决办法,那么小编就把最近这个问题进行一个简单的梳理,我们只需要在server中发布GP服务,将shp数据作为GP服务的输入参数即可实现:ArcGIS API For Javascript如何加载shp数据作为GP服务的输入参数进行调用;
1、处理流程
上传shapefile文件比较棘手的是现在没有GP工具提供上传shp到server作为gp工具参数的功能,所以需要通过上传zip并将其解压的方式绕行,思路就是,在模型构件器中将附件中的压缩和解压的脚本放入,后面接入处理shp文件的gp工具,将这个模型构建器里的工作流保存成为一个gp工具,发布成为GP服务即可。
需要注意的是,如果需要上传.zip格式的数据以供地理处理任务使用,需要在发布服务时,勾选允许上传操作,以便在客户端将所需的.zip文件上传至服务器。
2、GP制作和发布
我们需要把脚本在arcmap中制作成GP工具,然后发布成GP服务,这样在JS中调用发布的GP服务即可;
2.1、脚本文件制作GP
#**********************************************************************
# Description:
# Zips the contents of a folder.
# Parameters:
# 0 - Input folder.
# 1 - Output zip file. It is assumed that the user added the .zip
# extension.
#**********************************************************************
# Import modules and create the geoprocessor
#
import sys, zipfile, arcpy, os, traceback
# Function for zipping files. If keep is true, the folder, along with
# all its contents, will be written to the zip file. If false, only
# the contents of the input folder will be written to the zip file -
# the input folder name will not appear in the zip file.
#
def zipws(path, zip, keep):
path = os.path.normpath(path)
# os.walk visits every subdirectory, returning a 3-tuple
# of directory name, subdirectories in it, and file names
# in it.
#
for (dirpath, dirnames, filenames) in os.walk(path):
# Iterate over every file name
#
for file in filenames:
# Ignore .lock files
#
if not file.endswith('.lock'):
arcpy.AddMessage("Adding %s..." % os.path.join(path, dirpath, file))
try:
if keep:
zip.write(os.path.join(dirpath, file),
os.path.join(os.path.basename(path), os.path.join(dirpath, file)[len(path)+len(os.sep):]))
else:
zip.write(os.path.join(dirpath, file),
os.path.join(dirpath[len(path):], file))
except Exception, e:
arcpy.AddWarning(" Error adding %s: %s" % (file, e))
return None
if __name__ == '__main__':
try:
# Get the tool parameter values
#
infolder = arcpy.GetParameterAsText(0)
outfile = arcpy.GetParameterAsText(1)
# Create the zip file for writing compressed data. In some rare
# instances, the ZIP_DEFLATED constant may be unavailable and
# the ZIP_STORED constant is used instead. When ZIP_STORED is
# used, the zip file does not contain compressed data, resulting
# in large zip files.
#
try:
zip = zipfile.ZipFile(outfile, 'w', zipfile.ZIP_DEFLATED)
zipws(infolder, zip, True)
zip.close()
except RuntimeError:
# Delete zip file if it exists
#
if os.path.exists(outfile):
os.unlink(outfile)
zip = zipfile.ZipFile(outfile, 'w', zipfile.ZIP_STORED)
zipws(infolder, zip, True)
zip.close()
arcpy.AddWarning(" Unable to compress zip file contents.")
arcpy.AddMessage("Zip file created successfully")
except:
# Return any Python specific errors as well as any errors from the geoprocessor
#
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + \
str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
arcpy.AddError(pymsg)
msgs = "GP ERRORS:\n" + arcpy.GetMessages(2) + "\n"
arcpy.AddError(msgs)
#**********************************************************************
# Description:
# Unzips the contents of a zip file into an existing folder.
# Arguments:
# 0 - Input zip file
# 1 - Input folder - path to existing folder that will contain
# the contents of the zip file.
#**********************************************************************
# Import modules and create the geoprocessor
#
import sys, zipfile, arcpy, os, traceback
from os.path import isdir, join, normpath, split
# Function to unzipping the contents of the zip file
#
def unzip(path, zip):
# If the output location does not yet exist, create it
#
if not isdir(path):
os.makedirs(path)
for each in zip.namelist():
arcpy.AddMessage("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('/'):
root, name = split(each)
directory = normpath(join(path, root))
if not isdir(directory):
os.makedirs(directory)
file(join(directory, name), 'wb').write(zip.read(each))
if __name__ == '__main__':
try:
# Get the tool parameter values
#
infile = arcpy.GetParameterAsText(0)
outfol = arcpy.GetParameterAsText(1)
# Create the zipfile handle for reading and unzip it
#
zip = zipfile.ZipFile(infile, 'r')
unzip(outfol, zip)
zip.close()
except:
# Return any Python specific errors and any error returned by the geoprocessor
#
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + \
str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
arcpy.AddError(pymsg)
msgs = "GP ERRORS:\n" + arcpy.GetMessages(2) + "\n"
arcpy.AddError(msgs)
那么如何将上述脚本制作成arcmap中的GP工具呢?
我们在制作成GP工具前,需要先GP工具和GP服务的区别,以及如何制作和发布,参考:
ArcGIS API For Javascript如何加载shp数据作为GP服务的输入参数进行调用
2.2、model builder中如何输入shp数据
针对这个制作完GP工具后,在model builder中如何将本机文件的 shp数据 作为 GP工具的输入参数呢?
有些小伙伴可能感觉不会做,我们需要使用model builder中加一个遍历要素类的工具进行操作即可:
最后整个model build的GP工具,应该是下面这个样子的:
最后运行后,发布成GP服务,然后我们就可以正常的使用ArcGIS API For Javascrip对其进行发布的GP服务进行调用了;
3、ArcGIS API For Javascript调用GP服务
目前这方面小编还没梳理,日后有空的话,会进行整理;
大家可以参考官网的案例Geoprocessor示例,进而调用自己的GP服务:
Geoprocessing - viewshed analysis
后续有待完善;