概述:

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


实现后效果:


输入距离


点计算成功


线缓冲




面缓冲








posted on 2015-11-18 07:38  LZU-GIS  阅读(324)  评论(0编辑  收藏  举报