月球大数据地理空间分析展示平台-【前端】-月球2期前端
Surpriseplus
2023-09-05 8b8f88d7d8d713a1355d9849f467e3a2dc0c22fe
src/assets/js/Map/menuTool.js
@@ -1,11 +1,17 @@
import * as turf from "@turf/turf";
import WKT from 'terraformer-wkt-parser'
import WKT from "terraformer-wkt-parser";
import rightServer from "./rightServer";
import config from "../../../../public/config/config";
import CryptoJS from "crypto-js";
// import { Store } from "vuex";
import store from "@/store";
const menuTool = {
  toolMenu: null,
  toolFlag: null,
  oldLeftMenuId: null,
  bufferSize: 100,
  squareOjb: [],
  localPoint: null,
  colorAll: {
    point: SmartEarth.Cesium.Color.fromCssColorString("#ff0000"),
    polyline: SmartEarth.Cesium.Color.fromCssColorString("#ffff0050"),
@@ -47,20 +53,32 @@
      case "c6": //多边形
        this.createSimpleGraphic("polygon");
        break;
      case "d2"://等高线分析
        break
      case "d3"://坡度分析
        break
      case "d2": //等高线分析
        this.setcontour();
        break;
      case "d3": //坡度分析
        this.setSlopeMap();
        break;
      case "d4": //土方量计算
        this.Volumetric();
        break;
      case "d5": //影像对比
        this.CurtainContrast();
        break;
      case "e1"://快照
      case "d6": //阴影分析
        this.shadowAnalysis();
        break;
      case "e1": //快照
        this.snapshot();
        break;
      case "f1"://空间查询
      case "f2": //点查询
        this.spatialPointQuery();
        break;
      case "f3": //线查询
        this.spatialLineQuery();
        break;
      case "f4": //面查询
        this.spatialQuery();
        break;
      case "g1": //坐标定位
@@ -68,51 +86,200 @@
        break;
    }
  },
  shadowAnalysis() {
    if (earthCtrl.shadows) {
      Viewer.animation.container.style.visibility = "hidden";
      Viewer.timeline.container.style.visibility = "hidden";
      Viewer.clock.shouldAnimate = false;
      return (earthCtrl.shadows = false);
    }
    Viewer.clock.shouldAnimate = true;
    Viewer.animation.container.style.visibility = "visible";
    Viewer.timeline.container.style.visibility = "visible";
    earthCtrl.shadows = true;
  },
  setLocalPosition(res) {
    if (this.localPoint) {
      this.setLocalPositionClose();
    }
    var position = {
      X: parseFloat(res.lon),
      Y: parseFloat(res.lat),
      Altitude: parseFloat(res.alt),
    };
    this.localPoint = window.sgworld.Creator.CreateImageLabel(
      position,
      config.StaticFileBaseUrl + "/Workers/image/mark.png",
      {},
      0,
      "标签点"
    );
    var id = this.localPoint.item.id;
    window.sgworld.Navigate.flyToObj(this.localPoint.item);
  },
  setLocalPositionClose() {
    this.localPoint.deleteObject();
    this.localPoint = null;
  },
  //坡度分析
  setSlopeMap() {
    const globe = Viewer.scene.globe;
    if (window.material) {
      window.material = null;
      globe.material = null;
      Viewer.scene.globe.enableLighting = false;
      return;
    }
    window.material = new Cesium.Material({
      fabric: {
        type: "ElevationRamp",
        uniforms: {
          image: config.StaticFileBaseUrl + "Assets/Images/color.png",
          minimumHeight: -10000,
          maximumHeight: 10000,
        },
      },
    });
    Viewer.scene.globe.material = window.material;
  },
  //等高线
  setcontour() {
    var globe = window.Viewer.scene.globe;
    if (window.material) {
      window.material = null;
      globe.material = null;
      return;
    }
    var contourColor = new SmartEarth.Cesium.Color(
      117 / 255,
      71 / 255,
      18 / 255,
      255 / 255
    );
    var contourUniforms = {};
    window.material = Cesium.Material.fromType("ElevationContour");
    contourUniforms = material.uniforms;
    contourUniforms.width = 1.0;
    contourUniforms.spacing = 100.0;
    contourUniforms.color = contourColor;
    globe.material = window.material;
  },
  //线查询
  spatialLineQuery() {
    var that = this;
    sgworld.Creator.createSimpleGraphic('polyline', {}, (entity) => {
      var res = entity.polyline.positions.getValue()
      var std = []
      for (var i in res) {
        var line_data = this.setCartesianToEightFour(res[i])
        std.push([line_data.lng, line_data.lat])
      }
      var line = turf.lineString(std, { name: "polyline" })
      var restVal = turf.buffer(line, this.bufferSize, { units: "meters" })
      that.setSpatialQuery(restVal)
    });
  },
  spatialPointQuery() {
    var that = this;
    sgworld.Creator.createSimpleGraphic('point', {}, (entity) => {
      var res = entity.position.getValue();
      var val = that.setCartesianToEightFour(res)
      sgworld.Creator.SimpleGraphic.clear();
      var options = {
        steps: 10,
        units: "meters",
        properties: { foo: "bar" },
      }
      var circle = turf.circle([val.lng, val.lat], this.bufferSize, options)
      that.setSpatialQuery(circle)
    });
  },
  setCartesianToEightFour(res) {
    var std = {}
    let ellipsoid = Viewer.scene.globe.ellipsoid
    let cartographic = ellipsoid.cartesianToCartographic(res)
    std.lat = Cesium.Math.toDegrees(cartographic.latitude)
    std.lng = Cesium.Math.toDegrees(cartographic.longitude)
    std.alt = cartographic.height
    return std
  },
  //空间查询
  spatialQuery() {
    sgworld.Creator.createSimpleGraphic(
      "rectangle",
      { showSize: false },
      (entity) => {
        var west = Cesium.Math.toDegrees(
          entity.rectangle._coordinates.getValue().west
        ); // 根据弧度获取到经度
        var east = Cesium.Math.toDegrees(
          entity.rectangle._coordinates.getValue().east
        ); // 根据弧度获取到纬度
        var north = Cesium.Math.toDegrees(
          entity.rectangle._coordinates.getValue().north
        ); // 根据弧度获取到经度
        var south = Cesium.Math.toDegrees(
          entity.rectangle._coordinates.getValue().south
        ); // 根据弧度获取到纬度
        sgworld.Creator.SimpleGraphic.clear();
    sgworld.Creator.createSimpleGraphic('rectangle', { showSize: false }, (entity) => {
      var west = Cesium.Math.toDegrees(
        entity.rectangle._coordinates.getValue().west
      ); // 根据弧度获取到经度
      var east = Cesium.Math.toDegrees(
        entity.rectangle._coordinates.getValue().east
      ); // 根据弧度获取到纬度
      var north = Cesium.Math.toDegrees(
        entity.rectangle._coordinates.getValue().north
      ); // 根据弧度获取到经度
      var south = Cesium.Math.toDegrees(
        entity.rectangle._coordinates.getValue().south
      ); // 根据弧度获取到纬度
      sgworld.Creator.SimpleGraphic.clear();
        var polygon = turf.polygon([
          [
            [east, north],
            [west, north],
            [west, south],
            [east, south],
            [east, north],
          ],
        ]);
        this.setSpatialQuery(polygon)
      }
    );
  },
  setSpatialQuery(res) {
    var wkt = WKT.convert(res.geometry);
      var polygon = turf.polygon([
        [
          [east, north],
          [west, north],
          [west, south],
          [east, south],
          [east, north],
        ],
      ]);
      var wkt = WKT.convert(polygon.geometry)
      window.setSpatialQuery(wkt)
    window.functionForJs({
      type: "spatialQuery",
      value: this.encr(wkt),
    });
  },
  //加密
  encr(word) {
    return CryptoJS.AES.encrypt(
      CryptoJS.enc.Utf8.parse(word),
      CryptoJS.enc.Utf8.parse(base64Decode("QSNzX2xGX3NFcnZlX2sueQ==")),
      { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }
    ).toString();
  },
  //解密
  decr(word) {
    return CryptoJS.enc.Utf8.stringify(
      CryptoJS.AES.decrypt(
        word,
        CryptoJS.enc.Utf8.parse(base64Decode("QSNzX2xGX3NFcnZlX2sueQ==")),
        { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }
      )
    ).toString();
  },
  //快照
  snapshot() {
    let canvas = window.Viewer.scene.canvas;
    let image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
    let image = canvas
      .toDataURL("image/png")
      .replace("image/png", "image/octet-stream");
    let link = document.createElement("a");
    let blob = this.dataURLtoBlob(image);
    let objurl = URL.createObjectURL(blob);
    link.download = "scene.png";
    link.href = objurl;
    link.click();
  },
  dataURLtoBlob(dataurl) {
    let arr = dataurl.split(','),
    let arr = dataurl.split(","),
      mime = arr[0].match(/:(.*?);/)[1],
      bstr = atob(arr[1]),
      n = bstr.length,
@@ -121,7 +288,6 @@
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
  },
  leftTools(res) {
    this.clearLeftTools(res);
@@ -131,29 +297,44 @@
    }
    this.oldLeftMenuId = res.id;
    switch (res.id) {
      case "l1":
        store.state.isShowMap = true;
        break;
      case "l2":
        store.state.isShowMap = true;
        break;
      case "l3": //在线制图
        store.state.isShowMap = true;
        this.setThematicMap();
        break;
      case "l4":
        store.state.isShowMap = true;
        break;
      case "l5":
        store.state.isShowMap = false
        break;
      case "l6":
        store.state.isShowMap = true;
        break;
    }
  },
  thematicTools(res) {
    switch (res.id) {
      case "t1":
        store.state.setLayerManager = !store.state.setLayerManager;
        break;
      case "t2":
        this.createSimpleGraphic("rectangle", "square");
        break;
      case "t3":
        this.delRectangle();
        break;
      case "t4":
        store.state.setExportList = !store.state.setExportList
        break;
    }
  },
@@ -165,6 +346,7 @@
  },
  // 矩形正方形
  createRectangle(entity) {
    sgworld.Creator.SimpleGraphic.clear();
    this.delRectangle();
    var west = Cesium.Math.toDegrees(
      entity.rectangle._coordinates.getValue().west
@@ -186,43 +368,23 @@
    var south1 = ss[1];
    var geometry = Cesium.Rectangle.fromDegrees(west1, south1, east1, north1);
    // var e = Viewer.entities.add({
    //   name: "正方",
    //   polygon: {
    //     hierarchy: geometry1,
    //     //height : 100000,
    //     material: Cesium.Color.RED,
    //     outline: true,
    //     outlineColor: Cesium.Color.RED,
    //   },
    // });
    const stripeMaterial = new Cesium.StripeMaterialProperty({
      evenColor: Cesium.Color.WHITE.withAlpha(0.5),
      oddColor: Cesium.Color.BLUE.withAlpha(0.5),
      repeat: 5.0,
    });
    var e = Viewer.entities.add({
      rectangle: {
        coordinates: geometry,
        outline: true,
        outlineColor: Cesium.Color.WHITE,
        outlineWidth: 4,
        stRotation: Cesium.Math.toRadians(45),
        material: stripeMaterial,
      },
    });
    this.squareOjb.push(e);
    // earthCtrl.Creator.SimpleGraphic.clear();
    Viewer.entities.remove(entity);
    window.functionForJs({
      type: 'exportMap',
      value: true
    })
    Viewer.camera.setView({
      destination: Cesium.Rectangle.fromDegrees(west, south, east, north),
    });
    setTimeout(() => {
      window.functionForJs({
        type: "exportMap",
        value: true,
      });
    }, 1000);
  },
  // 创建图形
  createSimpleGraphic(type, scene) {
    sgworld.Creator.createSimpleGraphic(type, {}, (entity) => {
      if (scene === "square") {
        this.createRectangle(entity);
@@ -235,45 +397,86 @@
    // var handle = new SmartEarth.Cesium.ScreenSpaceEventHandler(
    //   earthCtrl.viewer.scene.canvas
    // );
  },
  //坐标定位
  Coordposition() { },
  Coordposition() {
    window.functionForJs({
      type: "coordLocation",
      value: true,
    });
  },
  //影像对比
  CurtainContrast() {
    this.toolMenu = earthCtrl.analysis.createCurtainContrast({
      leftIndex: 0,
      rightIndex: 1,
    });
    if (window.DoubleScreen) {
      window.DoubleScreen && window.DoubleScreen.destroy();
      window.DoubleScreen = null;
      const compass = document.getElementsByClassName("bottom_btn")[0];
      compass.style.right = "70px";
      store.state.doubleMap = false;
      store.state.doubleMenu = false;
      return;
    }
    window.DoubleScreen = new SmartEarth.DoubleScreen(
      window.sgworld,
      {},
      SmartEarth
    );
    window.rightViewer = DoubleScreen.right._Viewer;
    window.rightViewer.imageryLayers.removeAll();
    window.rightViewer = DoubleScreen.right._Viewer;
    window.rightViewer.animation.container.style.visibility = "hidden";
    // window.rightViewer.timeline.container.style.visibility = "hidden";
    //大气层
    window.rightViewer.scene.globe.showGroundAtmosphere = false;
    window.rightViewer.scene.skyAtmosphere.show = false;
    const compass = document.getElementsByClassName("bottom_btn")[0];
    compass.style.right = "calc(50% + 70px)";
    compass.style.positions = "absolute";
    store.state.doubleMap = true;
    setTimeout(() => {
      window.functionGetLayer({
        type: "getlayer",
        value: true,
      });
    }, 100);
  },
  //土方量计算
  Volumetric() {
    const Volumetric = earthCtrl.analysis.createVolumetricMeasure({});
    Volumetric.startDrawing();
    if (window.Volumetric) {
      window.Volumetric.deleteObject();
      window.Volumetric = null;
    }
    window.Volumetric = earthCtrl.analysis.createVolumetricMeasure({});
    window.Volumetric.startDrawing();
  },
  //垂直高度
  verticalHeight() {
    sgworld.Analysis.verticalHeight(this.colorAll, () => {
    });
    sgworld.Analysis.verticalHeight(this.colorAll, () => { });
  },
  //高程测量
  heightMeasure() {
    earthCtrl.Analysis.altitude(this.colorAll, () => { });
  },
  //平面面积
  //表面面积
  planeDistance() {
    // earthCtrl.analysis.getPlaneArea(function (e) { });
    // var colorAll = this.colorAll
    // sgworld.Analysis.surfaceArea({
    //   colorAll,
    //   tin: false, // 是否显示tin三角网
    //   onlyTerrain: false // 是否只测量精细地形
    // }, () => {
    sgworld.Analysis.planeArea(this.colorAll, () => {
    // });
    earthCtrl.analysis.getSurfaceArea3d(function (e) {
      console.info(e);
    });
  },
  //表面距离
  surfaceDistance() {
    sgworld.Analysis.horizontalDistance(this.colorAll, () => {
    });
    sgworld.Analysis.horizontalDistance(this.colorAll, () => { });
  },
  //点漫游
  pointRoam() {
@@ -305,43 +508,150 @@
  },
  //清除按钮
  clearALL(id) {
    debugger
    id.value.forEach((e) => {
      this.clearTopTools(e);
    });
    this.clearTopTools();
    // id.value.forEach((e) => {
    // });
  },
  //清除方法
  clearTopTools(id) {
    if (id) {
      switch (id) {
        case "a2":
          break;
        case "a3":
          break;
        case "b2"://距离测量
        case "b3"://面积测量
        case "b4"://高程测量
        case "b5"://高度测量
          sgworld.Analysis.clearMeasure()
          break;
        case "c2": //点
        case "c3": //文字
        case "c4": //线
        case "c5": //矩形
        case "c6": //多边形
          sgworld.Creator.SimpleGraphic.clear();
          break;
        case "d5":
          this.toolMenu.removeFromMap();
          break;
        case 'f1':
          sgworld.Creator.SimpleGraphic.clear();
          break;
      }
      // this.toolFlag = null;
      this.toolMenu = null;
    store.state.doubleMap = false;
    store.state.doubleMenu = false;
    if (earthCtrl.shadows) {
      Viewer.animation.container.style.visibility = "hidden";
      Viewer.timeline.container.style.visibility = "hidden";
      Viewer.clock.shouldAnimate = false;
      return (earthCtrl.shadows = false);
    }
    if (window.Volumetric) {
      window.Volumetric.deleteObject();
      window.Volumetric = null;
    }
    if (this.localPoint) {
      this.localPoint.deleteObject();
      this.localPoint = null;
    }
    const globe = Viewer.scene.globe;
    if (window.material) {
      window.material = null;
      globe.material = null;
      Viewer.scene.globe.enableLighting = false;
      return;
    }
    if (window.DoubleScreen) {
      window.DoubleScreen && window.DoubleScreen.destroy();
      window.DoubleScreen = null;
      const compass = document.getElementsByClassName("bottom_btn")[0];
      compass.style.right = "70px";
    }
    sgworld.Analysis.clearMeasure();
    sgworld.Creator.SimpleGraphic.clear();
    // this.toolMenu.removeFromMap();
    sgworld.Creator.SimpleGraphic.clear();
    // this.toolFlag = null;
    // this.toolMenu = null;
    // }
    earthCtrl.analysis.deleteObject();
  },
  clearLeftTools(res) { },
  //空间查询属性定位
  spaceLocation(res) {
    var name = '空间查询';
    debugger
    this.setClearLocation(name);
    switch (res.type) {
      case 'MultiPolygon':
        for (var i in res.coordinates) {
          this.setLocationPolygon(res.coordinates[i], name)
        }
        break;
      case 'MultiLineString':
        for (var i in res.coordinates) {
          this.setLocationLine(res.coordinates[i], name)
        }
        break;
      case 'Point':
        this.setLocationPoint(res.coordinates, name)
        break;
    }
  },
  //清空之前绘制图形
  setClearLocation(res) {
    for (var i = 0; i < window.Viewer.entities._entities._array.length; i++) {
      if (window.Viewer.entities._entities._array[i].name == res) {
        window.Viewer.entities.remove(window.Viewer.entities._entities._array[i])
        i = i - 1;
      }
    }
  },
  setLocationPolygon(res, name) {
    var position = [];
    var geom = res[0]
    for (var i in geom) {
      position.push(geom[i][0], geom[i][1])
    }
    var locationPolygonEntity = window.Viewer.entities.add({
      name: name,
      polygon: {
        hierarchy: { positions: Cesium.Cartesian3.fromDegreesArray(position) },
        outline: true,
        outlineWidth: 100,
        arcType: Cesium.ArcType.RHUMB,
        material: Cesium.Color.CORNFLOWERBLUE.withAlpha(0.4),
        outline: true, // height is required for outline to display
        outlineColor: Cesium.Color.DODGERBLUE,
      }
    });
    this.setViewerFlyTo(locationPolygonEntity)
  },
  setLocationPoint(res, name) {
    const position = window.Viewer.entities.add({
      name: name,
      position: Cesium.Cartesian3.fromDegrees(res[0], res[1]),
      billboard: {
        image: config.StaticFileBaseUrl + '/Workers/image/location.png',
        // 设置贴地
        heightReference: Cesium.Color.CORNFLOWERBLUE.withAlpha(0.4),
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        scale: 5.0,
      },
    });
    this.setViewerFlyTo(position)
  },
  setViewerFlyTo(entity) {
    window.Viewer.flyTo(entity, {
      offset: {
        heading: Cesium.Math.toRadians(0.0),
        pitch: Cesium.Math.toRadians(-45),
        range: 40000
      }
    });
  },
  setLocationLine(res, name) {
    var position = [];
    for (var i in res) {
      position.push(res[i][0], res[i][1])
    }
    var polyline = window.Viewer.entities.add({
      name: name,
      polyline: {
        positions: Cesium.Cartesian3.fromDegreesArray(position),
        width: 10.0,
        material: new Cesium.PolylineOutlineMaterialProperty({
          color: Cesium.Color.CORNFLOWERBLUE.withAlpha(0.4),
          outlineWidth: 1,
          outlineColor: Cesium.Color.CORNFLOWERBLUE,
        }),
        clampToGround: true,
      },
    })
    this.setViewerFlyTo(polyline)
  }
};
export default menuTool;