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>
相关推荐
ZJPRENO3 小时前
WorkBuddy 从零到一|混元4 Flash 实战手记-提醒小工具开发
前端
用户302822530683 小时前
AI 视频图片运镜为什么会抖?帧率拉高也救不了多次取整
前端·人工智能·typescript
前端码农人生3 小时前
前端转 Agent:真正要重写的不是接口层,是交互层
前端
AI2中文网3 小时前
App Inventor 做的表单怎么导出 PDF?WebViewer + JavaScript 在本地生成并保存 PDF 的完整示例
android·开发语言·javascript·pdf·鸿蒙·app inventor
孟陬3 小时前
Foobar 技术周刊 #003:黄仁勋的三段吃瓜和回归,抛开事实不谈(嘿嘿)让全人类 AGI 进程得以继续
前端·后端·程序员
涛涛ing3 小时前
别再用 AI 写计算逻辑了:你的页面卡死,连鼠标都点不动
前端
蓝蓝的代码人4 小时前
用 Python、Qt 和 Vue 做一个 Windows 股票看盘工具,观潮的行情小窗与桌面提醒
vue.js·python·qt·开源·桌面应用
OpenTiny社区4 小时前
当 AI 开始写 UI,我们缺的不是模型,是"设计词汇"
前端·github·ai编程
Amos_Web5 小时前
Rspack 源码解析(三十八):SubresourceIntegrityPlugin 如何跨四个插件协作生成 integrity
前端
Amos_Web5 小时前
通过 Rspack 源码学 Rust(八):struct 保存数据,impl 定义怎样使用数据
前端