oceanCurrent.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /**
  2. * 海流功能
  3. * Created by Administrator on 2017/11/7.
  4. */
  5. define([], function () {
  6. var ocean = {
  7. id: 'ocean',
  8. title: '海流',
  9. legend: 'images/meteo/legend/ocean.png',
  10. isInit: false,
  11. level: null, //当前要显示的层级
  12. layer: null, //所在地图图层id
  13. slat: null, //开始lat
  14. slng: null, //开始lng
  15. elat: null, //结束lat
  16. elng: null, //结束lng
  17. delta: null, //每个数据地理间隔
  18. rowNum: null, //lng跨度
  19. colNum: null, //lat跨度
  20. wd: null, //流向数组
  21. ws: null, //流速数据
  22. image: null, //图标所在图片
  23. interval: 1, //绘制间隔(控制疏密程度)
  24. timeOutId: null,
  25. elem: 'OWD',
  26. elem2: 'OWS',
  27. level: null,
  28. levelName: {
  29. '0000': '海表',
  30. '0010': '10米',
  31. '0020': '20米',
  32. '0030': '30米',
  33. '0050': '50米'
  34. },
  35. buttons: {},
  36. isHide: false,
  37. getData: function () { //获取风羽数据
  38. var dateStr = meteo.c.time.getTime();
  39. // var dateStr = '2018-01-07 14:00:00';
  40. var vti = meteo.c.time.getVti();
  41. var params = {
  42. dateStr: dateStr,
  43. vti: vti,
  44. elem: ocean.elem,
  45. level: ocean.level
  46. }
  47. meteo.c.http.httpFunction(meteo.c.http.oceanData, null, params, function (json) {
  48. ocean.slat = json.slat;
  49. ocean.slng = json.slng;
  50. ocean.elat = json.elat;
  51. ocean.elng = json.elng;
  52. ocean.delta = json.delta;
  53. ocean.rowNum = json.rowNum;
  54. ocean.colNum = json.colNum;
  55. ocean.wd = json.vals;
  56. ocean.showOceanWd();
  57. }, function () {
  58. // ONEMAP.C.publisher.publish({ type: 'warning', message: '海流流向暂无当前时次/层次数据' }, 'noteBar::add');
  59. });
  60. var params2 = {
  61. dateStr: dateStr,
  62. vti: vti,
  63. elem: ocean.elem2,
  64. level: ocean.level
  65. }
  66. meteo.c.http.httpFunction(meteo.c.http.oceanImage, null, params2, function (json) {
  67. meteo.c.title.updateTitle(ocean.id,
  68. meteo.c.process.setTitleTime(json.odate, json.vti, ocean.title, ocean.levelName[ocean.level]));
  69. $('#meteo-video-query-startDate').val(json.odate);
  70. json = JSON.parse(json.data);
  71. ocean.ws = json;
  72. ocean.showOceanWs(json);
  73. }, function () {
  74. // ONEMAP.C.publisher.publish({ type: 'warning', message: '海流流速暂无当前时次/层次数据' }, 'noteBar::add');
  75. // meteo.c.title.updateTitle(ocean.id,'海流暂无当前时次/层次数据');
  76. })
  77. params2.vti = parseInt(vti) + 3;
  78. meteo.c.http.httpFunction(meteo.c.http.oceanImage, null, params2, function (json) {
  79. json = json.data;
  80. json = JSON.parse(json);
  81. ocean.loadImage(json);
  82. }, function () {
  83. })
  84. },
  85. showOceanWs: function (data) {
  86. var images = [];
  87. var latI = (data.elat - data.slat) / data.row;
  88. var lngI = (data.elng - data.slng) / data.col;
  89. for (var i = 0; i < data.files.length; i++) {
  90. var value = data.files[i];
  91. if (value) {
  92. var str = value.split("\.")[0];
  93. str = str.split("_");
  94. var x = str[0];
  95. var y = str[1];
  96. // var slat = 75 - (x + 1) * 47.5;
  97. var slat = data.slat + x * latI;
  98. var slng = data.slng + y * lngI;
  99. var elat = slat + latI;
  100. var elng = slng + lngI;
  101. elat = elat > data.elat ? data.elat : elat;
  102. elng = elng > data.elng ? data.elat : elng;
  103. var url = data.url + value + '.mkt';
  104. images.push(meteo.c.map.addImage(url, slat, slng, elat, elng));
  105. // L.imageOverlay(
  106. // // data.url + value + '.mkt',
  107. // "scripts/meteo/test/wyl/WYL_2018010520_000_OWS_0000_" + x + "_" + y + ".png.mkt",
  108. // L.latLngBounds(L.latLng(slat, slng), L.latLng(elat, elng))
  109. // ).addTo(layer);
  110. }
  111. }
  112. if (!ocean.layer|| !ocean.layer.tLayer || ocean.isHide) {
  113. for (var i = 0; i < images.length; i++) {
  114. meteo.c.map.removeImage(images[i]);
  115. }
  116. } else {
  117. if (ocean.layer.images) {
  118. ocean.removeImage();
  119. }
  120. ocean.layer.images = images;
  121. }
  122. // setTimeout(function () {
  123. //
  124. // }, 300);
  125. },
  126. loadImage:function (data) { //预先加载图片
  127. for (var i = 0; i < data.files.length; i++) {
  128. var value = data.files[i];
  129. if (value) {
  130. var url = data.url + value + '.mkt';
  131. var img = $('<img />').attr('src', url);
  132. img = null;
  133. }
  134. }
  135. },
  136. showOceanWd: function () { //在当前显示区域内绘制
  137. if (!ocean.layer) {
  138. return;
  139. }
  140. //计算当前屏幕内显示的索引
  141. ocean.getAirPlumeLevel();
  142. var se = ocean.getBounds();
  143. var layerId = meteo.c.map.createLayer();
  144. var layer = map2DViewer.groups[layerId];
  145. //计算流向绘制屏幕内的初始索引
  146. var istart = (ocean.elat - se[2]) / ocean.delta >> 0;
  147. var jstart = (se[1] - ocean.slng) / ocean.delta >> 0;
  148. if (istart < 0) istart = 0;
  149. if (jstart < 0) jstart = 0;
  150. //计算流向绘制屏幕内的最大索引
  151. var row = Math.abs(se[0] - ocean.elat) / ocean.delta + 1;
  152. // var row = Math.abs(se[2] - ocean.slat) / ocean.delta + 1;
  153. var col = Math.abs(se[3] - ocean.slng) / ocean.delta + 1;
  154. if (row > ocean.rowNum) row = ocean.rowNum;
  155. if (col > ocean.colNum) col = ocean.colNum;
  156. for (var i = istart; i < row; i += ocean.interval) {
  157. for (var j = jstart; j < col; j += ocean.interval) {
  158. var lat = ocean.elat - i * ocean.delta;
  159. var lng = ocean.slng + j * ocean.delta;
  160. var wd = ocean.wd[i][j];
  161. if (wd < 0) continue;
  162. var icon = ocean.getIcon(wd, 30);
  163. L.marker([lat, lng], {icon: icon}).addTo(layer);
  164. // meteo.c.map.addText('', i + "*" + j, 16, "#000000", 1, lat, lng, layerId); //打印数据
  165. }
  166. }
  167. if (!ocean.layer || !ocean.layer.tLayer || ocean.isHide) {
  168. meteo.c.map.removeLayer(layerId);
  169. return;
  170. }
  171. if (ocean.layer.cLayer != null) {
  172. meteo.c.map.removeLayer(ocean.layer.cLayer);
  173. }
  174. ocean.layer.cLayer = layerId;
  175. //判断延时是否取消
  176. if (ocean.timeOutId) ocean.timeOutId = null;
  177. },
  178. getIcon: function (wd, size) { //获得图标
  179. var width = size * 1.2;
  180. var a = size * 0.6;
  181. var b = size / 2;
  182. var canvas = document.createElement('canvas');
  183. canvas.width = width;
  184. canvas.height = width;
  185. if (canvas.getContext) {
  186. //获取对应的CanvasRenderingContext2D对象(画笔)
  187. var ctx = canvas.getContext("2d");
  188. ctx.save();
  189. ctx.translate(a, a);
  190. ctx.rotate(wd * Math.PI / 180);
  191. ctx.translate(-a, -a);
  192. ctx.drawImage(ocean.image, a - b / 2, a - b, b, size);
  193. ctx.restore();
  194. var url = canvas.toDataURL('image/png');
  195. return L.icon({
  196. iconUrl: url,
  197. iconSize: [size, size],
  198. iconAnchor: [size / 2, size / 2],
  199. });
  200. }
  201. return null;
  202. },
  203. getAirPlumeLevel: function () { //根据地图缩放等级获取风羽疏密等级
  204. var zoom = map2DViewer.map.getZoom();
  205. ocean.interval = zoom < 2 ? 32 :
  206. zoom < 4 ? 24 :
  207. zoom < 5 ? 16 :
  208. zoom < 6 ? 12 :
  209. zoom < 7 ? 8 :
  210. zoom < 8 ? 4 :
  211. zoom < 10 ? 2 :
  212. 1;
  213. },
  214. getBounds: function () { //获取当前显示范围
  215. var se = [];
  216. se.push(map2DViewer.map.getBounds().getSouthWest().lat);
  217. se.push(map2DViewer.map.getBounds().getSouthWest().lng);
  218. se.push(map2DViewer.map.getBounds().getNorthEast().lat);
  219. se.push(map2DViewer.map.getBounds().getNorthEast().lng);
  220. return se;
  221. },
  222. create: function () {
  223. ocean.layer = {
  224. tLayer: meteo.c.map.createLayer(),
  225. cLayer: null,
  226. images: null, //图片图层列表
  227. }
  228. },
  229. remove: function () {
  230. if (ocean.layer) {
  231. meteo.c.map.removeLayer(ocean.layer.tLayer);
  232. meteo.c.map.removeLayer(ocean.layer.cLayer);
  233. ocean.removeImage();
  234. ocean.layer = null;
  235. }
  236. },
  237. removeImage: function () {
  238. if (ocean.layer.images) {
  239. for (var i = 0; i < ocean.layer.images.length; i++) {
  240. meteo.c.map.removeImage(ocean.layer.images[i]);
  241. }
  242. ocean.layer.images = null;
  243. }
  244. },
  245. show: function () {
  246. ocean.isHide = false;
  247. ocean.showOceanWd();
  248. ocean.showOceanWs(ocean.ws);
  249. },
  250. hide: function () {
  251. ocean.isHide = true;
  252. if (ocean.layer) {
  253. meteo.c.map.removeLayer(ocean.layer.cLayer);
  254. ocean.layer.cLayer = null;
  255. ocean.removeImage();
  256. }
  257. },
  258. open: function () {
  259. if(ONEMAP.M.myLayers.checkLength() >= map23DConfig.layerMaxLength) {
  260. ONEMAP.C.publisher.publish({ type: 'warning', message: '图层数量已达上限' }, 'noteBar::add');
  261. return;
  262. }
  263. //-------------创建图层------------------
  264. if (ocean.layer) {
  265. ocean.close();
  266. }
  267. ocean.create();
  268. //----------------图层管理-------------------
  269. var options = {
  270. action: "add",
  271. // mod: "qixiang",
  272. DOM: {
  273. guid: ocean.layer.tLayer,
  274. type: "group",
  275. name: ocean.title,
  276. },
  277. }
  278. var guid = ONEMAP.M.myLayers.myLayerControl(options);
  279. ONEMAP.C.publisher.subscribe(function (option) {
  280. switch (option.action) {
  281. case 'remove':
  282. ocean.close();
  283. break;
  284. case 'opacity':
  285. if (option.options.opacity) {
  286. ocean.show();
  287. } else {
  288. ocean.hide();
  289. }
  290. break;
  291. }
  292. }, guid);
  293. //-------------------------------------
  294. ocean.buttons[ocean.level].parent().addClass('current');
  295. meteo.c.title.addTitle(ocean.id);
  296. meteo.c.legend.addLegend(ocean.id, ocean.legend);
  297. meteo.c.time.open(true);
  298. //--------------------------------------
  299. ocean.isHide = false;
  300. ocean.getData();
  301. //-----------------第一次开启功能时添加动态绘制--------------------
  302. // if (ocean.isInit) return;
  303. // ocean.isInit = true;
  304. // map2DViewer.map.on("moveend", function (e) {
  305. // if (ocean.layer && ocean.layer.cLayer) {
  306. // if (ocean.timeOutId) { //如果有未开始的流向绘制,就取消对应任务
  307. // window.clearTimeout(ocean.timeOutId);
  308. // }
  309. // ocean.timeOutId = window.setTimeout(function () {
  310. // ocean.showOceanWd();
  311. // }, 800)
  312. // }
  313. // });
  314. },
  315. close: function () {
  316. var options = {
  317. action: "remove",
  318. DOMid: ocean.layer.tLayer,
  319. }
  320. ONEMAP.M.myLayers.myLayerControl(options);
  321. ocean.buttons[ocean.level].parent().removeClass('current');
  322. meteo.c.title.removeTitle(ocean.id);
  323. meteo.c.legend.removeLegend(ocean.id);
  324. meteo.c.time.close();
  325. ocean.level = null;
  326. ocean.remove();
  327. },
  328. update: function () { //用于时间变化时更新数据
  329. if (!ocean.layer || ocean.isHide) return;
  330. // var option = {
  331. // action: 'update',
  332. // DOMid: ocean.layer.tLayer,
  333. // DOM: {
  334. // name: '海流'
  335. // }
  336. // }
  337. // ONEMAP.M.myLayers.myLayerControl(option);
  338. ocean.getData()
  339. },
  340. updateLevel: function () {
  341. ocean.buttons[ocean.level].parent().siblings().removeClass('current');
  342. ocean.buttons[ocean.level].parent().addClass('current');
  343. ocean.getData();
  344. },
  345. click: function (level) {
  346. if (ocean.level && level != ocean.level) {
  347. ocean.level = level;
  348. ocean.updateLevel();
  349. } else {
  350. ocean.level = level;
  351. if (ocean.layer) {
  352. ocean.close();
  353. } else {
  354. ocean.open();
  355. }
  356. }
  357. },
  358. onMove: function () {
  359. if (ocean.layer && !ocean.isHide && meteo.c.map.mapType == '2d') {
  360. if (ocean.timeOutId) { //如果有未开始的流向绘制,就取消对应任务
  361. window.clearTimeout(ocean.timeOutId);
  362. }
  363. ocean.timeOutId = window.setTimeout(function () {
  364. ocean.showOceanWd();
  365. }, 800)
  366. }
  367. },
  368. init: function () {
  369. ocean.image = new Image();
  370. ocean.image.src = meteo.c.http.myImageUrlJs + "oceanCurrent/red.png";
  371. var buttons = $('.meteo-button-ocean');
  372. ocean.buttons['0000'] = buttons.eq(10);
  373. ocean.buttons['0010'] = buttons.eq(11);
  374. ocean.buttons['0020'] = buttons.eq(12);
  375. ocean.buttons['0030'] = buttons.eq(13);
  376. ocean.buttons['0050'] = buttons.eq(14);
  377. }
  378. }
  379. meteo.f.ocean = ocean;
  380. return meteo.f.ocean;
  381. })