Arcgis for js之GP實現緩衝區計算
概述:
GP服務的存在使得在Web端使用ArcGIS 提供的空間分析,而這些分析的能力是和案頭中的一樣的。因此,是Arcgis for js的一個重點,也是一個痛點。因此,在本文講述如何發布並在代碼中調用GP服務,實現緩衝區的分析計算。
模型參數:
Parameters: Parameter: input Data Type: GPFeatureRecordSetLayer Display Name input Description: input buffer Direction: esriGPParameterDirectionInput Default Value:Geometry Type: esriGeometryPoint HasZ: false HasM: false Spatial Reference: 4326 (4326) Fields:FID ( type: esriFieldTypeOID , alias: FID )name ( type: esriFieldTypeString , alias: name , length: 100 )id ( type: esriFieldTypeDouble , alias: id )Features: None.Parameter Type: esriGPParameterTypeRequired Category: Parameter: output Data Type: GPFeatureRecordSetLayer Display Name output Description: ouput feature Direction: esriGPParameterDirectionOutput Default Value:Geometry Type: esriGeometryPolygon HasZ: false HasM: false Spatial Reference: 4326 (4326) Fields:FID ( type: esriFieldTypeOID , alias: FID )name ( type: esriFieldTypeString , alias: name , length: 100 )id ( type: esriFieldTypeDouble , alias: id )BUFF_DIST ( type: esriFieldTypeDouble , alias: BUFF_DIST )Shape_Length ( type: esriFieldTypeDouble , alias: Shape_Length )Shape_Area ( type: esriFieldTypeDouble , alias: Shape_Area )Features: None.Parameter Type: esriGPParameterTypeRequired Category: Parameter: Distance__value_or_field_ Data Type: GPLinearUnit Display Name Distance Description: Distance Direction: esriGPParameterDirectionInput Default Value: 50.0 (esriKilometers) Parameter Type: esriGPParameterTypeRequired Category:
說明:
模型中有三個參數:1、輸入;2、輸出;3、緩衝距離單位或者欄位。
代碼實現:
1、添加繪製工具並定義事件
toolbar = new Draw(map); dojo.connect(toolbar, 'onDrawEnd', drawEnd); $(#point).on(click,function(){ map.graphics.clear(); toolbar.activate(esri.toolbars.Draw.POINT); }); $(#polyline).on(click,function(){ map.graphics.clear(); toolbar.activate(esri.toolbars.Draw.POLYLINE); }); $(#polygon).on(click,function(){ map.graphics.clear(); toolbar.activate(esri.toolbars.Draw.POLYGON); });2、繪製結束後提交計算
/** * 繪製結束 * @param geometry */ function drawEnd(geometry) { $.messager.prompt('提示資訊', '請輸入緩衝區範圍:', function(dist){ if (dist){ $.messager.progress({ text:計算中,請稍後... }); toolbar.deactivate(); var symbol = null; if(geometry.type===point){ symbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new esri.Color([255,0,0]), 1), new esri.Color([0,255,0,0.25])); } else if(geometry.type===polyline){ symbol=new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255, 0, 0]), 2); } else{ symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255, 0, 0]), 2), new dojo.Color([255, 255, 0, 0.25])); } var graphic = new esri.Graphic(geometry, symbol); map.graphics.add(graphic); tojob(graphic,dist); } }); } function tojob(graphic,distance) { //第一步構造GP var gpUrl = 'http://localhost:6080/arcgis/rest/services/buffer/GPServer/buffer'; gp = new esri.tasks.Geoprocessor(gpUrl); //第二步,構造參數 //我們通過上面,瞭解到GPFeatureRecordSetLayer對應FeatureSet var features = []; features.push(graphic); var featureset = new esri.tasks.FeatureSet(); featureset.features = features; //構造緩衝長度,這裡的單位是可以更改的,我使用的是度,簡單一些 var Dis = new esri.tasks.LinearUnit(); Dis.distance = distance; Dis.units = esri.Units.KILOMETERS; var parms = { input : featureset, Distance__value_or_field_ : Dis }; //這裡函數是非同步,使用函數是submitJob,同步的使用的是execute。 //成功之後,調用jobResult,建議看一下這個參數。 gp.submitJob(parms, jobResult); }3、計算成功將結果繪製出來
/** * 計算完成 * @param result */ function jobResult(result) { var jobId = result.jobId; var status = result.jobStatus; if(status === esri.tasks.JobInfo.STATUS_SUCCEEDED) { //成功之後,將其中的結果取出來,當然這也是參數名字。 //在模型中,想要取出中間結果,需要設定為模型參數 gp.getResultData(jobId, output, addResults); } } function addResults(results){ $.messager.progress('close'); var features = results.value.features; if(features.length>0) { for (var i = 0, length = features.length; i != length; ++i) { var feature = features[i]; var polySymbolRed = new esri.symbol.SimpleFillSymbol(); polySymbolRed.setOutline(new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 0, 0, 0.5]), 1)); polySymbolRed.setColor(new dojo.Color([255, 0, 0, 0.5])); feature.setSymbol(polySymbolRed); map.graphics.add(feature); } $.messager.alert(提示,計算成功!); } else{ $.messager.alert(提示,計算失敗!); } }
實現後效果:
輸入距離
點計算成功
線緩衝
面緩衝