写个webgis调用gp工具的代码
时间: 2023-10-25 09:08:57 浏览: 43
以下是一个简单的WebGIS调用GP工具的代码,使用JavaScript和ArcGIS API for JavaScript实现:
```javascript
require([
"esri/tasks/Geoprocessor",
"esri/tasks/FeatureSet",
"esri/geometry/Point",
"esri/symbols/SimpleMarkerSymbol",
"esri/Color",
"dojo/domReady!"
], function(Geoprocessor, FeatureSet, Point, SimpleMarkerSymbol, Color) {
// 创建GP工具对象
var gp = new Geoprocessor("http://myserver/arcgis/rest/services/MyGPTool/GPServer/MyGPTool");
// 创建一个点几何对象
var point = new Point(-118.15, 33.80);
// 创建一个简单符号
var symbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 10,
new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
new Color([255,0,0]), 1), new Color([0,0,0,0.25]));
// 在地图上显示点
var graphic = new Graphic(point, symbol);
map.graphics.add(graphic);
// 创建GP工具所需的输入参数
var inputParams = {
"Input_Point": point
};
// 创建FeatureSet对象作为GP工具的输入
var featureSet = new FeatureSet();
featureSet.features = [graphic];
// 设置GP工具的参数
gp.setOutSpatialReference(new SpatialReference(4326));
gp.setOutputSpatialReference(new SpatialReference(4326));
gp.setProcessSpatialReference(new SpatialReference(4326));
gp.setUpdateDelay(500);
gp.setParameter("Input_Point", featureSet);
// 执行GP工具
gp.submitJob(inputParams, function(jobInfo) {
// GP工具执行成功后的回调函数
gp.getResultData(jobInfo.jobId, "Output_Feature_Class", function(output) {
// 处理GP工具输出结果
var featureLayer = new FeatureLayer(output.value.url, {
id: "MyGPResultLayer"
});
map.addLayer(featureLayer);
});
}, function(error) {
// GP工具执行失败后的回调函数
console.log(error);
});
});
```
这个代码中,我们使用了ArcGIS API for JavaScript提供的`Geoprocessor`类来创建一个GP工具对象,然后设置GP工具的输入参数和执行参数,并调用`submitJob`方法来执行GP工具。在GP工具执行成功后的回调函数中,我们可以使用`getResultData`方法获取GP工具的输出结果,并将其显示在地图上。