1.按照id生成zip压缩包,下载到本地
参考博客
以上博客中提供的arcpy代码文件路径及文件名必须要中文路径,笔者稍作修改,如下:
1)识别中文路径:在inputname后面加上.decode(‘utf-8’).encode(‘gbk’)
inputname.decode('utf-8').encode('gbk')
2)固定名称:不去识别中文路径,而是给压缩包一个固定的名称
outname = ‘result’ + str(cnt)
全部arcpy代码:总体与上述博客相差不多,通过代码生成gp工具后就可以调用,下载zip包到arcgis server的安装路径,通过后台代码从服务器的arcgis server安装路径内下载到本地
# coding:utf8
# Author:PasserQi
# Time:2018/6/15
# Vesrion:0.2.1
# Param:
# - input
# - input_path:the feature class will be selected
# - out_dir:the output dir of the zip file
# - sql:query condition
# - output
# - out_path:the out path of the zip file
# Decs:
# 1. Select elements by attribute
# 2. Generates the SHP file from the selected element in the specified directory
# 3. Package the SHP file to generate the zip file
# 4. Reture the path to the zip file
import arcpy
import os
import sys
import zipfile
# Generate file names without repetition
# Maximum 100 files
def getFileName(inputfilepath,outdir):
ret_filename = None
inputname = os.path.basename(input_path)
if '.' in inputname: #Get the prefix
inputname = inputname.split('.')[0]
# Find name without repetition between inputname0 and inputname99
for cnt in range(0,100):
# inputname.decode('utf-8').encode('gbk')中文
outname = 'result' + str(cnt) #Get outname
outpath = os.path.join(outdir, outname + ".zip")
if os.path.exists(outpath): #If this filename exists
continue
else: #This filename don't exist
ret_filename = outname
if not ret_filename:
print 'More than 100 documents have been created.Created faild'
return ret_filename
# Package the SHP file to generate the zip file
def shpFilesToZips(outpath):
shpname = os.path.basename(outpath)
shppath = os.path.dirname(outpath)
name = shpname.replace(".shp","")
zippath = os.path.join(shppath, name+ ".zip")
# Get the files will be packaged
files =[]
# package
pre_len = len(os.path.dirname(shppath))
zipf = zipfile.ZipFile(zippath, 'w')
for parent, dirnames, filenames in os.walk(shppath):
for filename in filenames:
if '.zip' in filename:
continue
if name in filename:
pathfile = os.path.join(parent, filename)
arcname = pathfile.replace(shppath, '')
zipf.write(pathfile, arcname)
files.append(pathfile)
zipf.close()
print files
# delete files
for file in files:
if os.path.exists(file):
os.remove(file)
return zippath
if __name__ == '__main__':
input_path = sys.argv[1]
out_dir = sys.argv[2]
sql = sys.argv[3]
# Make a layer from the feature class
arcpy.MakeFeatureLayer_management(input_path, "lyr")
# Within selected features, further select only those cities which have a population > 10,000
arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", sql)
# Generate file names
input_name = getFileName(input_path, out_dir)
if not input_name:
arcpy.SetParameter(3, 'None')
sys.exit()
out_name = input_name + '.shp'
out_path = os.path.join(out_dir, out_name)
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", out_path)
# Zip files
zip_path = shpFilesToZips(out_path)
arcpy.SetParameter(3, zip_path)
Arcgis for js 方面
通过arcpy.js生成的gp工具发布后如下图:
主要是两个参数,一个是sql,一个是featureSet
arcgis api for js 代码如下
$("#download").click(function(){
console.log(resultLayer);
var layer=layui.layer;
var ids=[];//调用输出工具使用的参数
var graphics=resultLayer.graphics;
var featureSet=new FeatureSet();
if(graphics.items.length==0){
layer.open({
content: '导出失败,检测结果为空!',
scrollbar: false
});
}else if(graphics.items.length>0){
featureSet.features=graphics.items;
for(var i=0;i<graphics.items.length;i++){
ids.push(graphics.items[i].attributes.OBJECTID);
}
downloadEle(ids,featureSet);
}
});
var outputOptins;
function downloadEle(ids,featureSet){
var flow = layer.msg('正在准备导出...', {
icon: 16
,shade: [0.5, '#393D49']
});
var expression="OBJECTID =" + ids[0];
var outputUrl="http://localhost:6080/arcgis/rest/services/output_tool/GPServer/output";//输出gp
var outputGp = new Geoprocessor({
url: outputUrl,
outSpatialReference: {
wkid: 4548
}
});
for(var i = 1; i < ids.length; i++) {
expression += " or OBJECTID=" + ids[i];
}
outputOptins={
sql:expression,
polygon:featureSet
};
outputGp.submitJob(outputOptins).then(function(jobInfo) {
console.log("output_jobInfo",jobInfo);
var options = {
statusCallback: function(jobInfo1) {
console.log("正在计算",jobInfo1);
var flow = layer.msg('正在导出到服务器...', {
icon: 16
,shade: [0.5, '#393D49']
});
}
};
// once the job completes, add resulting layer to map
outputGp.waitForJobCompletion(jobInfo.jobId, options).then(function( jobInfo2) {
console.log("计算状态",jobInfo2);
if(jobInfo2.jobStatus=="job-succeeded"){
layer.open({
content: '导出到服务器成功!可下载到本地',
scrollbar: false
});
outputGp.getResultData(jobInfo2.jobId,"out_path").then(downResult);
}else{
layer.open({
content: '导出失败!',
scrollbar: false
});
}
});
});
};