Arcgis for js之GP实现缓冲区计算

概述:

GP服务的存在使得在Web端使用ArcGIS 提供的空间分析,而这些分析的能力是和桌面中的一样的。因此,是Arcgis for js的一个重点,也是一个难点。因此,在本文讲述如何发布并在代码中调用GP服务,实现缓冲区的分析计算。


简介:

框架介绍参考文章:http://www.cnblogs.com/HPhone/archive/2012/11/05/2755833.html

服务发布参考文章:http://www.cnblogs.com/HPhone/archive/2012/11/18/2775492.html


模型参数:

[plain]  view plain copy print ?
  1. Parameters:   
  2. Parameter: input   
  3. Data Type: GPFeatureRecordSetLayer   
  4. Display Name input   
  5. Description: input buffer   
  6. Direction: esriGPParameterDirectionInput   
  7. Default Value:  
  8. Geometry Type: esriGeometryPoint   
  9. HasZ: false   
  10. HasM: false   
  11. Spatial Reference: 4326  (4326)   
  12.   
  13. Fields:  
  14. FID ( type: esriFieldTypeOID , alias: FID )  
  15. name ( type: esriFieldTypeString , alias: name , length: 100 )  
  16. id ( type: esriFieldTypeDouble , alias: id )  
  17. Features: None.  
  18.   
  19.   
  20. Parameter Type: esriGPParameterTypeRequired   
  21. Category:   
  22.   
  23. Parameter: output   
  24. Data Type: GPFeatureRecordSetLayer   
  25. Display Name output   
  26. Description: ouput feature   
  27. Direction: esriGPParameterDirectionOutput   
  28. Default Value:  
  29. Geometry Type: esriGeometryPolygon   
  30. HasZ: false   
  31. HasM: false   
  32. Spatial Reference: 4326  (4326)   
  33.   
  34. Fields:  
  35. FID ( type: esriFieldTypeOID , alias: FID )  
  36. name ( type: esriFieldTypeString , alias: name , length: 100 )  
  37. id ( type: esriFieldTypeDouble , alias: id )  
  38. BUFF_DIST ( type: esriFieldTypeDouble , alias: BUFF_DIST )  
  39. Shape_Length ( type: esriFieldTypeDouble , alias: Shape_Length )  
  40. Shape_Area ( type: esriFieldTypeDouble , alias: Shape_Area )  
  41. Features: None.  
  42.   
  43.   
  44. Parameter Type: esriGPParameterTypeRequired   
  45. Category:   
  46.   
  47. Parameter: Distance__value_or_field_   
  48. Data Type: GPLinearUnit   
  49. Display Name Distance   
  50. Description: Distance   
  51. Direction: esriGPParameterDirectionInput   
  52. Default Value: 50.0   (esriKilometers)   
  53. Parameter Type: esriGPParameterTypeRequired   
  54. Category:   
说明:

模型中有三个参数:1、输入;2、输出;3、缓冲距离单位或者字段。


代码实现:

1、添加绘制工具并定义事件

[javascript]  view plain copy print ?
  1. toolbar = new Draw(map);  
  2. dojo.connect(toolbar, 'onDrawEnd', drawEnd);  
  3. $("#point").on("click",function(){  
  4.     map.graphics.clear();  
  5.     toolbar.activate(esri.toolbars.Draw.POINT);  
  6. });  
  7. $("#polyline").on("click",function(){  
  8.     map.graphics.clear();  
  9.     toolbar.activate(esri.toolbars.Draw.POLYLINE);  
  10. });  
  11. $("#polygon").on("click",function(){  
  12.     map.graphics.clear();  
  13.     toolbar.activate(esri.toolbars.Draw.POLYGON);  
  14. });  
2、绘制结束后提交计算

[javascript]  view plain copy print ?
  1. /** 
  2.  * 绘制结束 
  3.  * @param geometry 
  4.  */  
  5. function drawEnd(geometry) {  
  6.     $.messager.prompt('提示信息''请输入缓冲区范围:'function(dist){  
  7.         if (dist){  
  8.             $.messager.progress({  
  9.                 text:"计算中,请稍后..."  
  10.             });  
  11.             toolbar.deactivate();  
  12.             var symbol = null;  
  13.             if(geometry.type==="point"){  
  14.                 symbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10,  
  15.                         new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,  
  16.                                 new esri.Color([255,0,0]), 1),  
  17.                         new esri.Color([0,255,0,0.25]));  
  18.             }  
  19.             else if(geometry.type==="polyline"){  
  20.                 symbol=new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255, 0, 0]), 2);  
  21.             }  
  22.             else{  
  23.                 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]));  
  24.             }  
  25.             var graphic = new esri.Graphic(geometry, symbol);  
  26.             map.graphics.add(graphic);  
  27.             tojob(graphic,dist);  
  28.         }  
  29.     });  
  30. }  
  31.           
  32. function tojob(graphic,distance) {  
  33.     //第一步构造GP  
  34.     var gpUrl = 'http://localhost:6080/arcgis/rest/services/buffer/GPServer/buffer';  
  35.     gp = new esri.tasks.Geoprocessor(gpUrl);  
  36.     //第二步,构造参数  
  37.     //我们通过上面,了解到GPFeatureRecordSetLayer对应FeatureSet  
  38.     var features = [];  
  39.     features.push(graphic);  
  40.     var featureset = new esri.tasks.FeatureSet();  
  41.     featureset.features = features;  
  42.     //构造缓冲长度,这里的单位是可以更改的,我使用的是度,简单一些  
  43.     var Dis = new esri.tasks.LinearUnit();  
  44.     Dis.distance = distance;  
  45.     Dis.units = esri.Units.KILOMETERS;  
  46.     var parms = {  
  47.         input : featureset,  
  48.         Distance__value_or_field_ : Dis  
  49.     };  
  50.     //这里函数是异步的,使用函数是submitJob,同步的使用的是execute。  
  51.     //成功之后,调用jobResult,建议看一下这个参数。  
  52.     gp.submitJob(parms, jobResult);  
  53. }  
3、计算成功将结果绘制出来

[javascript]  view plain copy print ?
  1. /** 
  2.  * 计算完成 
  3.  * @param result 
  4.  */  
  5. function jobResult(result) {  
  6.     var jobId = result.jobId;  
  7.     var status = result.jobStatus;  
  8.     if(status === esri.tasks.JobInfo.STATUS_SUCCEEDED) {  
  9.         //成功之后,将其中的结果取出来,当然这也是参数名字。  
  10.         //在模型中,想要取出中间结果,需要设置为模型参数  
  11.         gp.getResultData(jobId, "output", addResults);  
  12.     }  
  13. }  
  14. function addResults(results){  
  15.     $.messager.progress('close');  
  16.     var features = results.value.features;  
  17.     if(features.length>0) {  
  18.         for (var i = 0, length = features.length; i != length; ++i) {  
  19.             var feature = features[i];  
  20.             var polySymbolRed = new esri.symbol.SimpleFillSymbol();  
  21.             polySymbolRed.setOutline(new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 0, 0, 0.5]), 1));  
  22.             polySymbolRed.setColor(new dojo.Color([255, 0, 0, 0.5]));  
  23.             feature.setSymbol(polySymbolRed);  
  24.             map.graphics.add(feature);  
  25.         }  
  26.         $.messager.alert("提示","计算成功!");  
  27.     }  
  28.     else{  
  29.         $.messager.alert("提示","计算失败!");  
  30.     }  
  31. }  


实现后效果:


输入距离


点计算成功


线缓冲




面缓冲


转载自:http://blog.csdn.net/gisshixisheng/article/details/49901807

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值