前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Arcgis for js之GP实现缓冲区计算

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

作者头像
lzugis
发布2018-10-23 12:25:50
1.2K0
发布2018-10-23 12:25:50
举报

概述:

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

模型参数:

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("提示","计算失败!");
            }
        }

实现后效果:

输入距离

点计算成功

线缓冲

面缓冲

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年11月18日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档