featureUpdate.js 6.11 KB
/*
* 空间图层数据添加、修改、删除
* */
import {loadModules} from "esri-loader"

export default {
    methods:{
        addGraphic(url,graphic,callBackFunction){
            loadModules([
                "esri/layers/FeatureLayer",
                "esri/Graphic",
                "esri/geometry/Point",
                "esri/geometry/Polygon",
                "esri/geometry/Polyline"
            ]).then(([
                                     FeatureLayer,
                                     Graphic,
                                     Point,
                                     Polygon,
                                     Polyline
                                 ])=>{
                var featureLayer = new FeatureLayer({
                    url:url
                })
                console.log(featureLayer);
                console.log(graphic);
                var saveGraphics = [];
                if(!Array.isArray(graphic)){
                    graphic = [graphic];
                }
                for(var i = 0;i < graphic.length;i++){
                    var geo = null;
                    if(!graphic[i].geometry){
                        geo = null;
                    }else if(graphic[i].geometry.type == 'point'){
                        geo = new Point(graphic[i].geometry);
                    }else if(graphic[i].geometry.type == 'polyline'){
                        geo = new Polyline(graphic[i].geometry);
                    }else if(graphic[i].geometry.type == 'polygon'){
                        geo = new Polygon(graphic[i].geometry);
                    }
                    var saveGraphic = new Graphic({
                        attributes:graphic[i].attributes,
                        geometry:geo
                    });
                    saveGraphics.push(saveGraphic);
                } 
                featureLayer.applyEdits({
                    addFeatures:saveGraphics 
                }).then(function (res){ 
                    if(callBackFunction && typeof callBackFunction == 'function'){
                        callBackFunction(res);
                    }
                })
            }).catch( err => {
                throw (err);
            })
        },
        updateGraphic(url,graphic,callBackFunction){
            loadModules([
                "esri/layers/FeatureLayer",
                "esri/Graphic",
                "esri/geometry/Point",
                "esri/geometry/Polygon",
                "esri/geometry/Polyline"
            ]).then(([
                         FeatureLayer,
                         Graphic,
                                     Point,
                                     Polygon,
                                     Polyline
                     ])=>{
                var featureLayer = new FeatureLayer({
                    url:url
                })
                var updGraphics = [];
                if(!Array.isArray(graphic)){
                    graphic = [graphic];
                }
                for(var i = 0;i < graphic.length;i++){
                    var geo = null;
                    if(!graphic[i].geometry){
                        geo = new Polygon();
                    }else if(graphic[i].geometry.type == 'point'){
                        geo = new Point(graphic[i].geometry);
                    }else if(graphic[i].geometry.type == 'polyline'){
                        geo = new Polyline(graphic[i].geometry);
                    }else if(graphic[i].geometry.type == 'polygon'){
                        geo = new Polygon(graphic[i].geometry);
                    }
                    var updGraphic = new Graphic({
                        attributes:graphic[i].attributes,
                        geometry:geo
                    });
                    updGraphics.push(updGraphic);
                }

                featureLayer.applyEdits({
                    updateFeatures:updGraphics
                }).then(function (res){
                    if(callBackFunction && typeof callBackFunction == 'function'){
                        callBackFunction(res);
                    }
                })
            }).catch( err => {
                throw (err);
            })
        },
        delGraphic(url,graphic,callBackFunction){
            loadModules([
                "esri/layers/FeatureLayer",
                "esri/Graphic",
                "esri/geometry/Point",
                "esri/geometry/Polygon",
                "esri/geometry/Polyline"
            ]).then(([
                         FeatureLayer,
                                     Graphic,
                                     Point,
                                     Polygon,
                                     Polyline
                     ])=>{
                var featureLayer = new FeatureLayer({
                    url:url
                })
                var delGraphics = [];
                if(!Array.isArray(graphic)){
                    graphic = [graphic];
                }
                for(var i = 0;i < graphic.length;i++){
                    var geo = null;
                    if(!graphic[i].geometry){
                        geo = null;
                    }else if(graphic[i].geometry.type == 'point'){
                        geo = new Point(graphic[i].geometry);
                    }else if(graphic[i].geometry.type == 'polyline'){
                        geo = new Polyline(graphic[i].geometry);
                    }else if(graphic[i].geometry.type == 'polygon'){
                        geo = new Polygon(graphic[i].geometry);
                    }
                    var delGraphic = new Graphic({
                        attributes:graphic[i].attributes,
                        geometry:geo
                    });
                    delGraphics.push(delGraphic);
                }


                featureLayer.applyEdits({
                    deleteFeatures:delGraphics
                }).then(function (res){
                    if(callBackFunction && typeof callBackFunction == 'function'){
                        callBackFunction(res);
                    }
                })
            }).catch( err => {
                throw (err);
            })
        }
    }
}