Arcgis for js之GP實現緩衝區計算

來源:互聯網
上載者:User

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(提示,計算失敗!);            }        }

 

 

實現後效果:

輸入距離

點計算成功

線緩衝

 

 

面緩衝

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.