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 小时前
价值1000的 AI 工作流:Codex 通用前端协作模式
前端·aigc·ai编程
sunny_2 小时前
面试踩大坑!同一段 Node.js 代码,CJS 和 ESM 的执行顺序居然是反的?!99% 的人都答错了
前端·面试·node.js
拉不动的猪2 小时前
移动端调试工具VConsole初始化时的加载阻塞问题
前端·javascript·微信小程序
ayqy贾杰4 小时前
Agent First Engineering
前端·vue.js·面试
IT_陈寒4 小时前
SpringBoot实战:5个让你的API性能翻倍的隐藏技巧
前端·人工智能·后端
iceiceiceice5 小时前
iOS PDF阅读器段评实现:如何从 PDFSelection 精准还原一个自然段
前端·人工智能·ios
大金乄5 小时前
封装一个vue2的elementUI 表格组件(包含表格编辑以及多级表头)
前端·javascript
葡萄城技术团队5 小时前
【性能优化篇】面对万行数据也不卡顿?揭秘协同服务器的“片段机制 (Fragments)”
前端
程序员阿峰6 小时前
2026前端必备:TensorFlow.js,浏览器里的AI引擎,不写Python也能玩转智能
前端
Jans6 小时前
Shipfe — Rust 写的前端静态部署工具:一条命令上线 + 零停机 + 可回滚 + 自动清理
前端