飞道的博客

Cesium实战记录(八)三维风场+风速热力图(水平+垂直)

2623人阅读  评论(0)

目录

老规矩首先看下效果

一、风场

1、数据

2、原理剖析

首先,第一步就是构造网格数据

 然后,撒粒子

再然后,起风吧

二、热力场


老规矩首先看下效果

风场v1.0(平面版,只有U V 方向风速)

三维风场(不带高度)

(如果观看不了就点击链接:三维风场(不带高度)_哔哩哔哩_bilibili

风场v2.0(高度版,加入W垂直风速)

三维风场(带垂直风速)

(如果观看不了就点击链接:三维风场(带垂直风速)_哔哩哔哩_bilibili

风场v1.0结合风速热力图效果(风速只有计算U V 方向)

三维风场叠加风速热力图

(如果观看不了就点击链接:三维风场叠加风速热力图_哔哩哔哩_bilibili



看完效果,那就开始风场吧

一、风场

1、数据

首先是风场的数据格式

气象数据之风向数据json格式解析 - 莫小龙 - 博客园

气象数据之风向数据展示原理 - 莫小龙 - 博客园

这篇博客详细介绍了风场的数据格式,但这个是解析过后的,原始数据一般是netcdf数据或grib2数据,然后解析成类似上面展示的json格式。

其实那个我也没用他的,我只用了东西方向 V 的风速数据 、南北方向 U 的风速数据,垂直方向W 的风速数据 (一般你们不要求高度也不用这个数据,现在网上还没看到用垂直风速的),网格数(宽度+长度),网格的四至范围 ,这些就够了。

其中U 和 V 以及W 的数据是数组格式的,单位是m/s,数据长度是 网格数宽*高。

什么意思呢,你就想象成一个矩形的面,然后划分成多少行多少列(宽的意思就是多少列cols,高的意思就是多少行rows),也就是网上大家普遍说的构造棋盘格。每个格子都有初始的风速,东西方向和南北方向的。

看下我的数据吧

 

 比如说上面风速有2256条数据,就是cols * rows 的数,代表每个47*48个网格里面的风速,分东西和南北的。

max和min就是这个方向上 数组里最大最小值,不是必须的,我是因为业务需要




2、原理剖析

根据网上大部队来吧,

首先,第一步就是构造网格数据

造好了上述的json数据后,还需要做一步事就是计算水平面上南北和东西方向结合的矢量数据,算向量模长 u*u + v*v 开根号,就是朝这个方向的风速,用来计算下一步风的经纬度的。这个没啥说的吧就是算向量。

看下我的棋盘格数据吧

是47*48条,然后每条数据是【u,v,w, Math.sqrt(u * u + v * v)】,没有高度的就没有w

 然后,撒粒子

有了这么多格子,那我们就往这些个格子里面撒点吧,随机撒,以为我们也不知道风的起点在哪里,撒上就算作风的起点了,只需要计算下一步风的走向就行。

按随机撒5000个点吧,让这5000个点落在棋盘格内,(随机数落在四至内,不说了)

 其中 age 表述粒子的生命周期,

lng,lat,height表示当前位置的经纬度高度,

tlng,tlat,theight表示下一步的位置经纬度高度,

speed表示水平方向的风速

如果你们用不上高度那就不要。

为什么不能用speed直接成风速呢?因为这个speed是水平方向的风速,他的风速不是m/s而是每一步所走的经纬度,如果你看其他人的源码就知道了,大家都是这么去做的,为什么不看我的源码呢?因为我不准备放我的源码!!!

所以高程点只能通过水平距离去算移动的时间,然后再 乘 垂直方向的风速 m/s (这是我所理解的算法,或许有大佬可以直接算三维向量的模长,算三维方向的风速那也是可以的,但是我就不麻烦了,我分开,水平是水平的,垂直另算)

再然后,起风吧

用canvas去做比cesium 的 primitive 效率会高上很多,我也是用canvas去做的。这里用到的是循环去 实现 canvas的画线以及浏览器的刷新机制 requestAnimationFrame 。

好了,到此就全部完成了。具体的源码我就不放了,因为我也是站在别人的肩膀去完善的,网上也能找到源码,csdn上就有, 大家的源码都是这样的,我的被我改的乱七八糟的,给了也只会误导你们,如果你看了网上还不太明白的,那你联系我,有空我给你解答哈




最后在插一嘴实现高度的方法,因为网上我没找到有实现高度的,用的都是canvas 2d 的方式,我最后实现了,但其实也是2d方法的,不过我觉得结合three.js去实现 应该可以完美复现三维风场垂直风速的那种效果。

现在说下高度的获取方法吧:

如果用上高度,那我多一嘴,这是网上其他都没有说到的,也是我自己胡JB想的,不知道对不对。

高度计算方法:首先撒完点后,拿到经纬度和下一步的经纬度后,算当前点的贴地高程,算当前点和下一个点的空间距离,计算出距离,比上速度,算出移动的时间,在根据时间 * 当前网格的垂直风速,就是这个点移动到下一个位置所移动的高度,最后再加上当前点的贴地高程即是下个点的高程数据。




二、热力场

热力场怎么做呢?在上面我们已经构建好网格数据了,那热力数据就展示每个网格的风速数据,这里我就只展示了uv方向的数据。

算好uv方向的数据,然后撒5000个点,计算每个点落在的网格数据,然后利用二分插值算法找到这个网格内的风速,把这个风速就赋给这个点作为这个点的value。这样一个热力图的数据就做好了。

接下来就是构建热力图层然后叠加了,这个网上都很完善了,我就不写了哈。

最后看下我的热力点位数据吧





如果你看到了这里,那么恭喜你,我最终还是决定把源码放上供大家参考下,但是我事先说明啊,我写的和网上大差不差,但是被我乱改一通。也不设置积分下载了,纯纯福利好不啦~ 

不带高度的


  
  1. /**
  2. * @Description:风场
  3. * @author MrKuang
  4. * @date 2023/1/13 0013
  5. */
  6. export default class CanvasWindy {
  7. constructor( json, params) {
  8. this. _windData = json;
  9. this. _viewer = params. viewer;
  10. this. _canvas = params. canvas;
  11. //可配置的参数
  12. this. _canvasContext = params. canvas. getContext( '2d') // canvas上下文
  13. this. _canvasWidth = params. canvasWidth; //画布宽度
  14. this. _canvasHeight = params. canvasHeight; //画布高度
  15. this. _speedRate = params. speedRate || 50;
  16. this. _particlesNumber = params. particlesNumber || 5000; //粒子数
  17. this. _maxAge = params. maxAge || 120; //粒子生命周期
  18. this. _frameTime = 1000/(params. frameRate || 10) ; // 每秒刷新次数
  19. this. _color = params. color || '#ffffff';
  20. this. _lineWidth = params. lineWidth || 1 // 线宽度
  21. // 内置参数
  22. this. _grid = [];
  23. this. _initExtent = [] // 风场初始范围
  24. this. _calc_speedRate = [ 0, 0] // 根据speedRate参数计算经纬度步进长度
  25. this. _windField = null
  26. this. _particles = []
  27. this. _animateFrame = null // requestAnimationFrame事件句柄,用来清除操作
  28. this. isdistory = false // 是否销毁,进行删除操作
  29. this. _init();
  30. }
  31. _init( ) {
  32. //创建棋盘格子
  33. this. _createWindField();
  34. this. _calcStep();
  35. // 创建风场粒子
  36. for ( var i = 0; i < this. _particlesNumber; i++) {
  37. this. _particles. push( this. _randomParticle( new CanvasParticle()));
  38. }
  39. this. _canvasContext. fillStyle = 'rgba(0, 0, 0, 0.97)'
  40. this. _canvasContext. globalAlpha = 0.6
  41. this. _animate();
  42. var then = Date. now();
  43. let that = this;
  44. ( function frame( ) {
  45. if (!that. isdistory) {
  46. that. _animateFrame = requestAnimationFrame(frame)
  47. var now = Date. now();
  48. var delta = now - then;
  49. if (delta> that. _frameTime) {
  50. then = now - delta % that. _frameTime;
  51. that. _animate()
  52. }
  53. } else {
  54. that. removeLines()
  55. }
  56. })()
  57. }
  58. _animate( ) {
  59. var nextLng = null
  60. var nextLat = null
  61. var uv = null
  62. this. _graphicData = [];
  63. this. _particles. forEach( (particle) => {
  64. if (particle. age <= 0) {
  65. this. _randomParticle(particle);
  66. }
  67. if (particle. age > 0) {
  68. var tlng = particle. tlng
  69. var tlat = particle. tlat
  70. let height = particle. theight;
  71. var gridpos = this. _togrid(tlng, tlat)
  72. var tx = gridpos[ 0]
  73. var ty = gridpos[ 1]
  74. if (! this. _isInExtent(tlng, tlat)) {
  75. particle. age = 0
  76. } else {
  77. uv = this. _getIn(tx, ty)
  78. nextLng = tlng + this. calc_speedRate[ 0] * uv[ 0]
  79. nextLat = tlat + this. calc_speedRate[ 1] * uv[ 1]
  80. particle. lng = tlng
  81. particle. lat = tlat
  82. particle. x = tx
  83. particle. y = ty
  84. particle. tlng = nextLng
  85. particle. tlat = nextLat
  86. particle. age--
  87. }
  88. }
  89. })
  90. if ( this. _particles. length <= 0) this. removeLines()
  91. this. _drawLines()
  92. }
  93. _drawLines( ) {
  94. var particles = this. _particles
  95. this. _canvasContext. lineWidth = this. _lineWidth
  96. // 后绘制的图形和前绘制的图形如果发生遮挡的话,只显示后绘制的图形跟前一个绘制的图形重合的前绘制的图形部分,示例:https://www.w3school.com.cn/tiy/t.asp?f=html5_canvas_globalcompop_all
  97. this. _canvasContext. globalCompositeOperation = 'destination-in'
  98. this. _canvasContext. fillRect( 0, 0, this. _canvasWidth, this. _canvasHeight)
  99. this. _canvasContext. globalCompositeOperation = 'lighter' // 重叠部分的颜色会被重新计算
  100. this. _canvasContext. globalAlpha = 0.9
  101. this. _canvasContext. beginPath()
  102. this. _canvasContext. strokeStyle = this. _color
  103. particles. forEach( (particle) => {
  104. var movetopos = this. _tomap(particle. lng, particle. lat, particle)
  105. var linetopos = this. _tomap(particle. tlng, particle. tlat, particle)
  106. if (movetopos != null && linetopos != null) {
  107. this. _canvasContext. moveTo(movetopos[ 0], movetopos[ 1])
  108. this. _canvasContext. lineTo(linetopos[ 0], linetopos[ 1])
  109. }
  110. })
  111. this. _canvasContext. stroke()
  112. }
  113. // 根据粒子当前所处的位置(棋盘网格位置),计算经纬度,在根据经纬度返回屏幕坐标
  114. _tomap( lng, lat, particle) {
  115. if (!lng || !lat) {
  116. return null
  117. }
  118. var ct3 = Cesium. Cartesian3. fromDegrees(lng, lat, 0)
  119. // 判断当前点是否在地球可见端
  120. var isVisible = new Cesium. EllipsoidalOccluder( Cesium. Ellipsoid. WGS84, this. _viewer. camera. position). isPointVisible(ct3)
  121. var pos = Cesium. SceneTransforms. wgs84ToWindowCoordinates( this. _viewer. scene, ct3)
  122. if (!isVisible) {
  123. particle. age = 0
  124. }
  125. return pos ? [pos. x, pos. y] : null
  126. }
  127. // 粒子是否在地图范围内
  128. _isInExtent( lng, lat) {
  129. if ((lng >= this. _windData. xmin && lng <= this. _windData. xmax) && (lat >= this. _windData. ymin && lat <= this. _windData. ymax)) return true
  130. return false
  131. }
  132. //创建棋盘格子
  133. _createWindField( ) {
  134. var k = 0
  135. var rows = null
  136. var uv = null
  137. for ( var j = 0; j < this. _windData. rows; j++) {
  138. rows = []
  139. for ( var i = 0; i < this. _windData. cols; i++, k++) {
  140. uv = this. _calcUV( this. _windData. udata[k], this. _windData. vdata[k])
  141. rows. push(uv)
  142. }
  143. this. _grid. push(rows)
  144. }
  145. console. log( this. _grid)
  146. }
  147. //计算风场向量
  148. _calcUV( u, v) {
  149. return [+u, +v, Math. sqrt(u * u + v * v)]
  150. }
  151. // 计算经纬度步进长度
  152. _calcStep( ) {
  153. var calcSpeed = this. _speedRate;
  154. this. calc_speedRate = [( this. _windData. xmax - this. _windData. xmin) / calcSpeed, ( this. _windData. ymax - this. _windData. ymin) / calcSpeed]
  155. }
  156. //根据风场范围随机生成粒子
  157. _randomParticle( particle) {
  158. let safe = 30;
  159. let x = - 1;
  160. let y = - 1;
  161. let lng = null;
  162. let lat = null;
  163. do {
  164. try {
  165. lng = this. _fRandomByfloat( this. _windData. xmin, this. _windData. xmax)
  166. lat = this. _fRandomByfloat( this. _windData. ymin, this. _windData. ymax)
  167. } catch (e) {
  168. // console.log(e)
  169. }
  170. if (lng) {
  171. //找到随机生成的粒子的经纬度在棋盘的位置 x y
  172. var gridpos = this. _togrid(lng, lat);
  173. x = gridpos[ 0];
  174. y = gridpos[ 1];
  175. }
  176. } while ( this. _getIn(x, y)[ 2] <= 0 && safe++ < 30)
  177. let uv = this. _getIn(x, y);
  178. var nextLng = lng + this. calc_speedRate[ 0] * uv[ 0]
  179. var nextLat = lat + this. calc_speedRate[ 1] * uv[ 1]
  180. particle. lng = lng
  181. particle. lat = lat
  182. particle. x = x
  183. particle. y = y
  184. particle. tlng = nextLng
  185. particle. tlat = nextLat
  186. particle. speed = uv[ 2]
  187. particle. age = Math. round( Math. random() * this. _maxAge) // 每一次生成都不一样
  188. return particle;
  189. }
  190. _getIn( x, y) {
  191. // 局部风场使用
  192. if (x < 0 || x >= this. _windData. cols || y >= this. _windData. rows || y <= 0) {
  193. return [ 0, 0, 0]
  194. }
  195. var x0 = Math. floor(x) //向下取整
  196. var y0 = Math. floor(y)
  197. var x1;
  198. var y1
  199. if (x0 === x && y0 === y) return this. _grid[y][x]
  200. x1 = x0 + 1
  201. y1 = y0 + 1
  202. var g00 = this. _getIn(x0, y0)
  203. var g10 = this. _getIn(x1, y0)
  204. var g01 = this. _getIn(x0, y1)
  205. var g11 = this. _getIn(x1, y1)
  206. var result = null
  207. // console.log(x - x0, y - y0, g00, g10, g01, g11)
  208. try {
  209. result = this. _bilinearInterpolation(x - x0, y - y0, g00, g10, g01, g11)
  210. // console.log(result)
  211. } catch (e) {
  212. console. log(x, y)
  213. }
  214. return result
  215. }
  216. // 二分差值算法计算给定节点的速度
  217. _bilinearInterpolation( x, y, g00, g10, g01, g11) {
  218. var rx = ( 1 - x)
  219. var ry = ( 1 - y)
  220. var a = rx * ry;
  221. var b = x * ry;
  222. var c = rx * y;
  223. var d = x * y
  224. var u = g00[ 0] * a + g10[ 0] * b + g01[ 0] * c + g11[ 0] * d
  225. var v = g00[ 1] * a + g10[ 1] * b + g01[ 1] * c + g11[ 1] * d
  226. return this. _calcUV(u, v)
  227. }
  228. // 随机数生成器(小数)
  229. _fRandomByfloat( under, over) {
  230. return under + Math. random() * (over - under)
  231. }
  232. // 根据经纬度,算出棋盘格位置
  233. _togrid( lng, lat) {
  234. var x = (lng - this. _windData. xmin) / ( this. _windData. xmax - this. _windData. xmin) * ( this. _windData. cols - 1)
  235. var y = ( this. _windData. ymax - lat) / ( this. _windData. ymax - this. _windData. ymin) * ( this. _windData. rows - 1)
  236. return [x, y]
  237. }
  238. _resize( width, height) {
  239. this. canvasWidth = width
  240. this. canvasHeight = height
  241. }
  242. removeLines( ) {
  243. window. cancelAnimationFrame( this. _animateFrame)
  244. this. isdistory = true
  245. this. _canvas. width = 1
  246. document. getElementById( 'box'). removeChild( this. _canvas)
  247. }
  248. }
  249. function CanvasParticle( ) {
  250. this. lng = null // 粒子初始经度
  251. this. lat = null // 粒子初始纬度
  252. this. x = null // 粒子初始x位置(相对于棋盘网格,比如x方向有360个格,x取值就是0-360,这个是初始化时随机生成的)
  253. this. y = null // 粒子初始y位置(同上)
  254. this. tlng = null // 粒子下一步将要移动的经度,这个需要计算得来
  255. this. tlat = null // 粒子下一步将要移动的y纬度,这个需要计算得来
  256. this. age = null // 粒子生命周期计时器,每次-1
  257. this. speed = null // 粒子移动速度,可以根据速度渲染不同颜色
  258. }

带高度的


  
  1. /**
  2. * @Description:风场
  3. * @author MrKuang
  4. * @date 2023/1/13 0013
  5. */
  6. export default class CanvasWindy {
  7. constructor( json, params) {
  8. this. _windData = json;
  9. this. _viewer = params. viewer;
  10. this. _canvas = params. canvas;
  11. //可配置的参数
  12. this. _canvasContext = params. canvas. getContext( '2d') // canvas上下文
  13. this. _canvasWidth = params. canvasWidth; //画布宽度
  14. this. _canvasHeight = params. canvasHeight; //画布高度
  15. this. _speedRate = params. speedRate || 50;
  16. this. _particlesNumber = params. particlesNumber || 5000; //粒子数
  17. this. _maxAge = params. maxAge || 120; //粒子生命周期
  18. this. _frameTime = 1000/(params. frameRate || 10) ; // 每秒刷新次数
  19. this. _color = params. color || '#ffffff';
  20. this. _lineWidth = params. lineWidth || 1 // 线宽度
  21. // 内置参数
  22. this. _grid = [];
  23. this. _initExtent = [] // 风场初始范围
  24. this. _calc_speedRate = [ 0, 0] // 根据speedRate参数计算经纬度步进长度
  25. this. _windField = null
  26. this. _particles = []
  27. this. _animateFrame = null // requestAnimationFrame事件句柄,用来清除操作
  28. this. isdistory = false // 是否销毁,进行删除操作
  29. this. _zspeed = 0;
  30. this. _init();
  31. }
  32. _init( ) {
  33. //创建棋盘格子
  34. this. _createWindField();
  35. this. _calcStep();
  36. // 创建风场粒子
  37. for ( var i = 0; i < this. _particlesNumber; i++) {
  38. this. _particles. push( this. _randomParticle( new CanvasParticle()));
  39. }
  40. this. _canvasContext. fillStyle = 'rgba(0, 0, 0, 0.97)'
  41. this. _canvasContext. globalAlpha = 0.6
  42. this. _animate();
  43. var then = Date. now();
  44. let that = this;
  45. ( function frame( ) {
  46. if (!that. isdistory) {
  47. that. _animateFrame = requestAnimationFrame(frame)
  48. var now = Date. now();
  49. var delta = now - then;
  50. if (delta> that. _frameTime) {
  51. then = now - delta % that. _frameTime;
  52. that. _animate()
  53. }
  54. } else {
  55. that. removeLines()
  56. }
  57. })()
  58. }
  59. _animate( ) {
  60. var nextLng = null
  61. var nextLat = null
  62. var uvw = null;
  63. this. _graphicData = [];
  64. this. _particles. forEach( (particle) => {
  65. if (particle. age <= 0) {
  66. this. _randomParticle(particle);
  67. }
  68. if (particle. age > 0) {
  69. var tlng = particle. tlng
  70. var tlat = particle. tlat
  71. let height = particle. theight;
  72. var gridpos = this. _togrid(tlng, tlat)
  73. var tx = gridpos[ 0]
  74. var ty = gridpos[ 1]
  75. if (! this. _isInExtent(tlng, tlat)) {
  76. particle. age = 0
  77. } else {
  78. uvw = this. _getIn(tx, ty)
  79. nextLng = tlng + this. _calc_speedRate[ 0] * uvw[ 0]
  80. nextLat = tlat + this. _calc_speedRate[ 1] * uvw[ 1]
  81. particle. lng = tlng
  82. particle. lat = tlat
  83. particle. x = tx
  84. particle. y = ty
  85. particle. tlng = nextLng
  86. particle. tlat = nextLat
  87. particle. height = height;
  88. //计算空间距离
  89. let d = mars3d. MeasureUtil. getDistance([ new mars3d. LngLatPoint(particle. lng, particle. lat, 0), new mars3d. LngLatPoint(particle. tlng, particle. tlat, 0)])
  90. let t = d / uvw[ 3];
  91. particle. theight = particle. height + t * uvw[ 2];
  92. particle. age--
  93. }
  94. }
  95. })
  96. if ( this. _particles. length <= 0) this. removeLines()
  97. this. _drawLines()
  98. }
  99. _drawLines( ) {
  100. var particles = this. _particles
  101. this. _canvasContext. lineWidth = this. _lineWidth
  102. // 后绘制的图形和前绘制的图形如果发生遮挡的话,只显示后绘制的图形跟前一个绘制的图形重合的前绘制的图形部分,示例:https://www.w3school.com.cn/tiy/t.asp?f=html5_canvas_globalcompop_all
  103. this. _canvasContext. globalCompositeOperation = 'destination-in'
  104. this. _canvasContext. fillRect( 0, 0, this. _canvasWidth, this. _canvasHeight)
  105. this. _canvasContext. globalCompositeOperation = 'lighter' // 重叠部分的颜色会被重新计算
  106. this. _canvasContext. globalAlpha = 0.9
  107. this. _canvasContext. beginPath()
  108. this. _canvasContext. strokeStyle = this. _color
  109. particles. forEach( (particle) => {
  110. var movetopos = this. _tomap(particle. lng, particle. lat, particle)
  111. var linetopos = this. _tomap1(particle. tlng, particle. tlat, particle)
  112. if (movetopos != null && linetopos != null) {
  113. this. _canvasContext. moveTo(movetopos[ 0], movetopos[ 1])
  114. this. _canvasContext. lineTo(linetopos[ 0], linetopos[ 1])
  115. }
  116. })
  117. this. _canvasContext. stroke()
  118. }
  119. // 根据粒子当前所处的位置(棋盘网格位置),计算经纬度,在根据经纬度返回屏幕坐标
  120. _tomap( lng, lat, particle) {
  121. if (!lng || !lat) {
  122. return null
  123. }
  124. var ct3 = Cesium. Cartesian3. fromDegrees(lng, lat, particle. height)
  125. // 判断当前点是否在地球可见端
  126. var isVisible = new Cesium. EllipsoidalOccluder( Cesium. Ellipsoid. WGS84, this. _viewer. camera. position). isPointVisible(ct3)
  127. var pos = Cesium. SceneTransforms. wgs84ToWindowCoordinates( this. _viewer. scene, ct3)
  128. if (!isVisible) {
  129. particle. age = 0
  130. }
  131. return pos ? [pos. x, pos. y] : null
  132. }
  133. _tomap1( lng, lat, particle) {
  134. if (!lng || !lat) {
  135. return null
  136. }
  137. var ct3 = Cesium. Cartesian3. fromDegrees(lng, lat, particle. theight)
  138. // 判断当前点是否在地球可见端
  139. var isVisible = new Cesium. EllipsoidalOccluder( Cesium. Ellipsoid. WGS84, this. _viewer. camera. position). isPointVisible(ct3)
  140. var pos = Cesium. SceneTransforms. wgs84ToWindowCoordinates( this. _viewer. scene, ct3)
  141. if (!isVisible) {
  142. particle. age = 0
  143. }
  144. return pos ? [pos. x, pos. y] : null
  145. }
  146. // 粒子是否在地图范围内
  147. _isInExtent( lng, lat) {
  148. if ((lng >= this. _windData. xmin && lng <= this. _windData. xmax) && (lat >= this. _windData. ymin && lat <= this. _windData. ymax)) return true
  149. return false
  150. }
  151. //创建棋盘格子
  152. _createWindField( ) {
  153. var k = 0
  154. var rows = null
  155. var uvw = null
  156. for ( var j = 0; j < this. _windData. rows; j++) {
  157. rows = []
  158. for ( var i = 0; i < this. _windData. cols; i++, k++) {
  159. uvw = this. _calcUVW( this. _windData. udata[k], this. _windData. vdata[k], this. _windData. wdata[k])
  160. rows. push(uvw)
  161. }
  162. this. _grid. push(rows)
  163. }
  164. console. log( this. _grid)
  165. }
  166. //计算风场向量
  167. _calcUVW( u, v, w) {
  168. return [+u, +v, +w, Math. sqrt(u * u + v * v)]
  169. }
  170. // 计算经纬度步进长度
  171. _calcStep( ) {
  172. var calcSpeed = this. _speedRate;
  173. this. _calc_speedRate = [( this. _windData. xmax - this. _windData. xmin) / calcSpeed, ( this. _windData. ymax - this. _windData. ymin) / calcSpeed]
  174. }
  175. //根据风场范围随机生成粒子
  176. _randomParticle( particle) {
  177. let safe = 30;
  178. let x = - 1;
  179. let y = - 1;
  180. let lng = null;
  181. let lat = null;
  182. do {
  183. try {
  184. lng = this. _fRandomByfloat( this. _windData. xmin, this. _windData. xmax)
  185. lat = this. _fRandomByfloat( this. _windData. ymin, this. _windData. ymax)
  186. } catch (e) {
  187. // console.log(e)
  188. }
  189. if (lng) {
  190. //找到随机生成的粒子的经纬度在棋盘的位置 x y
  191. var gridpos = this. _togrid(lng, lat);
  192. x = gridpos[ 0];
  193. y = gridpos[ 1];
  194. }
  195. } while ( this. _getIn(x, y)[ 2] <= 0 && safe++ < 30)
  196. let uvw = this. _getIn(x, y);
  197. var nextLng = lng + this. _calc_speedRate[ 0] * uvw[ 0]
  198. var nextLat = lat + this. _calc_speedRate[ 1] * uvw[ 1]
  199. particle. lng = lng
  200. particle. lat = lat
  201. //计算随机点的高程
  202. particle. height = mars3d. PointUtil. getHeight( this. _viewer. scene, new mars3d. LngLatPoint(particle. lng, particle. lat, 0));
  203. particle. x = x
  204. particle. y = y
  205. particle. tlng = nextLng
  206. particle. tlat = nextLat
  207. particle. speed = uvw[ 3]
  208. particle. age = Math. round( Math. random() * this. _maxAge) // 每一次生成都不一样
  209. //计算空间距离
  210. let d = mars3d. MeasureUtil. getDistance([ new mars3d. LngLatPoint(particle. lng, particle. lat, 0), new mars3d. LngLatPoint(particle. tlng, particle. tlat, 0)])
  211. let t = d / uvw[ 3];
  212. // console.log("距离"+d)
  213. // console.log("时间"+t)
  214. // console.log("速度"+particle.speed)
  215. particle. theight = particle. height + t * uvw[ 2];
  216. // console.log("垂直风速"+uvw[2])
  217. // console.log("一开始垂直高度"+particle.height)
  218. // console.log("垂直高度"+particle.theight)
  219. return particle;
  220. }
  221. _getIn( x, y) {
  222. // 局部风场使用
  223. if (x < 0 || x >= this. _windData. cols || y >= this. _windData. rows || y <= 0) {
  224. return [ 0, 0, 0]
  225. }
  226. var x0 = Math. floor(x) //向下取整
  227. var y0 = Math. floor(y)
  228. var x1;
  229. var y1
  230. if (x0 === x && y0 === y) return this. _grid[y][x]
  231. x1 = x0 + 1
  232. y1 = y0 + 1
  233. var g00 = this. _getIn(x0, y0)
  234. var g10 = this. _getIn(x1, y0)
  235. var g01 = this. _getIn(x0, y1)
  236. var g11 = this. _getIn(x1, y1)
  237. var result = null
  238. // console.log(x - x0, y - y0, g00, g10, g01, g11)
  239. try {
  240. result = this. _bilinearInterpolation(x - x0, y - y0, g00, g10, g01, g11)
  241. // console.log(result)
  242. } catch (e) {
  243. console. log(x, y)
  244. }
  245. return result
  246. }
  247. // 根据现有参数重新生成风场
  248. redraw( ) {
  249. window. cancelAnimationFrame( this. _animateFrame)
  250. this. _particles = [];
  251. this. _grid = [];
  252. this. _init()
  253. }
  254. // 二分差值算法计算给定节点的速度
  255. _bilinearInterpolation( x, y, g00, g10, g01, g11) {
  256. var rx = ( 1 - x)
  257. var ry = ( 1 - y)
  258. var a = rx * ry;
  259. var b = x * ry;
  260. var c = rx * y;
  261. var d = x * y
  262. var u = g00[ 0] * a + g10[ 0] * b + g01[ 0] * c + g11[ 0] * d
  263. var v = g00[ 1] * a + g10[ 1] * b + g01[ 1] * c + g11[ 1] * d
  264. var w = g00[ 2] * a + g10[ 2] * b + g01[ 2] * c + g11[ 2] * d
  265. return this. _calcUVW(u, v, w)
  266. }
  267. // 随机数生成器(小数)
  268. _fRandomByfloat( under, over) {
  269. return under + Math. random() * (over - under)
  270. }
  271. // 根据经纬度,算出棋盘格位置
  272. _togrid( lng, lat) {
  273. var x = (lng - this. _windData. xmin) / ( this. _windData. xmax - this. _windData. xmin) * ( this. _windData. cols - 1)
  274. var y = ( this. _windData. ymax - lat) / ( this. _windData. ymax - this. _windData. ymin) * ( this. _windData. rows - 1)
  275. return [x, y]
  276. }
  277. _resize( width, height) {
  278. this. canvasWidth = width
  279. this. canvasHeight = height
  280. }
  281. removeLines( ) {
  282. window. cancelAnimationFrame( this. _animateFrame)
  283. this. isdistory = true
  284. this. _canvas. width = 1
  285. document. getElementById( 'box'). removeChild( this. _canvas)
  286. }
  287. }
  288. function CanvasParticle( ) {
  289. this. lng = null // 粒子初始经度
  290. this. lat = null // 粒子初始纬度
  291. this. x = null // 粒子初始x位置(相对于棋盘网格,比如x方向有360个格,x取值就是0-360,这个是初始化时随机生成的)
  292. this. y = null // 粒子初始y位置(同上)
  293. this. tlng = null // 粒子下一步将要移动的经度,这个需要计算得来
  294. this. tlat = null // 粒子下一步将要移动的y纬度,这个需要计算得来
  295. this. age = null // 粒子生命周期计时器,每次-1
  296. this. speed = null // 粒子移动速度,可以根据速度渲染不同颜色
  297. }

然后页面调用

 

 初始化球和数据重构后

 网上有些加了重新绘制,但是我这个数据算起来比较麻烦不是静态的都是走接口然后自己拼起来的,加了比较耗时,我就去了,也不影响。

热力场我就不放了,你把数据搞好了就很简单了,数据怎么做我上面都说了,上面方法也有,你就从里面找。核心代码就是撒完点找到对应的网格,然后利用插值算法算出uv平方和开根号的风速值作为当前点的热力数值,即可!


转载:https://blog.csdn.net/KK_bluebule/article/details/128702207
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场