fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
 
// 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并)
export const mapOptions = {
  scene: {
    center: { lat: 20.648765, lng: 129.340334, alt: 19999976, heading: 355, pitch: -89 }
  },
  control: {
    sceneModePicker: false
  }
}
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
 
  map.basemap = 2017 // 蓝色底图
  addLayer()
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
let windLayer
function addLayer() {
  windLayer = new mars3d.layer.WindLayer({
    particlesNumber: 9000,
    fadeOpacity: 0.996,
    dropRate: 0.003,
    dropRateBump: 0.01,
    speedFactor: 0.2,
    lineWidth: 4.0,
    // 色带配置
    colors: [
      "rgb(4,14,216)",
      "rgb(32,80,255)",
      "rgb(65,150,255)",
      "rgb(109,193,255)",
      "rgb(134,217,255)",
      "rgb(156,238,255)",
      "rgb(175,245,255)",
      "rgb(206,255,255)",
      "rgb(255,254,71)",
      "rgb(255,235,0)",
      "rgb(255,196,0)",
      "rgb(255,144,0)",
      "rgb(255,72,0)",
      "rgb(255,0,0)",
      "rgb(213,0,0)",
      "rgb(158,0,0)"
    ]
  })
  map.addLayer(windLayer)
 
  loadNetCDF("//data.mars3d.cn/file/apidemo/wind.nc").then((data) => {
    windLayer.setData(data)
  })
}
 
let canrefresh
 
// 参数调整面板
export function onParticleSystemOptionsChange(options) {
  clearTimeout(canrefresh)
  canrefresh = setTimeout(() => {
    windLayer.setOptions(options)
  }, 500)
}
 
// 加载并解析NC数据
function loadNetCDF(filePath) {
  return new Promise(function (resolve) {
    const request = new XMLHttpRequest()
    request.open("GET", filePath)
    request.responseType = "arraybuffer"
 
    request.onload = function () {
      const arrayToMap = function (array) {
        return array.reduce(function (map, object) {
          map[object.name] = object
          return map
        }, {})
      }
 
      // eslint-disable-next-line new-cap
      const NetCDF = new netcdfjs(request.response)
      const variables = arrayToMap(NetCDF.variables)
      const uAttributes = arrayToMap(variables.U.attributes)
      const vAttributes = arrayToMap(variables.V.attributes)
 
      const arrLon = NetCDF.getDataVariable("lon").flat()
      const arrLat = NetCDF.getDataVariable("lat").flat()
      const arrU = NetCDF.getDataVariable("U").flat()
      const maxU = uAttributes.max.value
      const minU = uAttributes.min.value
      const arrV = NetCDF.getDataVariable("V").flat()
      const maxV = vAttributes.max.value
      const minV = vAttributes.min.value
 
      // 构造WindLayer类需要的格式数据
      const result = {
        xmin: Math.min(...arrLon),
        xmax: Math.max(...arrLon),
        ymin: Math.min(...arrLat),
        ymax: Math.max(...arrLat),
        rows: arrLat.length,
        cols: arrLon.length,
        udata: arrU, // 横向风速
        vdata: arrV, // 纵向风速
        umin: minU,
        umax: maxU,
        vmin: minV,
        vmax: maxV
      }
      resolve(result)
    }
    request.send()
  })
}
 
// 改变颜色
export function changeColor(color) {
  windLayer.colors = [color]
}