Typhoon.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * 单个台风对象
  3. * @class Typhoon
  4. */
  5. class Typhoon {
  6. //========== 构造方法 ==========
  7. constructor(options, map) {
  8. this.options = options;
  9. this.map = map;
  10. //创建台风相关矢量图层
  11. this.typhoonLayer = new mars3d.layer.GraphicLayer();
  12. map.addLayer(this.typhoonLayer);
  13. this.clickPtLayer = new mars3d.layer.GraphicLayer();
  14. map.addLayer(this.clickPtLayer);
  15. this.showTyphoonToMap(options.path);
  16. }
  17. //========== 对外属性 ==========
  18. //显示隐藏
  19. get show() {
  20. return this.options.show;
  21. }
  22. set show(val) {
  23. this.options.show = val;
  24. this.typhoonLayer.show = val;
  25. this.clickPtLayer.show = val;
  26. }
  27. getPointById(id) {
  28. return this.typhoonLayer.getGraphicById(id);
  29. }
  30. flyTo(options) {
  31. this.typhoonLayer.flyTo(options);
  32. }
  33. getPointTooltip(event, isYB) {
  34. let item = event.graphic?.attr;
  35. if (!item) {
  36. return;
  37. }
  38. let ybHtml = "";
  39. if (isYB) {
  40. ybHtml = `<p>预报机构:&nbsp;&nbsp;中央气象台</p>`;
  41. }
  42. let fqHtml = '<table style="width:100%">';
  43. if (item.circle7) {
  44. fqHtml += `<tr><th>风圈半径</th> <th>东北</th> <th>东南</th> <th>西南</th> <th>西北</th></tr>
  45. <tr><td>七级</td> <td>${item.circle7.radius1}</td> <td>${item.circle7.radius2}</td> <td>${item.circle7.radius3}</td> <td>${item.circle7.radius4}&nbsp;(KM)</td></tr> `;
  46. if (item.circle10) {
  47. fqHtml += ` <tr><td>十级</td> <td>${item.circle10.radius1}</td> <td>${item.circle10.radius2}</td> <td>${item.circle10.radius3}</td> <td>${item.circle10.radius4}&nbsp;(KM)</td></tr>`;
  48. if (item.circle12) {
  49. fqHtml += `<tr><td>十二级</td> <td>${item.circle12.radius1}</td> <td>${item.circle12.radius2}</td> <td>${item.circle12.radius3}</td> <td>${item.circle12.radius4}&nbsp;(KM)</td></tr>`;
  50. }
  51. }
  52. }
  53. fqHtml += "</table>";
  54. return `<div class="tipBox">
  55. <div class="triangle-left"></div>
  56. <div class="tipHeader">
  57. <p>${this.options.typnumber} ${this.options.name_cn}</p>
  58. </div>
  59. <div class="tipBodyFirstPart">
  60. ${ybHtml}
  61. <p>过去时间:&nbsp;&nbsp;${item.time_str}</p>
  62. <p>中心位置:&nbsp;&nbsp;${item.lat}N/${item.lon}E</p>
  63. <p>最大风速:&nbsp;&nbsp;${item.centerSpeed}米/秒</p>
  64. <p>中心气压:&nbsp;&nbsp;${item.strength}百帕</p>
  65. <p>移动方向:&nbsp;&nbsp;${item.moveTo_str}</p>
  66. <p>移动速度:&nbsp;&nbsp;${item.windSpeed}公里/小时</p>
  67. </div>
  68. <div class="tipBodySecondPart">${fqHtml}</div>
  69. </div>`;
  70. }
  71. addNameGraphic(firstItem) {
  72. //[起点]绘制台风起点名字
  73. var nameGraphic = new mars3d.graphic.RectanglePrimitive({
  74. positions: [
  75. [firstItem.lon, firstItem.lat],
  76. [firstItem.lon + 0.7, firstItem.lat - 0.4],
  77. ],
  78. style: {
  79. material: mars3d.MaterialUtil.createMaterial(mars3d.MaterialType.Text, {
  80. text: this.options.name_cn,
  81. font_size: 60,
  82. fillColor: "red",
  83. }),
  84. zIndex: 2,
  85. },
  86. attr: firstItem,
  87. });
  88. this.typhoonLayer.addGraphic(nameGraphic);
  89. }
  90. //绘制一个台风到地图上
  91. showTyphoonToMap(arr) {
  92. if (arr.length < 1) {
  93. return;
  94. }
  95. let firstItem = arr[0];
  96. let endItem = arr[arr.length - 1];
  97. let lastType;
  98. var arrPoint = [];
  99. //路径点
  100. for (let i = 0, len = arr.length; i < len; i++) {
  101. let item = arr[i];
  102. let point = [item.lon, item.lat];
  103. //在图层上绘画出所有的点
  104. let pointEntity = new mars3d.graphic.PointEntity({
  105. id: item.id,
  106. position: point,
  107. style: {
  108. pixelSize: 6,
  109. color: item.color,
  110. },
  111. attr: item,
  112. });
  113. this.typhoonLayer.addGraphic(pointEntity);
  114. //轨迹点绑定点击事件
  115. pointEntity.on(mars3d.EventType.click, (event) => {
  116. this.showPointFQ(event.graphic.attr);
  117. });
  118. pointEntity.bindTooltip(
  119. (event) => {
  120. return this.getPointTooltip(event);
  121. },
  122. {
  123. template: "",
  124. anchor: [260, -20],
  125. }
  126. );
  127. arrPoint.push(point);
  128. //判断台风的typlevel
  129. if (lastType !== item.level || i == len - 1) {
  130. //绘制线
  131. let graphicLine = new mars3d.graphic.PolylineEntity({
  132. positions: arrPoint,
  133. style: {
  134. color: getColor(lastType),
  135. },
  136. });
  137. this.typhoonLayer.addGraphic(graphicLine);
  138. lastType = item.level;
  139. arrPoint = [point];
  140. }
  141. }
  142. //[起点]绘制台风起点名字
  143. this.addNameGraphic(firstItem);
  144. //[终点]绘制台风当前位置gif点
  145. let gifGraphic = new mars3d.graphic.DivGraphic({
  146. position: [endItem.lon, endItem.lat],
  147. style: {
  148. html: `<img src="img/marker/typhoon.gif">`,
  149. horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
  150. verticalOrigin: Cesium.VerticalOrigin.CENTER,
  151. },
  152. });
  153. this.typhoonLayer.addGraphic(gifGraphic);
  154. this.gifGraphic = gifGraphic;
  155. //显示最后所在点的对应风圈等。
  156. this.showPointFQ(endItem);
  157. }
  158. //点击事件,点击出现台风icon
  159. showPointFQ(item, availability) {
  160. if (!availability) {
  161. this.clickPtLayer.clear();
  162. }
  163. let position = [item.lon, item.lat];
  164. //台风实时位置gif点更新
  165. if (this.gifGraphic) {
  166. this.gifGraphic.position = position;
  167. }
  168. // 绘制7级风圈面
  169. if (item.circle7) {
  170. let points7 = [];
  171. points7 = points7.concat(getPoints(position, item.circle7.radius1, 0)); //东北方向
  172. points7 = points7.concat(getPoints(position, item.circle7.radius2, 90)); //东南
  173. points7 = points7.concat(getPoints(position, item.circle7.radius3, 180)); //西南
  174. points7 = points7.concat(getPoints(position, item.circle7.radius4, 270)); //西北
  175. let graphic = new mars3d.graphic.PolygonEntity({
  176. positions: points7,
  177. availability: availability,
  178. style: {
  179. setHeight: 900,
  180. color: "#eed139",
  181. opacity: 0.3,
  182. outline: true,
  183. outlineWidth: 2,
  184. outlineColor: "#eed139",
  185. },
  186. });
  187. this.clickPtLayer.addGraphic(graphic);
  188. }
  189. //绘制10级风圈面
  190. if (item.circle10) {
  191. let points10 = [];
  192. points10 = points10.concat(getPoints(position, item.circle10.radius1, 0)); //东北方向
  193. points10 = points10.concat(getPoints(position, item.circle10.radius2, 90));
  194. points10 = points10.concat(getPoints(position, item.circle10.radius3, 180));
  195. points10 = points10.concat(getPoints(position, item.circle10.radius4, 270));
  196. let tenGraphic = new mars3d.graphic.PolygonEntity({
  197. positions: points10,
  198. availability: availability,
  199. style: {
  200. setHeight: 800,
  201. color: "#fe9c45",
  202. opacity: 0.3,
  203. outline: true,
  204. outlineWidth: 2,
  205. outlineColor: "#fe9c45",
  206. arcType: Cesium.ArcType.GEODESIC,
  207. },
  208. });
  209. this.clickPtLayer.addGraphic(tenGraphic);
  210. }
  211. //绘制12级风圈面
  212. if (item.circle12) {
  213. let points12 = [];
  214. points12 = points12.concat(getPoints(position, item.circle12.radius1, 0)); //东北方向
  215. points12 = points12.concat(getPoints(position, item.circle12.radius2, 90));
  216. points12 = points12.concat(getPoints(position, item.circle12.radius3, 180));
  217. points12 = points12.concat(getPoints(position, item.circle12.radius4, 270));
  218. let tenGraphic = new mars3d.graphic.PolygonEntity({
  219. positions: points12,
  220. availability: availability,
  221. style: {
  222. setHeight: 700,
  223. color: "#ffff00",
  224. opacity: 0.3,
  225. outline: true,
  226. outlineWidth: 2,
  227. outlineColor: "#ffff00",
  228. arcType: Cesium.ArcType.GEODESIC,
  229. },
  230. });
  231. this.clickPtLayer.addGraphic(tenGraphic);
  232. }
  233. //台风预测路径绘制
  234. if (item.forecast) {
  235. let linePoint = [position];
  236. item.forecast.forEach((element) => {
  237. let forecastPt = [element.lon, element.lat];
  238. linePoint.push(forecastPt);
  239. //在图层上绘画出所有的点
  240. let pointEntity = new mars3d.graphic.PointEntity({
  241. position: forecastPt,
  242. availability: availability,
  243. style: {
  244. pixelSize: 6,
  245. color: element.color, //不同typlevel显示不同的颜色
  246. opacity: 0.8,
  247. },
  248. attr: item,
  249. });
  250. this.clickPtLayer.addGraphic(pointEntity);
  251. pointEntity.bindTooltip(
  252. (event) => {
  253. return this.getPointTooltip(event, true);
  254. },
  255. {
  256. template: "",
  257. anchor: [260, -20],
  258. }
  259. );
  260. //预测路线
  261. let graphicLine = new mars3d.graphic.PolylineEntity({
  262. positions: linePoint,
  263. availability: availability,
  264. style: {
  265. material: mars3d.MaterialUtil.createMaterialProperty(mars3d.MaterialType.PolylineDash, {
  266. dashLength: 20.0,
  267. color: "red",
  268. }),
  269. },
  270. });
  271. this.clickPtLayer.addGraphic(graphicLine);
  272. });
  273. }
  274. }
  275. //释放
  276. destroy() {
  277. this.show = false;
  278. if (this.typhoonLayer) {
  279. this.typhoonLayer.destroy();
  280. delete this.typhoonLayer;
  281. }
  282. if (this.clickPtLayer) {
  283. this.clickPtLayer.destroy();
  284. delete this.clickPtLayer;
  285. }
  286. }
  287. }
  288. /**
  289. * 动态播放 单个台风对象
  290. * @class PlayTyphoon
  291. */
  292. class PlayTyphoon extends Typhoon {
  293. //========== 对外属性 ==========
  294. //显示隐藏
  295. get isStart() {
  296. return this._isStart;
  297. }
  298. set isStart(val) {
  299. this._isStart = val;
  300. }
  301. //绘制一个台风对应的对象。
  302. showTyphoonToMap(arr) {
  303. if (arr.length < 1) {
  304. return;
  305. }
  306. let firstItem = arr[0];
  307. let endItem = arr[arr.length - 1];
  308. this.startTime = Cesium.JulianDate.fromDate(firstItem.time); //开始时间
  309. this.stopTime = Cesium.JulianDate.fromDate(endItem.time); //结束时间
  310. let lastType = arr[0].level;
  311. let property = new Cesium.SampledPositionProperty();
  312. for (let i = 0, len = arr.length; i < len; i++) {
  313. let item = arr[i];
  314. let point = [item.lon, item.lat];
  315. let position = Cesium.Cartesian3.fromDegrees(item.lon, item.lat); //经度、纬度坐标转化
  316. let pointTime = Cesium.JulianDate.fromDate(item.time); //将时间转化成需要的格式
  317. property.addSample(pointTime, position);
  318. //绘制点
  319. let pointEntity = new mars3d.graphic.PointEntity({
  320. id: item.id,
  321. position: point,
  322. availability: new Cesium.TimeIntervalCollection([
  323. new Cesium.TimeInterval({
  324. start: pointTime,
  325. stop: this.stopTime,
  326. }),
  327. ]),
  328. style: {
  329. pixelSize: 6,
  330. color: item.color,
  331. },
  332. attr: item,
  333. });
  334. pointEntity.bindTooltip(
  335. (event) => {
  336. return this.getPointTooltip(event);
  337. },
  338. {
  339. template: "",
  340. anchor: [260, -20],
  341. }
  342. );
  343. this.typhoonLayer.addGraphic(pointEntity);
  344. if (lastType !== item.level || i == len - 1) {
  345. //绘制线
  346. let graphicLine = new mars3d.graphic.PathEntity({
  347. position: property,
  348. style: {
  349. leadTime: 0,
  350. color: getColor(lastType),
  351. },
  352. });
  353. this.typhoonLayer.addGraphic(graphicLine);
  354. lastType = item.level;
  355. property = new Cesium.SampledPositionProperty(); //控制动画播放的对象
  356. property.addSample(pointTime, position);
  357. }
  358. //显示每个点的风圈和预测路线
  359. let lastTime;
  360. if (i == len - 1) {
  361. lastTime = this.lastTime;
  362. } else {
  363. lastTime = Cesium.JulianDate.fromDate(arr[i + 1].time);
  364. }
  365. let availability = new Cesium.TimeIntervalCollection([
  366. new Cesium.TimeInterval({
  367. start: pointTime,
  368. stop: lastTime,
  369. }),
  370. ]);
  371. this.showPointFQ(item, availability);
  372. }
  373. //[起点]绘制台风起点名字
  374. this.addNameGraphic(firstItem);
  375. }
  376. //开始播放
  377. start() {
  378. this._isStart = true;
  379. this.map.clock.startTime = this.startTime.clone();
  380. this.map.clock.stopTime = this.stopTime.clone();
  381. this.map.clock.currentTime = this.startTime.clone();
  382. this.map.clock.clockRange = Cesium.ClockRange.CLAMPED; //到达时间点后终止
  383. this.map.clock.multiplier = 16000;
  384. if (this.map.viewer.timeline) {
  385. this.map.viewer.timeline.zoomTo(this.startTime, this.stopTime);
  386. }
  387. this.show = true;
  388. this.map.clock.shouldAnimate = true;
  389. }
  390. //停止播放
  391. stop() {
  392. this._isStart = false;
  393. this.show = false;
  394. let now = Cesium.JulianDate.fromDate(new Date());
  395. this.map.clock.startTime = now.clone();
  396. this.map.clock.stopTime = Cesium.JulianDate.addDays(now, 1.0, new Cesium.JulianDate());
  397. this.map.clock.currentTime = now.clone();
  398. this.map.clock.clockRange = Cesium.ClockRange.UNBOUNDED;
  399. this.map.clock.multiplier = 1;
  400. }
  401. }
  402. //不同等级的台风对应不同的颜色
  403. function getColor(level) {
  404. switch (level) {
  405. default:
  406. case "TD": //热带低压
  407. return "rgb(238,209,57)";
  408. case "TS": //热带风暴
  409. return "rgb(0,0,255)";
  410. case "STS": //强热带风暴
  411. return "rgb(15,128,0)";
  412. case "TY": //台风
  413. return "rgb(254,156,69)";
  414. case "STY": //强台风
  415. return "rgb(254,0,254)";
  416. case "SuperTY": //超强台风
  417. return "rgb(254,0,0)";
  418. }
  419. }
  420. // 根据经纬度 直径 以及方向计算方法
  421. function getPoints(center, cradius, startAngle) {
  422. let points = [];
  423. let radius = cradius / 100;
  424. let pointNum = 90;
  425. let endAngle = startAngle + 90;
  426. let sin, cos, x, y, angle;
  427. for (let i = 0; i <= pointNum; i++) {
  428. angle = startAngle + ((endAngle - startAngle) * i) / pointNum;
  429. sin = Math.sin((angle * Math.PI) / 180);
  430. cos = Math.cos((angle * Math.PI) / 180);
  431. x = center[0] + radius * sin;
  432. y = center[1] + radius * cos;
  433. points.push([x, y]);
  434. }
  435. return points;
  436. }