geoUtils.js 8.44 KB
/*
* 图形相关的操作 js
* */

import layers from '@/assets/json/layers.json'
import queryUtils from "@libs/map/queryUtils";
import identifyUtils from '@/libs/map/IdentifyUtils'
import {loadModules} from "esri-loader"
import arcgisParser from 'terraformer-arcgis-parser'
import wktParse from 'terraformer-wkt-parser'


export  default {
    data(){
        return{

        }
    },
    methods: {
        getLayerByName(name) {
            for (var i = 0; i < layers.length; i++) {
                if (layers[i].layerName == name) {
                    return layers[i];
                }
            }
            return null;
        },
        queryGeoByBsm(bsm, type, callBackFunction) {
            var layer = null;
            if (type == 'zd') {
                layer = this.getLayerByName("ZDJBXX");
            } else if (type == 'zrz') {
                layer = this.getLayerByName("ZRZ");
            } else {
                console.log("未定义类型!!");
                return;
            }
            if (!layer) {
                console.log("没有找到图层,不能查询");
                return;
            }
            queryUtils.methods.queryByWhere(layer.layerUrl+"/"+layer.id, {"BSM": bsm}, null, true, null, null, function (res) {
                var features = res.features;
                if (callBackFunction && typeof callBackFunction == 'function') {
                    callBackFunction(features);
                }
            });
        },
        //生成介质点
        craetJZPoint(graphic) {
            var geomtry = graphic.geometry,rings = geomtry.rings[0];
            var pointInfos = [];
            this.getPointByRings(rings,pointInfos);
            return pointInfos;
        },
        getPointByRings(rings,pointInfos){
            for(var i = 0;i < rings.length;i++){
              var children = rings[i];
              if(children.length == 2 && typeof children[0] == 'number'){
                  var obj = {};
                  obj.jzdh = "j"+pointInfos.length;
                  obj.sxh = pointInfos.length;
                  obj.x = children[0];
                  obj.y = children[1];
                  pointInfos.push(obj);
              }else {
                  this.getPointByRings(children,pointInfos);
              }
            }
        },
        //生成介质线
        createJZLine(graphic,callBackFunction) {
            var self = this;
            loadModules([
                "esri/geometry/support/geodesicUtils",
                "esri/geometry/Point",
                "esri/geometry/Polyline"

            ]).then(([
                                     geodesicUtils,
                                     Point,
                                     Polyline
                                 ])=>{
                //取得各个坐标点 然后生成外围坐标线
                var geometry = graphic.geometry,
                    rings = geometry.rings,
                    points = [];
                if(rings.length > 0){
                    for(var i = 0;i < rings.length;i++){
                        var danPoints = [];
                        self.getPointByRings(rings[i],danPoints);
                        points.push(danPoints);
                    }
                }else {
                    var danPoints = [];
                    self.getPointByRings(rings,danPoints);
                    points.push(danPoints);
                }
               var lines = [];
                //meters
                for(var i = 0;i < points.length;i++){
                    for(var j = 0;j < points[i].length;j++){
                        if(j < (points[i].length - 1)){
                            const join = geodesicUtils.geodesicDistance(
                                new Point({ x: points[i][j].x, y: points[i][j].y }),
                                new Point({ x: points[i][j+1].x, y: points[i][j+1].y }),
                                "meters"
                            );
                            const { distance, azimuth } = join;

                            var obj = {
                                startPoint:points[i][j].x +"," +points[i][j].y ,
                                endPoint:points[i][j+1].x +","+points[i][j+1].y,
                                qdh:points[i][j].sxh,
                                zdh:points[i][j+1].sxh,
                                distance:distance,
                                jzxlx:"",
                                jzxwz:"",
                                jzxxz:"",
                                remark:""
                            }
                            lines.push(obj);
                        }else{
                            const join = geodesicUtils.geodesicDistance(
                                new Point({ x: points[i][j].x, y: points[i][j].y }),
                                new Point({ x: points[i][0].x, y: points[i][0].y }),
                                "meters"
                            );
                            const { distance, azimuth } = join;
                            var obj = {
                                startPoint:points[i][j].x +"," +points[i][j].y ,
                                endPoint:points[i][0].x +"," +points[i][0].y ,
                                qdh:points[i][j].sxh,
                                zdh:points[i][0].sxh,
                                distance:distance,
                                jzxlx:"",
                                jzxwz:"",
                                jzxxz:"",
                                remark:""
                            }
                            lines.push(obj);
                        }
                    }
                }
               if(callBackFunction && typeof callBackFunction == "function"){
                   callBackFunction(lines);
               }
            }).catch( err => {
                throw (err);
            })
        },
        //wkt转换成arcgis
        parseWktToArc(wkt){
            var primitive  = wktParse.parse(wkt);
            /*if(primitive.type == "MultiPolygon"){
                primitive.type = "Polygon"
            }*/
            return arcgisParser.convert(primitive)
        },
        //导入空间图形是 先判断数据是否跨界
        geoJoint(geometry,callBacFunction){
            var self = this;
            loadModules([
                "esri/geometry/geometryEngine",
                "esri/geometry/Polygon"
            ]).then(([
                                     geometryEngine,
                                     Polygon
                                 ]) => {
                var djqLayer = null,djzqLayer = null,xjzqLayer = null;
                djqLayer = self.getLayerByName("DJQ");
                djzqLayer = self.getLayerByName("DJZQ");
                xjzqLayer = self.getLayerByName("XJZQ");
                var layerIds = [];
                layerIds.push(djqLayer.id);
                layerIds.push(djzqLayer.id);
                layerIds.push(xjzqLayer.id);
                var polygon =  new Polygon(geometry);
                identifyUtils.methods.identify(djqLayer.layerUrl,layerIds,polygon,function (res) {
                    var results = res.results;
                    var isJoint = false,layerName = "",message="";
                    if(!results || results.length == 0){
                        callBacFunction(true,"不在行政区内,请检查空间位置信息!!!");
                    }
                    for(var i = 0;i < results.length;i++){
                        var feature = results[i].feature;
                       var flag =  geometryEngine.disjoint(polygon,feature.geometry);
                       if(flag){
                           isJoint = true;
                           layerName = results[i].layerName;
                           switch (layerName) {
                               case 'DJQ':message = "地块跨越地籍区,数据不合法!!!";break;
                               case 'DJZQ':message = "地块跨越地籍子区,数据不合法!!!";break;
                               case 'XJZQ':message = "地块跨越行政区,数据不合法!!!";break;
                           }
                           break
                       }
                    }
                    if(callBacFunction && typeof callBacFunction == "function"){
                        callBacFunction(isJoint,message);
                    }
                })
            }).catch(err => {
                console.log(err);
                throw (err);
            })
        }

    }
}