mapTools.js 7.18 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);
				view.center = graphic.geometry.extent.center;
                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:"identifyLayer"
                    })
                    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);

            })
        },
		//定位到一个点上
		postionToPoint(viewId,x,y){
			var self = this;
			loadModules([
				"esri/geometry/Point",
				"esri/symbols/PictureMarkerSymbol",
				"esri/geometry/coordinateFormatter",
				"esri/Graphic"
			]).then(([
				Point,
				PictureMarkerSymbol,
				coordinateFormatter,
				Graphic
			])=>{
				var point = new Point({
					x:x,
					y:y,
					
				});
				var view = maps[viewId];
				var symbol = new PictureMarkerSymbol({
					url:require('@assets/images/map/postion.png'),
					width:"32px",
					height:'32px'
				});
				view.graphics.removeAll();
				view.graphics.add(new Graphic({
					geometry:point,
					symbol:symbol
				}));
				view.center = point;
			}).catch(err => {
				console.log(err);
			})
		},
		//清除定位图层
		clearPostionLayer(viewId){
			var view = maps[viewId];
			view.graphics.removeAll();
		},
		//打印地图
		printMap(viewId,outSpatialReference,serverUrl,priTemParames,callBackFunction){
			var self = this;
			loadModules([
				"esri/tasks/PrintTask",
				"esri/tasks/support/PrintParameters",
				"esri/tasks/support/PrintTemplate"
			]).then(([
				PrintTask,
				PrintParameters,
				PrintTemplate
			]) => {
				var view = maps[viewId],
				    printTask = new PrintTask({
						url:serverUrl
					}),
				    template  = new PrintTemplate({
						layout:priTemParames.layOut,
						format:priTemParames.format,
						layoutOptions: {
							titleText:priTemParames.title,
							scalebarUnit:'Meters',
							authorText:priTemParames.author
						}
					}),
				    paramets = new PrintParameters({
						view:view,
						template:template
					});
				paramets.outSpatialReference = outSpatialReference ? outSpatialReference : view.spatialReference;
				printTask.execute(paramets).then(res => {
					if(callBackFunction && typeof callBackFunction == 'function'){
						callBackFunction(res);
					}
				});
			}).catch(err => {
				console.log(err);
			})
		}
    }
}