Vue.js2+Cesium1.103.0 九、淹没分析效果

Vue.js2+Cesium1.103.0 九、淹没分析效果

Demo

vue 复制代码
<template>
  <div
    id="cesium-container"
    style="width: 100%; height: 100%;"
  >
    <span
      id="button"
      style="position: absolute; right: 50px; top: 50px; z-index: 999; font-size: 24px; color: #fff; cursor: pointer;"
    >淹没分析</span>
  </div>
</template>

<script>
/* eslint-disable no-undef */
/* eslint-disable new-cap */
import { lineString, bbox, bboxPolygon, area, pointGrid } from '@turf/turf'
export default {
  data() {
    return {}
  },
  computed: {},
  watch: {},
  mounted() {
    window.$InitMap()
    const tileset = new Cesium.Cesium3DTileset({
      url: 'https://lab.earthsdk.com/model/3610c2b0d08411eab7a4adf1d6568ff7/tileset.json',
      debugShowMemoryUsage: false
    })
    viewer.scene.primitives.add(tileset)
    viewer.zoomTo(tileset)

    const entity = viewer.entities.add({
      polygon: {
        hierarchy: new Cesium.PolygonHierarchy(
          Cesium.Cartesian3.fromDegreesArrayHeights([
            121.4631015, 31.2671618, 0, 121.5367499, 31.2658501, 0, 121.5328263,
            31.2059886, 0, 121.4590159, 31.2055623, 0
          ])
        ),
        // extrudedHeight: 200,
        // perPositionHeight: true,
        // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        material: new Cesium.ColorMaterialProperty(
          new Cesium.Color.fromCssColorString('#17E980').withAlpha(0)
        )
      }
    })

    // 淹没分析
    function FloodAnalysis() {
      const positions = entity.polygon.hierarchy.getValue(
        Cesium.JulianDate.now()
      ).positions
      const tempPoints = []
      for (let i = 0; i < positions.length; i++) {
        const cartographic =
          viewer.scene.globe.ellipsoid.cartesianToCartographic(positions[i])
        const lat = Cesium.Math.toDegrees(cartographic.latitude)
        const lng = Cesium.Math.toDegrees(cartographic.longitude)
        tempPoints.push([lng, lat])
      }
      // 生成外接矩形
      const line = lineString(tempPoints)
      const _bbox = bbox(line)
      const _bboxPolygon = bboxPolygon(_bbox)
      const _area = area(_bboxPolygon)
      // 生成格网
      // 计算网格点之间的距离,尽量保证范围内有1万个左右格网点。
      const cellSide = Math.sqrt(_area / 1000000) / 100
      const options = { units: 'kilometers' }
      const grid = pointGrid(_bbox, cellSide, options)
      const gridPositions = []
      grid.features.forEach(f => {
        gridPositions.push(
          Cesium.Cartographic.fromDegrees(
            f.geometry.coordinates[0],
            f.geometry.coordinates[1]
          )
        )
      })
      const promise = Cesium.sampleTerrainMostDetailed(
        viewer.terrainProvider,
        gridPositions
      )
      let maxHeight = 100
      let minHeight = 0
      Promise.resolve(promise).then(function (updatedPositions) {
        for (let i = 0; i < updatedPositions.length; i++) {
          const height = updatedPositions[i].height
          // 获取格网点处地形高度
          minHeight = height < minHeight ? height : minHeight
          maxHeight = height > maxHeight ? height : maxHeight
        }
        const value = {
          minHeight,
          maxHeight
        }
        let waterHeight = value.minHeight
        const targertWaterHeight = value.maxHeight
        entity.polygon.perPositionHeight = true
        entity.polygon.material = new Cesium.Color.fromBytes(64, 157, 253, 150)
        entity.polygon.extrudedHeight = new Cesium.CallbackProperty(
          function () {
            waterHeight += 0.5
            if (waterHeight > targertWaterHeight) {
              waterHeight = targertWaterHeight
            }
            return waterHeight
          },
          false
        )
        return value
      })
    }

    document.getElementById('button').onclick = function () {
      FloodAnalysis()
    }
  },
  methods: {}
}
</script>
相关推荐
码界领航2 分钟前
【2025最新版】Chrome谷歌浏览器如何能恢复到之前的旧版本
前端·chrome
customer0831 分钟前
【开源免费】基于SpringBoot+Vue.JS个人博客系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
乐多_L1 小时前
使用vue3框架vue-next-admin导出表格excel(带图片)
前端·javascript·vue.js
初尘屿风1 小时前
基于微信小程序的电影院订票选座系统的设计与实现,SSM+Vue+毕业论文+开题报告+任务书+指导搭建视频
vue.js·微信小程序·小程序
南望无一1 小时前
React Native 0.70.x如何从本地安卓源码(ReactAndroid)构建
前端·react native
Mike_188702783511 小时前
1688代采下单API接口使用指南:实现商品采集与自动化下单
前端·python·自动化
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS打卡健康评测系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
鲨鱼辣椒️面1 小时前
HTML视口动画
前端·html
一小路一1 小时前
Go Web 开发基础:从入门到实战
服务器·前端·后端·面试·golang
堇舟1 小时前
HTML第一节
前端·html