node-red-contrib-jwht-map/worldmap/leaflet/leaflet-velocity.min.js
2018-06-26 20:42:24 +01:00

1 line
12 KiB
JavaScript

"use strict";L.DomUtil.setTransform||(L.DomUtil.setTransform=function(t,n,e){var i=n||new L.Point(0,0);t.style[L.DomUtil.TRANSFORM]=(L.Browser.ie3d?"translate("+i.x+"px,"+i.y+"px)":"translate3d("+i.x+"px,"+i.y+"px,0)")+(e?" scale("+e+")":"")}),L.CanvasLayer=(L.Layer?L.Layer:L.Class).extend({initialize:function(t){this._map=null,this._canvas=null,this._frame=null,this._delegate=null,L.setOptions(this,t)},delegate:function(t){return this._delegate=t,this},needRedraw:function(){return this._frame||(this._frame=L.Util.requestAnimFrame(this.drawLayer,this)),this},_onLayerDidResize:function(t){this._canvas.width=t.newSize.x,this._canvas.height=t.newSize.y},_onLayerDidMove:function(){var t=this._map.containerPointToLayerPoint([0,0]);L.DomUtil.setPosition(this._canvas,t),this.drawLayer()},getEvents:function(){var t={resize:this._onLayerDidResize,moveend:this._onLayerDidMove};return this._map.options.zoomAnimation&&L.Browser.any3d&&(t.zoomanim=this._animateZoom),t},onAdd:function(t){this._map=t,this._canvas=L.DomUtil.create("canvas","leaflet-layer"),this.tiles={};var n=this._map.getSize();this._canvas.width=n.x,this._canvas.height=n.y;var e=this._map.options.zoomAnimation&&L.Browser.any3d;L.DomUtil.addClass(this._canvas,"leaflet-zoom-"+(e?"animated":"hide")),t._panes.overlayPane.appendChild(this._canvas),t.on(this.getEvents(),this);var i=this._delegate||this;i.onLayerDidMount&&i.onLayerDidMount(),this.needRedraw();var o=this;setTimeout(function(){o._onLayerDidMove()},0)},onRemove:function(t){var n=this._delegate||this;n.onLayerWillUnmount&&n.onLayerWillUnmount(),t.getPanes().overlayPane.removeChild(this._canvas),t.off(this.getEvents(),this),this._canvas=null},addTo:function(t){return t.addLayer(this),this},LatLonToMercator:function(t){return{x:6378137*t.lng*Math.PI/180,y:6378137*Math.log(Math.tan((90+t.lat)*Math.PI/360))}},drawLayer:function(){var t=this._map.getSize(),n=this._map.getBounds(),e=this._map.getZoom(),i=this.LatLonToMercator(this._map.getCenter()),o=this.LatLonToMercator(this._map.containerPointToLatLng(this._map.getSize())),a=this._delegate||this;a.onDrawLayer&&a.onDrawLayer({layer:this,canvas:this._canvas,bounds:n,size:t,zoom:e,center:i,corner:o}),this._frame=null},_setTransform:function(t,n,e){var i=n||new L.Point(0,0);t.style[L.DomUtil.TRANSFORM]=(L.Browser.ie3d?"translate("+i.x+"px,"+i.y+"px)":"translate3d("+i.x+"px,"+i.y+"px,0)")+(e?" scale("+e+")":"")},_animateZoom:function(t){var n=this._map.getZoomScale(t.zoom),e=L.Layer?this._map._latLngToNewLayerPoint(this._map.getBounds().getNorthWest(),t.zoom,t.center):this._map._getCenterOffset(t.center)._multiplyBy(-n).subtract(this._map._getMapPanePos());L.DomUtil.setTransform(this._canvas,e,n)}}),L.canvasLayer=function(){return new L.CanvasLayer},L.Control.Velocity=L.Control.extend({options:{position:"bottomleft",emptyString:"Unavailable",angleConvention:"bearingCCW",speedUnit:"m/s"},onAdd:function(t){return this._container=L.DomUtil.create("div","leaflet-control-velocity"),L.DomEvent.disableClickPropagation(this._container),t.on("mousemove",this._onMouseMove,this),this._container.innerHTML=this.options.emptyString,this._container},onRemove:function(t){t.off("mousemove",this._onMouseMove,this)},vectorToSpeed:function(t,n,e){var i=Math.sqrt(Math.pow(t,2)+Math.pow(n,2));return"k/h"===e?this.meterSec2kilometerHour(i):"kt"===e?this.meterSec2Knots(i):i},vectorToDegrees:function(t,n,e){e.endsWith("CCW")&&(n=n>0?n=-n:Math.abs(n));var i=Math.sqrt(Math.pow(t,2)+Math.pow(n,2)),o=Math.atan2(t/i,n/i),a=180*o/Math.PI+180;return"bearingCW"!==e&&"meteoCCW"!==e||(a+=180,a>=360&&(a-=360)),a},meterSec2Knots:function(t){return t/.514},meterSec2kilometerHour:function(t){return 3.6*t},_onMouseMove:function(t){var n=this,e=this.options.leafletVelocity._map.containerPointToLatLng(L.point(t.containerPoint.x,t.containerPoint.y)),i=this.options.leafletVelocity._windy.interpolatePoint(e.lng,e.lat),o="";o=i&&!isNaN(i[0])&&!isNaN(i[1])&&i[2]?"<strong>"+this.options.velocityType+" Direction: </strong>"+n.vectorToDegrees(i[0],i[1],this.options.angleConvention).toFixed(2)+"°, <strong>"+this.options.velocityType+" Speed: </strong>"+n.vectorToSpeed(i[0],i[1],this.options.speedUnit).toFixed(2)+this.options.speedUnit:this.options.emptyString,n._container.innerHTML=o,0==$(".leaflet-control-velocity").index()&&$(".leaflet-control-velocity").insertAfter(".leaflet-control-mouseposition")}}),L.Map.mergeOptions({positionControl:!1}),L.Map.addInitHook(function(){this.options.positionControl&&(this.positionControl=new L.Control.MousePosition,this.addControl(this.positionControl))}),L.control.velocity=function(t){return new L.Control.Velocity(t)},L.VelocityLayer=(L.Layer?L.Layer:L.Class).extend({options:{displayValues:!0,displayOptions:{velocityType:"Velocity",position:"bottomleft",emptyString:"No velocity data"},maxVelocity:10,colorScale:null,data:null},_map:null,_canvasLayer:null,_windy:null,_context:null,_timer:0,_mouseControl:null,initialize:function(t){L.setOptions(this,t)},onAdd:function(t){this._canvasLayer=L.canvasLayer().delegate(this),this._canvasLayer.addTo(t),this._map=t},onRemove:function(t){this._destroyWind()},setData:function(t){this.options.data=t,this._windy&&(this._windy.setData(t),this._clearAndRestart()),this.fire("load")},onDrawLayer:function(t,n){var e=this;return this._windy?void(this.options.data&&(this._timer&&clearTimeout(e._timer),this._timer=setTimeout(function(){e._startWindy()},750))):void this._initWindy(this)},_startWindy:function(){var t=this._map.getBounds(),n=this._map.getSize();this._windy.start([[0,0],[n.x,n.y]],n.x,n.y,[[t._southWest.lng,t._southWest.lat],[t._northEast.lng,t._northEast.lat]])},_initWindy:function(t){var n=Object.assign({canvas:t._canvasLayer._canvas},t.options);this._windy=new Windy(n),this._context=this._canvasLayer._canvas.getContext("2d"),this._canvasLayer._canvas.classList.add("velocity-overlay"),this.onDrawLayer(),this._map.on("dragstart",t._windy.stop),this._map.on("dragend",t._clearAndRestart),this._map.on("zoomstart",t._windy.stop),this._map.on("zoomend",t._clearAndRestart),this._map.on("resize",t._clearWind),this._initMouseHandler()},_initMouseHandler:function(){if(!this._mouseControl&&this.options.displayValues){var t=this.options.displayOptions||{};t.leafletVelocity=this,this._mouseControl=L.control.velocity(t).addTo(this._map)}},_clearAndRestart:function(){this._context&&this._context.clearRect(0,0,3e3,3e3),this._windy&&this._startWindy()},_clearWind:function(){this._windy&&this._windy.stop(),this._context&&this._context.clearRect(0,0,3e3,3e3)},_destroyWind:function(){this._timer&&clearTimeout(this._timer),this._windy&&this._windy.stop(),this._context&&this._context.clearRect(0,0,3e3,3e3),this._mouseControl&&this._map.removeControl(this._mouseControl),this._mouseControl=null,this._windy=null,this._map.removeLayer(this._canvasLayer)}}),L.velocityLayer=function(t){return new L.VelocityLayer(t)};var Windy=function(t){var n,e,i,o,a,r,s,h,l,c,u=t.minVelocity||0,d=t.maxVelocity||10,m=(t.velocityScale||.005)*(Math.pow(window.devicePixelRatio,1/3)||1),f=t.particleAge||90,y=t.lineWidth||1,_=t.particleMultiplier||1/300,p=Math.pow(window.devicePixelRatio,1/3)||1.6,v=t.frameRate||15,g=1e3/v,L=["rgb(36,104, 180)","rgb(60,157, 194)","rgb(128,205,193 )","rgb(151,218,168 )","rgb(198,231,181)","rgb(238,247,217)","rgb(255,238,159)","rgb(252,217,125)","rgb(255,182,100)","rgb(252,150,75)","rgb(250,112,52)","rgb(245,64,32)","rgb(237,45,28)","rgb(220,24,32)","rgb(180,0,35)"],M=t.colorScale||L,w=[NaN,NaN,null],x=t.data,C=function(t){x=t},b=function(t,n,e,i,o,a){var r=1-t,s=1-n,h=r*s,l=t*s,c=r*n,u=t*n,d=e[0]*h+i[0]*l+o[0]*c+a[0]*u,m=e[1]*h+i[1]*l+o[1]*c+a[1]*u;return[d,m,Math.sqrt(d*d+m*m)]},T=function(t,n){var e=t.data,i=n.data;return{header:t.header,data:function(t){return[e[t],i[t]]},interpolate:b}},D=function(t){var n=null,e=null,i=null;return t.forEach(function(t){switch(t.header.parameterCategory+","+t.header.parameterNumber){case"1,2":case"2,2":n=t;break;case"1,3":case"2,3":e=t;break;default:i=t}}),T(n,e)},P=function(t,c){n=D(t);var u=n.header;o=u.lo1,a=u.la1,r=u.dx,s=u.dy,h=u.nx,l=u.ny,i=new Date(u.refTime),i.setHours(i.getHours()+u.forecastTime),e=[];for(var d=0,m=Math.floor(h*r)>=360,f=0;f<l;f++){for(var y=[],_=0;_<h;_++,d++)y[_]=n.data(d);m&&y.push(y[0]),e[f]=y}c({date:i,interpolate:z})},z=function(t,i){if(!e)return null;var h,l=W(t-o,360)/r,c=(a-i)/s,u=Math.floor(l),d=u+1,m=Math.floor(c),f=m+1;if(h=e[m]){var y=h[u],_=h[d];if(S(y)&&S(_)&&(h=e[f])){var p=h[u],v=h[d];if(S(p)&&S(v))return n.interpolate(l-u,c-m,y,_,p,v)}}return null},S=function(t){return null!==t&&void 0!==t},W=function(t,n){return t-n*Math.floor(t/n)},R=function(){return/android|blackberry|iemobile|ipad|iphone|ipod|opera mini|webos/i.test(navigator.userAgent)},A=function(t,n,e,i,o,a,r,s){var h=r[0]*a,l=r[1]*a,c=U(t,n,e,i,o,s);return r[0]=c[0]*h+c[2]*l,r[1]=c[1]*h+c[3]*l,r},U=function(t,n,e,i,o,a){var r=2*Math.PI,s=Math.pow(10,-5.2),h=n<0?s:-s,l=e<0?s:-s,c=O(e,n+h,a),u=O(e+l,n,a),d=Math.cos(e/360*r);return[(c[0]-i)/h/d,(c[1]-o)/h/d,(u[0]-i)/l,(u[1]-o)/l]},N=function(t,n,e){function i(n,e){var i=t[Math.round(n)];return i&&i[Math.round(e)]||w}i.release=function(){t=[]},i.randomize=function(t){var e,o,a=0;do e=Math.round(Math.floor(Math.random()*n.width)+n.x),o=Math.round(Math.floor(Math.random()*n.height)+n.y);while(null===i(e,o)[2]&&a++<30);return t.x=e,t.y=o,t},e(n,i)},F=function(t,n,e){var i=t[0],o=t[1],a=Math.round(i[0]),r=Math.max(Math.floor(i[1],0),0),s=(Math.min(Math.ceil(o[0],n),n-1),Math.min(Math.ceil(o[1],e),e-1));return{x:a,y:r,xMax:n,yMax:s,width:n,height:e}},V=function(t){return t/180*Math.PI},k=function(t){return t/(Math.PI/180)},E=function(t,n,e){var i=e.east-e.west,o=e.width/k(i)*360/(2*Math.PI),a=o/2*Math.log((1+Math.sin(e.south))/(1-Math.sin(e.south))),r=e.height+a,s=(r-n)/o,h=180/Math.PI*(2*Math.atan(Math.exp(s))-Math.PI/2),l=k(e.west)+t/e.width*k(i);return[l,h]},I=function(t){return Math.log(Math.tan(t/2+Math.PI/4))},O=function(t,n,e){var i=I(e.south),o=I(e.north),a=e.width/(e.east-e.west),r=e.height/(o-i),s=I(V(t)),h=(V(n)-e.west)*a,s=(o-s)*r;return[h,s]},H=function(t,n,e,i){function o(i){for(var o=[],r=n.y;r<=n.yMax;r+=2){var l=E(i,r,e);if(l){var c=l[0],u=l[1];if(isFinite(c)){var d=t.interpolate(c,u);d&&(d=A(a,c,u,i,r,s,d,e),o[r+1]=o[r]=d)}}}h[i+1]=h[i]=o}var a={},r=(e.south-e.north)*(e.west-e.east),s=m*Math.pow(r,.4),h=[],l=n.x;!function c(){for(var t=Date.now();l<n.width;)if(o(l),l+=2,Date.now()-t>1e3)return void setTimeout(c,25);N(h,n,i)}()},B=function(n,e){function i(t,n){return M.indexFor=function(e){return Math.max(0,Math.min(M.length-1,Math.round((e-t)/(n-t)*(M.length-1))))},M}function o(){s.forEach(function(t){t.length=0}),m.forEach(function(t){t.age>f&&(e.randomize(t).age=0);var n=t.x,i=t.y,o=e(n,i),a=o[2];if(null===a)t.age=f;else{var h=n+o[0],l=i+o[1];null!==e(h,l)[2]?(t.xt=h,t.yt=l,s[r.indexFor(a)].push(t)):(t.x=h,t.y=l)}t.age+=1})}function a(){var t="lighter";L.globalCompositeOperation="destination-in",L.fillRect(n.x,n.y,n.width,n.height),L.globalCompositeOperation=t,L.globalAlpha=.9,s.forEach(function(t,n){t.length>0&&(L.beginPath(),L.strokeStyle=r[n],t.forEach(function(t){L.moveTo(t.x,t.y),L.lineTo(t.xt,t.yt),t.x=t.xt,t.y=t.yt}),L.stroke())})}var r=i(u,d),s=r.map(function(){return[]}),h=Math.round(n.width*n.height*_);R()&&(h*=p);for(var l="rgba(0, 0, 0, 0.97)",m=[],v=0;v<h;v++)m.push(e.randomize({age:Math.floor(Math.random()*f)+0}));var L=t.canvas.getContext("2d");L.lineWidth=y,L.fillStyle=l,L.globalAlpha=.6;var w=Date.now();!function x(){c=requestAnimationFrame(x);var t=Date.now(),n=t-w;n>g&&(w=t-n%g,o(),a())}()},q=function(t,n,e,i){var o={south:V(i[0][1]),north:V(i[1][1]),east:V(i[1][0]),west:V(i[0][0]),width:n,height:e};Z(),P(x,function(i){H(i,F(t,n,e),o,function(t,n){K.field=n,B(t,n)})})},Z=function(){K.field&&K.field.release(),c&&cancelAnimationFrame(c)},K={params:t,start:q,stop:Z,createField:N,interpolatePoint:z,setData:C};return K};window.cancelAnimationFrame||(window.cancelAnimationFrame=function(t){clearTimeout(t)});