mapTools.js 5.24 KB
import {loadModules} from 'esri-loader'
import {maps} from '@/libs/map/mapUtils'
import graphicSymbol from '@/assets/json/graphicSymbol.json'
import point from "shapefile/shp/point";
import queryUtils from "@libs/map/queryUtils";
import layers from '@/assets/json/layers.json'
import arcgisParser from 'terraformer-arcgis-parser'
import wktParse from 'terraformer-wkt-parser'

export default {
    methods:{
        getLayerByName(name) {
            for (var i = 0; i < layers.length; i++) {
                if (layers[i].layerName == name) {
                    return layers[i];
                }
            }
            return null;
        },
        //创建buffer图形
        createBuffer(geometry,distance,viewId,callBackFunction){
            loadModules([
                "esri/geometry/geometryEngine",
                "esri/Graphic",
                "esri/layers/GraphicsLayer"
            ]).then(([
                                     geometryEngine,
                                     Graphic,
                                     GraphicsLayer
                                 ])=>{
                var view = maps[viewId];
                var bufferGeometry = geometryEngine.buffer(geometry,distance,'meters',true);
                var layer = view.map.findLayerById("identifyLayer");
                if(layer){
                    layer.removeAll();
                }else {
                    layer = new GraphicsLayer({
                        id:"identifyLayer"
                    })
                    view.map.add(layer);
                }
                var symbol = geometry.type == 'point' ? graphicSymbol.pointSymbol.bufferSymbol : graphicSymbol.fillSymbol.bufferSymbol;
                var oldGraphic = new Graphic({
                    geometry:geometry,
                    symbol:symbol
                })
                layer.add(oldGraphic);
                var graphic = new Graphic({
                    geometry:bufferGeometry,
                    symbol:graphicSymbol.fillSymbol.bufferGeoSymbol
                })
                layer.add(graphic);
                if(callBackFunction && typeof callBackFunction == 'function'){
                    callBackFunction(bufferGeometry);
                }
            }).catch(err => {
                console.log(err);
            })
        },
        queryAttributes(url,where,callbackFunction){
            queryUtils.methods.queryByWhere(url,where,null,false,"",null,function (res) {
                var features = res.features;
                if(callbackFunction && typeof callbackFunction == 'function'){
                    callbackFunction(features);
                }
            })
        },
        //清除缓冲图层
        clearBufferLayer(viewId){
            var view = maps[viewId];
            var layer = view.map.findLayerById("identifyLayer");
            if(layer){
                layer.removeAll();
            }
        },
        //查询地籍区/地籍子区根据区域代码
        queryDjqByDm(url,type,dm,callBackFunction){
            var where = "";
            if(type == 'djq'){
                where = {"DJQDM":dm}
            }else if(type == 'djzq'){
                where = {"DJZQDM":dm}
            }
            queryUtils.methods.queryByWhere(url,where,null,true,"","",function (res) {
                var features = res.features;
                if(callBackFunction && typeof callBackFunction == 'function'){
                    callBackFunction(features);
                }
            });
        },
        parseWktToArc(wkt){
            var primitive  = wktParse.parse(wkt);
            /*if(primitive.type == "MultiPolygon"){
                primitive.type = "Polygon"
            }*/
            return arcgisParser.convert(primitive)
        },
        //添加添加元素和覆盖的元素到地图上
        addOverLayer(geometry){
            var view = maps["testMap"];
            loadModules([
                "esri/Graphic",
                "esri/geometry/Polygon",
                "esri/layers/GraphicsLayer",
                "esri/geometry/geometryEngineAsync",
                "esri/geometry/Extent"
            ]).then(([
                         Graphic,
                         Polygon,
                         GraphicsLayer,
                         geometryEngineAsync,
                         Extent
                     ])=>{
                var graphic = new Graphic({
                    geometry:geometry
                })
                var layer = view.map.findLayerById("identifyLayer");
                if(layer){
                    layer.removeAll();
                }else {
                    layer = new GraphicsLayer({
                        id:"overLayer"
                    })
                    view.map.add(layer);
                }
                var impotSymbol = graphicSymbol.fillSymbol.importSymbol;
                graphic.symbol = impotSymbol;
                layer.add(graphic);
                var extent =  new Extent(JSON.parse(JSON.stringify(graphic.geometry.extent)))
                extent.spatialReference = view.spatialReference;
                //  view.extent = extent;
                view.center = extent.center;
                view.zoom = 15;
            }).catch(err => {
                console.log(err);

            })
        },
    }
}