首页 > 其他分享 >vue + cesium 洪水淹没分析完整示例_向着太阳往前冲的博客

vue + cesium 洪水淹没分析完整示例_向着太阳往前冲的博客

时间:2023-02-20 10:24:20浏览次数:63  
标签:vue 示例 viewer positions Cesium push cesium activeShapePoints Math

目录

一、洪水淹没分析效果

二、部分核心代码

1、绘制多边形范围

2、处理多边形区域的最大和最小高程

三、JS完整代码


一、洪水淹没分析效果

二、部分核心代码

1、绘制多边形范围

     /**
      * @author: 
      * @Date: 2022-04-11 16:44:02
      * @note: 注意事项
      * @description: 绘制范围
      */    
     drawExtent () {
      // 开启深度检测
      window.viewer.scene.globe.depthTestAgainstTerrain = true
      handler = new Cesium.ScreenSpaceEventHandler(window.viewer.canvas)

      handler.setInputAction((event) => {
        const earthPosition = viewer.scene.pickPosition(event.position);
        if (Cesium.defined(earthPosition)) {
          if (activeShapePoints.length === 0) {
            floatingPoint = this.createPoint(earthPosition);
            activeShapePoints.push(earthPosition);
            const dynamicPositions = new Cesium.CallbackProperty(function () {
              return new Cesium.PolygonHierarchy(activeShapePoints);
            }, false);
            activeShape = this.drawShape(dynamicPositions, Cesium.Color.fromBytes(64, 157, 253, 50));
          }
          activeShapePoints.push(earthPosition);
          this.tempEntities.push(this.createPoint(earthPosition))
        }
      }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

      handler.setInputAction((event) => {
        if (Cesium.defined(floatingPoint)) {
          const newPosition = viewer.scene.pickPosition(event.endPosition);
          if (Cesium.defined(newPosition)) {
            floatingPoint.position.setValue(newPosition);
            activeShapePoints.pop();
            activeShapePoints.push(newPosition);
          }
        }
      }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

      handler.setInputAction((event) => {
        activeShapePoints.pop()
        if(activeShapePoints.length < 3) return
        
        this.tempEntities.push(this.drawPolyline(activeShapePoints))
        let ploy = this.drawShape(activeShapePoints, Cesium.Color.fromBytes(64, 157, 253, 20))
        this.tempEntities.push(ploy)
        this.getAreaHeight(activeShapePoints)
        
        window.viewer.entities.remove(floatingPoint);
        window.viewer.entities.remove(activeShape);
        floatingPoint = undefined;
        activeShape = undefined;
        handler.destroy()// 关闭事件句柄
        handler = null
      }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
    },

2、处理多边形区域的最大和最小高程

    /**
     * @author: 
     * @Date: 2022-04-11 16:48:43
     * @note: 注意事项
     * @description: 获取区域内最大和最小高程
     * @param {*} positions
     */    
    getAreaHeight (positions) {
      let startP = positions[0]
      let endP = positions[positions.length -1]
      if(startP.x != endP.x && startP.y != endP.y && startP.z != endP.z) positions.push(positions[0])

      const tempPoints = []
      for (let i = 0; i < positions.length; i++) {
        var ellipsoid = window.viewer.scene.globe.ellipsoid
        var cartographic = ellipsoid.cartesianToCartographic(positions[i])
        var lat = Cesium.Math.toDegrees(cartographic.latitude)
        var lng = Cesium.Math.toDegrees(cartographic.longitude)
        tempPoints.push([lng, lat])
      }
      var line = turf.lineString(tempPoints)
      var chunk = turf.lineChunk(line, 10, {units: 'meters'});

      const tempArray = []
      chunk.features.forEach(f => {
        f.geometry.coordinates.forEach(c => {
          tempArray.push(Cesium.Cartographic.fromDegrees(c[0], c[1]))
        })
      })

      var promise = Cesium.sampleTerrainMostDetailed(window.viewer.terrainProvider, tempArray)
      Cesium.when(promise, (updatedPositions) => {
        const max = Math.max.apply(Math, updatedPositions.map(item => { return item.height }))
        const min = Math.min.apply(Math, updatedPositions.map(item => { return item.height }))
        this.waterHeight = Math.ceil(min)
        this.minWaterHeight = Math.ceil(min)
        this.maxWaterHeight = Math.ceil(max)
        // 禁用绘制按钮
        this.isDraw = !this.isDraw
      })
    },

三、JS完整代码

<script>
import * as turf from '@turf/turf'

let activeShapePoints = []
let floatingPoint = undefined
let activeShape = undefined
let handler = undefined

export default {
  data() {
    return {
      isDraw: false,
      maxWaterHeight: 2000,
      minWaterHeight: 0,
      warningWaterHeight: 0, // 预警高度
      waterHeight: 0,
      waterHeightShow: false,
      speed: '1',
      waterHeightTimeer: 0,
      waterPrimitive: undefined,
      tempEntities: []
    }
  },
  beforeDestroy() {
    // 关闭当前组件之前清除所有数据
    this.clearAllEntities()
  },
  methods: {
     /**
      * @author: 
      * @Date: 2022-04-11 16:44:02
      * @note: 注意事项
      * @description: 绘制范围
      */    
     drawExtent () {
      // 开启深度检测
      window.viewer.scene.globe.depthTestAgainstTerrain = true
      handler = new Cesium.ScreenSpaceEventHandler(window.viewer.canvas)

      handler.setInputAction((event) => {
        const earthPosition = viewer.scene.pickPosition(event.position);
        if (Cesium.defined(earthPosition)) {
          if (activeShapePoints.length === 0) {
            floatingPoint = this.createPoint(earthPosition);
            activeShapePoints.push(earthPosition);
            const dynamicPositions = new Cesium.CallbackProperty(function () {
              return new Cesium.PolygonHierarchy(activeShapePoints);
            }, false);
            activeShape = this.drawShape(dynamicPositions, Cesium.Color.fromBytes(64, 157, 253, 50));
          }
          activeShapePoints.push(earthPosition);
          this.tempEntities.push(this.createPoint(earthPosition))
        }
      }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

      handler.setInputAction((event) => {
        if (Cesium.defined(floatingPoint)) {
          const newPosition = viewer.scene.pickPosition(event.endPosition);
          if (Cesium.defined(newPosition)) {
            floatingPoint.position.setValue(newPosition);
            activeShapePoints.pop();
            activeShapePoints.push(newPosition);
          }
        }
      }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

      handler.setInputAction((event) => {
        activeShapePoints.pop()
        if(activeShapePoints.length < 3) return
        
        this.tempEntities.push(this.drawPolyline(activeShapePoints))
        let ploy = this.drawShape(activeShapePoints, Cesium.Color.fromBytes(64, 157, 253, 20))
        this.tempEntities.push(ploy)
        this.getAreaHeight(activeShapePoints)
        
        window.viewer.entities.remove(floatingPoint);
        window.viewer.entities.remove(activeShape);
        floatingPoint = undefined;
        activeShape = undefined;
        handler.destroy()// 关闭事件句柄
        handler = null
      }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
    },
    
    /**
     * @author: 
     * @Date: 2022-04-11 16:48:43
     * @note: 注意事项
     * @description: 获取区域内最大最小高程
     * @param {*} positions
     */    
    getAreaHeight (positions) {
      let startP = positions[0]
      let endP = positions[positions.length -1]
      if(startP.x != endP.x && startP.y != endP.y && startP.z != endP.z) positions.push(positions[0])

      const tempPoints = []
      for (let i = 0; i < positions.length; i++) {
        var ellipsoid = window.viewer.scene.globe.ellipsoid
        var cartographic = ellipsoid.cartesianToCartographic(positions[i])
        var lat = Cesium.Math.toDegrees(cartographic.latitude)
        var lng = Cesium.Math.toDegrees(cartographic.longitude)
        tempPoints.push([lng, lat])
      }
      var line = turf.lineString(tempPoints)
      var chunk = turf.lineChunk(line, 10, {units: 'meters'});

      const tempArray = []
      chunk.features.forEach(f => {
        f.geometry.coordinates.forEach(c => {
          tempArray.push(Cesium.Cartographic.fromDegrees(c[0], c[1]))
        })
      })

      var promise = Cesium.sampleTerrainMostDetailed(window.viewer.terrainProvider, tempArray)
      Cesium.when(promise, (updatedPositions) => {
        const max = Math.max.apply(Math, updatedPositions.map(item => { return item.height }))
        const min = Math.min.apply(Math, updatedPositions.map(item => { return item.height }))
        this.waterHeight = Math.ceil(min)
        this.minWaterHeight = Math.ceil(min)
        this.maxWaterHeight = Math.ceil(max)
        // 禁用绘制按钮
        this.isDraw = !this.isDraw
      })
    },

    /**
     * @author: 
     * @Date: 2022-04-11 16:46:47
     * @note: 注意事项
     * @description: 创建点
     * @param {*} worldPosition
     */
    createPoint(worldPosition) {
      const point = window.viewer.entities.add({
        position: worldPosition,
        point: {
          color: Cesium.Color.SKYBLUE,
          pixelSize: 5,
          heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        },
      });
      return point;
    },

    /**
     * @author: 
     * @Date: 2022-04-11 16:46:37
     * @note: 注意事项
     * @description: 绘制多边形
     * @param {*} positionData
     * @param {*} mat
     */
    drawShape(positionData, mat) {
      let shape = window.viewer.entities.add({
        polygon: {
          hierarchy: positionData,
          material: mat,
          outline: true,
          outlineColor: Cesium.Color.SKYBLUE,
          outlineWidth: 4,
        }
      });
      return shape;
    },

    /**
     * @author: 
     * @Date: 2022-04-11 16:46:11
     * @note: 注意事项
     * @description: 绘制线
     * @param {*} positions
     */
    drawPolyline (positions) {
      if (positions.length < 1) return

      let startP = positions[0]
      let endP = positions[positions.length -1]
      if(startP.x != endP.x && startP.y != endP.y && startP.z != endP.z) positions.push(positions[0])

      return window.viewer.entities.add({
        name: 'polyline',
        polyline: {
          positions: positions,
          width: 2.0,
          material: Cesium.Color.SKYBLUE,
          clampToGround: true
        }
      })
    },

    /**
     * @author: 
     * @Date: 2022-04-11 16:45:05
     * @note: 注意事项
     * @description: 淹没分析
     */
    induationAnalysis () {
      if(Number(this.warningWaterHeight) < Number(this.minWaterHeight) || Number(this.warningWaterHeight) > Number(this.maxWaterHeight)){
        this.$message({
          message: '预警高度必须在最小高度和最小高度之间',
          type: 'warning'
        });
        return
      }
      
      let shape = window.viewer.entities.add({
        polygon: {
          hierarchy: activeShapePoints,
          material: Cesium.Color.fromBytes(64, 157, 253, 20),
          extrudedHeight: Number(this.warningWaterHeight),
          outline: true,
          outlineColor: Cesium.Color.RED,
          outlineWidth: 4,
          // perPositionHeight: true
        }
      });
      this.tempEntities.push(shape)

      this.waterHeightShow = true
      this.waterHeight = Number(this.minWaterHeight)
      const en = window.viewer.entities.add({
        polygon: {
          hierarchy: activeShapePoints,
          extrudedHeight: new Cesium.CallbackProperty(() =>{
            return this.waterHeight
          }, false),
          material: Cesium.Color.fromBytes(64, 157, 253, 150),
        }
      })
      this.tempEntities.push(en)

      this.waterHeightTimeer = setInterval(() => {
        this.waterHeight += Number(this.speed)

        let l = this.speed.split('.').length > 1 ? this.speed.split('.')[1].length : 0
        this.waterHeight = Number(this.waterHeight.toFixed(l))
        this.maxWaterHeight = Number(this.maxWaterHeight)
        this.minWaterHeight = Number(this.minWaterHeight)
        if (this.waterHeight > this.maxWaterHeight || this.waterHeight < this.minWaterHeight) {
          clearInterval(this.waterHeightTimeer)
          this.waterHeight = this.waterHeight > this.maxWaterHeight ? this.maxWaterHeight : this.minWaterHeight
        }

      }, 1000)
    },

    /**
     * @author: 
     * @Date: 2022-04-11 16:44:42
     * @note: 注意事项
     * @description: 清除当前页面所有数据
     */
    clearAllEntities () {
      if (this.waterHeightTimeer) {
        clearInterval(this.waterHeightTimeer)
      }
      this.positions = []
      const length = this.tempEntities.length
      for (let f = 0; f < length; f++) {
        window.viewer.entities.remove(this.tempEntities[f])
      }
      this.tempEntities = []
      this.waterHeightShow = false
      activeShapePoints = [];
      this.warningWaterHeight = 0
      this.isDraw = !this.isDraw
      floatingPoint = undefined
      activeShape = undefined
      if(handler){
        handler.destroy()// 关闭事件句柄
        handler = undefined
      }
    }
  }
}
</script>

完整项目示例下载

本文转自 https://blog.csdn.net/u013869554/article/details/124272138,如有侵权,请联系删除。

标签:vue,示例,viewer,positions,Cesium,push,cesium,activeShapePoints,Math
From: https://www.cnblogs.com/hustshu/p/17136428.html

相关文章

  • vue——使用$router.push跳转无效
    我的情况:登录组件A,点击登录后,跳转首页无效,在全局路由守卫router.beforeEach中打印,也没有跳转信息。原因:登录组件A中,有个beforeRouteLeave生命周期函数,没有写next()回调......
  • Vue的六种基本通信方式
    1props父组件Father子组件Son//父组件给子组件传递参数<Sonname="何世蟒"sex="男":age="22"/>//子组件接收参数//方式一props:['name','sex','age']//方式二限......
  • Vue系列---【自定义vue组件发布npm仓库】
    自定义vue组件发布npm仓库参考链接:自定义vue组件发布npm仓库......
  • 2023-02-19,新的30道Vue面试题!
    分享当下较新的30道Vue面试题!(qq.com)Thedifferencebetweenvueandangular?VueandAngulararebothpopularJavaScriptframeworksusedforbuildingwebappli......
  • vue3+vite | assets动态引入图片的几种方式,解决打包后图片路径错误不显示的问题
    问题vite官方默认的配置,如果资源文件在assets文件夹打包后会把图片名加上hash值,但是直接通过:src="imgSrc"方式引入并不会在打包的时候解析,导致开发环境可以正常引入,打包......
  • Seata分布式事务框架示例
    一、背景阿里给出了Seata的官方示例,地址:https://github.com/seata/seata-samples,提供了很多示例:springcloud-seata-sharding-jdbc-mybatis-plus-samplesEasytound......
  • Cesium entity画各种图(十六)
    1.polygon(面)varsquare=this.viewer.entities.add({id:8,position:newCesium.Cartesian3.fromDegrees(103.8621,30.7065,495),pol......
  • vue---day05( )
    上节课回顾#0checkboxv-model只针对于input,做双向数据绑定 -单选:选中或不选中选中就是true,不选中就是false -多选:数组,选了多个,把选中的value值放到数组中......
  • Vue数据响应式底层原理
    Vue数据响应式底层原理数据响应式定义:(当数据变化的时候,会自动运行一些相关函数)原理就是通过Object.defineProperty()对属性进行劫持,当访问该属性时我们就收集依赖,当数......
  • Vue前后端交互、生命周期、组件化开发
    目录Vue前后端交互、生命周期、组件化开发一、Vue用axios与后端交互二、Vue的生命周期三、组件化开发Vue前后端交互、生命周期、组件化开发一、Vue用axios与后端交互​ ......