From b2a09f8d61dc1633b530c9878d036d18ec27a6c0 Mon Sep 17 00:00:00 2001 From: Dan Wild Date: Fri, 22 Nov 2019 12:43:08 +1100 Subject: [PATCH] update dist with #42, npm releasing 1.5.0 --- dist/leaflet-velocity.js | 70 +++++++++++++----------------------- dist/leaflet-velocity.min.js | 2 +- package-lock.json | 2 +- package.json | 4 +-- 4 files changed, 28 insertions(+), 50 deletions(-) diff --git a/dist/leaflet-velocity.js b/dist/leaflet-velocity.js index 6bc7d26..496eb09 100644 --- a/dist/leaflet-velocity.js +++ b/dist/leaflet-velocity.js @@ -99,13 +99,6 @@ L.CanvasLayer = (L.Layer ? L.Layer : L.Class).extend({ map.addLayer(this); return this; }, - // -------------------------------------------------------------------------------- - LatLonToMercator: function LatLonToMercator(latlon) { - return { - x: latlon.lng * 6378137 * Math.PI / 180, - y: Math.log(Math.tan((90 + latlon.lat) * Math.PI / 360)) * 6378137 - }; - }, //------------------------------------------------------------------------------ drawLayer: function drawLayer() { // -- todo make the viewInfo properties flat objects. @@ -115,8 +108,10 @@ L.CanvasLayer = (L.Layer ? L.Layer : L.Class).extend({ var zoom = this._map.getZoom(); - var center = this.LatLonToMercator(this._map.getCenter()); - var corner = this.LatLonToMercator(this._map.containerPointToLatLng(this._map.getSize())); + var center = this._map.options.crs.project(this._map.getCenter()); + + var corner = this._map.options.crs.project(this._map.containerPointToLatLng(this._map.getSize())); + var del = this._delegate || this; del.onDrawLayer && del.onDrawLayer({ layer: this, @@ -352,7 +347,8 @@ L.VelocityLayer = (L.Layer ? L.Layer : L.Class).extend({ _initWindy: function _initWindy(self) { // windy object, copy options var options = Object.assign({ - canvas: self._canvasLayer._canvas + canvas: self._canvasLayer._canvas, + map: this._map }, self.options); this._windy = new Windy(options); // prepare context global var, start drawing @@ -631,23 +627,26 @@ var Windy = function Windy(params) { */ - var distort = function distort(projection, λ, φ, x, y, scale, wind, windy) { + var distort = function distort(projection, λ, φ, x, y, scale, wind) { var u = wind[0] * scale; var v = wind[1] * scale; - var d = distortion(projection, λ, φ, x, y, windy); // Scale distortion vectors by u and v, then add. + var d = distortion(projection, λ, φ, x, y); // Scale distortion vectors by u and v, then add. wind[0] = d[0] * u + d[2] * v; wind[1] = d[1] * u + d[3] * v; return wind; }; - var distortion = function distortion(projection, λ, φ, x, y, windy) { - var τ = 2 * Math.PI; - var H = Math.pow(10, -5.2); + var distortion = function distortion(projection, λ, φ, x, y) { + var τ = 2 * Math.PI; // var H = Math.pow(10, -5.2); // 0.00000630957344480193 + // var H = 0.0000360; // 0.0000360°φ ~= 4m (from https://github.com/cambecc/earth/blob/master/public/libs/earth/1.0.0/micro.js#L13) + + var H = 5; // ToDo: Why does this work? + var hλ = λ < 0 ? H : -H; var hφ = φ < 0 ? H : -H; - var pλ = project(φ, λ + hλ, windy); - var pφ = project(φ + hφ, λ, windy); // Meridian scale factor (see Snyder, equation 4-3), where R = 1. This handles issue where length of 1º λ + var pλ = project(φ, λ + hλ); + var pφ = project(φ + hφ, λ); // Meridian scale factor (see Snyder, equation 4-3), where R = 1. This handles issue where length of 1º λ // changes depending on φ. Without this, there is a pinching effect at the poles. var k = Math.cos(φ / 360 * τ); @@ -710,40 +709,19 @@ var Windy = function Windy(params) { return deg / 180 * Math.PI; }; - var rad2deg = function rad2deg(ang) { - return ang / (Math.PI / 180.0); - }; - var invert = function invert(x, y, windy) { - var mapLonDelta = windy.east - windy.west; - var worldMapRadius = windy.width / rad2deg(mapLonDelta) * 360 / (2 * Math.PI); - var mapOffsetY = worldMapRadius / 2 * Math.log((1 + Math.sin(windy.south)) / (1 - Math.sin(windy.south))); - var equatorY = windy.height + mapOffsetY; - var a = (equatorY - y) / worldMapRadius; - var lat = 180 / Math.PI * (2 * Math.atan(Math.exp(a)) - Math.PI / 2); - var lon = rad2deg(windy.west) + x / windy.width * rad2deg(mapLonDelta); - return [lon, lat]; - }; - - var mercY = function mercY(lat) { - return Math.log(Math.tan(lat / 2 + Math.PI / 4)); + var latlon = params.map.containerPointToLatLng(L.point(x, y)); + return [latlon.lng, latlon.lat]; }; var project = function project(lat, lon, windy) { - // both in radians, use deg2rad if neccessary - var ymin = mercY(windy.south); - var ymax = mercY(windy.north); - var xFactor = windy.width / (windy.east - windy.west); - var yFactor = windy.height / (ymax - ymin); - var y = mercY(deg2rad(lat)); - var x = (deg2rad(lon) - windy.west) * xFactor; - var y = (ymax - y) * yFactor; // y points south - - return [x, y]; + var xy = params.map.latLngToContainerPoint(L.latLng(lat, lon)); + return [xy.x, xy.y]; }; var interpolateField = function interpolateField(grid, bounds, extent, callback) { - var projection = {}; + var projection = {}; // map.crs used instead + var mapArea = (extent.south - extent.north) * (extent.west - extent.east); var velocityScale = VELOCITY_SCALE * Math.pow(mapArea, 0.4); var columns = []; @@ -753,7 +731,7 @@ var Windy = function Windy(params) { var column = []; for (var y = bounds.y; y <= bounds.yMax; y += 2) { - var coord = invert(x, y, extent); + var coord = invert(x, y); if (coord) { var λ = coord[0], @@ -763,7 +741,7 @@ var Windy = function Windy(params) { var wind = grid.interpolate(λ, φ); if (wind) { - wind = distort(projection, λ, φ, x, y, velocityScale, wind, extent); + wind = distort(projection, λ, φ, x, y, velocityScale, wind); column[y + 1] = column[y] = wind; } } diff --git a/dist/leaflet-velocity.min.js b/dist/leaflet-velocity.min.js index 32d96dc..22b03e0 100644 --- a/dist/leaflet-velocity.min.js +++ b/dist/leaflet-velocity.min.js @@ -1 +1 @@ -"use strict";L.DomUtil.setTransform||(L.DomUtil.setTransform=function(t,e,n){var i=e||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)")+(n?" scale("+n+")":"")}),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){console.log("canvas onAdd",this),this._map=t,this._canvas=L.DomUtil.create("canvas","leaflet-layer"),this.tiles={};var e=this._map.getSize();this._canvas.width=e.x,this._canvas.height=e.y;var n=this._map.options.zoomAnimation&&L.Browser.any3d;L.DomUtil.addClass(this._canvas,"leaflet-zoom-"+(n?"animated":"hide")),this.options.pane.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 e=this._delegate||this;e.onLayerWillUnmount&&e.onLayerWillUnmount(),this.options.pane.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(),e=this._map.getBounds(),n=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:e,size:t,zoom:n,center:i,corner:o}),this._frame=null},_setTransform:function(t,e,n){var i=e||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)")+(n?" scale("+n+")":"")},_animateZoom:function(t){var e=this._map.getZoomScale(t.zoom),n=L.Layer?this._map._latLngToNewLayerPoint(this._map.getBounds().getNorthWest(),t.zoom,t.center):this._map._getCenterOffset(t.center)._multiplyBy(-e).subtract(this._map._getMapPanePos());L.DomUtil.setTransform(this._canvas,n,e)}}),L.canvasLayer=function(t){return new L.CanvasLayer(t)},L.Control.Velocity=L.Control.extend({options:{position:"bottomleft",emptyString:"Unavailable",angleConvention:"bearingCCW",speedUnit:"m/s",onAdd:null,onRemove:null},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.options.leafletVelocity.options.onAdd&&this.options.leafletVelocity.options.onAdd(),this._container},onRemove:function(t){t.off("mousemove",this._onMouseMove,this),this.options.leafletVelocity.options.onRemove&&this.options.leafletVelocity.options.onRemove()},vectorToSpeed:function(t,e,n){var i=Math.sqrt(Math.pow(t,2)+Math.pow(e,2));return"k/h"===n?this.meterSec2kilometerHour(i):"kt"===n?this.meterSec2Knots(i):i},vectorToDegrees:function(t,e,n){n.endsWith("CCW")&&(e=0"+this.options.velocityType+" Direction: "+this.vectorToDegrees(n[0],n[1],this.options.angleConvention).toFixed(2)+"°, "+this.options.velocityType+" Speed: "+this.vectorToSpeed(n[0],n[1],this.options.speedUnit).toFixed(2)+this.options.speedUnit:this.options.emptyString,this._container.innerHTML=i}}),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._paneName=this.options.paneName||"overlayPane";var e=t._panes.overlayPane;t.getPane&&(e=(e=t.getPane(this._paneName))||t.createPane(this._paneName)),this._canvasLayer=L.canvasLayer({pane:e}).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")},setOptions:function(t){this.options=Object.assign(this.options,t),t.hasOwnProperty("displayOptions")&&(this.options.displayOptions=Object.assign(this.options.displayOptions,t.displayOptions),this._initMouseHandler(!0)),t.hasOwnProperty("data")&&(this.options.data=t.data),this._windy&&(this._windy.setOptions(t),t.hasOwnProperty("data")&&this._windy.setData(t.data),this._clearAndRestart()),this.fire("load")},onDrawLayer:function(t,e){var n=this;this._windy?this.options.data&&(this._timer&&clearTimeout(n._timer),this._timer=setTimeout(function(){n._startWindy()},750)):this._initWindy(this)},_startWindy:function(){var t=this._map.getBounds(),e=this._map.getSize();this._windy.start([[0,0],[e.x,e.y]],e.x,e.y,[[t._southWest.lng,t._southWest.lat],[t._northEast.lng,t._northEast.lat]])},_initWindy:function(t){var e=Object.assign({canvas:t._canvasLayer._canvas},t.options);this._windy=new Windy(e),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(!1)},_initMouseHandler:function(t){if(t&&(this._map.removeControl(this._mouseControl),this._mouseControl=!1),!this._mouseControl&&this.options.displayValues){var e=this.options.displayOptions||{};(e.leafletVelocity=this)._mouseControl=L.control.velocity(e).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(d){function o(t,e,n,i,o,a){var r=1-t,s=1-e,h=r*s,l=t*s,c=r*e,d=t*e,u=n[0]*h+i[0]*l+o[0]*c+a[0]*d,p=n[1]*h+i[1]*l+o[1]*c+a[1]*d;return[u,p,Math.sqrt(u*u+p*p)]}function h(t){var e=null,n=null;return t.forEach(function(t){switch(t.header.parameterCategory+","+t.header.parameterNumber){case"1,2":case"2,2":e=t;break;case"1,3":case"2,3":n=t;break;default:t}}),function(t,e){var n=t.data,i=e.data;return{header:t.header,data:function(t){return[n[t],i[t]]},interpolate:o}}(e,n)}function a(i,o,t){function a(t,e){var n=i[Math.round(t)];return n&&n[Math.round(e)]||R}a.release=function(){i=[]},a.randomize=function(t){for(var e,n,i=0;null===a(e=Math.round(Math.floor(Math.random()*o.width)+o.x),n=Math.round(Math.floor(Math.random()*o.height)+o.y))[2]&&i++<30;);return t.x=e,t.y=n,t},t(o,a)}function l(t){return t/180*Math.PI}function S(t){return t/(Math.PI/180)}function c(t){return Math.log(Math.tan(t/2+Math.PI/4))}function r(i,s){var e,n,h=(e=M,n=x,O.indexFor=function(t){return Math.max(0,Math.min(O.length-1,Math.round((t-e)/(n-e)*(O.length-1))))},O),l=h.map(function(){return[]}),t=Math.round(i.width*i.height*b);/android|blackberry|iemobile|ipad|iphone|ipod|opera mini|webos/i.test(navigator.userAgent)&&(t*=T);for(var o=[],a=0;aP&&(s.randomize(t).age=0);var e=t.x,n=t.y,i=s(e,n),o=i[2];if(null===o)t.age=P;else{var a=e+i[0],r=n+i[1];null!==s(a,r)[2]?(t.xt=a,t.yt=r,l[h.indexFor(o)].push(t)):(t.x=a,t.y=r)}t.age+=1}),r.globalCompositeOperation="destination-in",r.fillRect(i.x,i.y,i.width,i.height),r.globalCompositeOperation="lighter",r.globalAlpha=.9,l.forEach(function(t,e){0"+this.options.velocityType+" Direction: "+this.vectorToDegrees(n[0],n[1],this.options.angleConvention).toFixed(2)+"°, "+this.options.velocityType+" Speed: "+this.vectorToSpeed(n[0],n[1],this.options.speedUnit).toFixed(2)+this.options.speedUnit:this.options.emptyString,this._container.innerHTML=i}}),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._paneName=this.options.paneName||"overlayPane";var e=t._panes.overlayPane;t.getPane&&(e=(e=t.getPane(this._paneName))||t.createPane(this._paneName)),this._canvasLayer=L.canvasLayer({pane:e}).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")},setOptions:function(t){this.options=Object.assign(this.options,t),t.hasOwnProperty("displayOptions")&&(this.options.displayOptions=Object.assign(this.options.displayOptions,t.displayOptions),this._initMouseHandler(!0)),t.hasOwnProperty("data")&&(this.options.data=t.data),this._windy&&(this._windy.setOptions(t),t.hasOwnProperty("data")&&this._windy.setData(t.data),this._clearAndRestart()),this.fire("load")},onDrawLayer:function(t,e){var n=this;this._windy?this.options.data&&(this._timer&&clearTimeout(n._timer),this._timer=setTimeout(function(){n._startWindy()},750)):this._initWindy(this)},_startWindy:function(){var t=this._map.getBounds(),e=this._map.getSize();this._windy.start([[0,0],[e.x,e.y]],e.x,e.y,[[t._southWest.lng,t._southWest.lat],[t._northEast.lng,t._northEast.lat]])},_initWindy:function(t){var e=Object.assign({canvas:t._canvasLayer._canvas,map:this._map},t.options);this._windy=new Windy(e),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(!1)},_initMouseHandler:function(t){if(t&&(this._map.removeControl(this._mouseControl),this._mouseControl=!1),!this._mouseControl&&this.options.displayValues){var e=this.options.displayOptions||{};(e.leafletVelocity=this)._mouseControl=L.control.velocity(e).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(P){function o(t,e,n,i,o,a){var r=1-t,s=1-e,l=r*s,h=t*s,c=r*e,d=t*e,p=n[0]*l+i[0]*h+o[0]*c+a[0]*d,u=n[1]*l+i[1]*h+o[1]*c+a[1]*d;return[p,u,Math.sqrt(p*p+u*u)]}function l(t){var e=null,n=null;return t.forEach(function(t){switch(t.header.parameterCategory+","+t.header.parameterNumber){case"1,2":case"2,2":e=t;break;case"1,3":case"2,3":n=t;break;default:t}}),function(t,e){var n=t.data,i=e.data;return{header:t.header,data:function(t){return[n[t],i[t]]},interpolate:o}}(e,n)}function a(i,o,t){function a(t,e){var n=i[Math.round(t)];return n&&n[Math.round(e)]||R}a.release=function(){i=[]},a.randomize=function(t){for(var e,n,i=0;null===a(e=Math.round(Math.floor(Math.random()*o.width)+o.x),n=Math.round(Math.floor(Math.random()*o.height)+o.y))[2]&&i++<30;);return t.x=e,t.y=n,t},t(o,a)}function r(t){return t/180*Math.PI}function s(i,s){var e,n,l=(e=g,n=w,O.indexFor=function(t){return Math.max(0,Math.min(O.length-1,Math.round((t-e)/(n-e)*(O.length-1))))},O),h=l.map(function(){return[]}),t=Math.round(i.width*i.height*C);/android|blackberry|iemobile|ipad|iphone|ipod|opera mini|webos/i.test(navigator.userAgent)&&(t*=b);for(var o=[],a=0;aM&&(s.randomize(t).age=0);var e=t.x,n=t.y,i=s(e,n),o=i[2];if(null===o)t.age=M;else{var a=e+i[0],r=n+i[1];null!==s(a,r)[2]?(t.xt=a,t.yt=r,h[l.indexFor(o)].push(t)):(t.x=a,t.y=r)}t.age+=1}),r.globalCompositeOperation="destination-in",r.fillRect(i.x,i.y,i.width,i.height),r.globalCompositeOperation="lighter",r.globalAlpha=.9,h.forEach(function(t,e){0