UNPKG

wind-layer

Version:

a openlayers ol bmap amap maptalks extension to windjs

2 lines (1 loc) 10.4 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("maptalks")):"function"==typeof define&&define.amd?define(["maptalks"],e):(t=t||self).windLayer=e(t.maptalks)}(this,function(a){"use strict";function o(u){void 0===u&&(u={}),this.params=u;var d=this;function e(t){t.projection||(t.projection="EPSG:4326"),d.MIN_VELOCITY_INTENSITY=t.minVelocity||0,d.MAX_VELOCITY_INTENSITY=t.maxVelocity||10,d.VELOCITY_SCALE=(t.velocityScale||.005)*(Math.pow(window.devicePixelRatio,1/3)||1),d.MAX_PARTICLE_AGE=t.particleAge||90,d.PARTICLE_LINE_WIDTH=t.lineWidth||1,d.PARTICLE_MULTIPLIER=t.particleMultiplier||1/300,d.PARTICLE_REDUCTION=Math.pow(window.devicePixelRatio,1/3)||1.6,d.FRAME_RATE=t.frameRate||16,d.COLOR_SCALE=t.colorScale||a,d.DEVICEPIXELRATIO=t.devicePixelRatio||1}d.canvas=u.canvas;var a=["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)"];function i(t,e,a,n,i,o){var r=1-t,c=1-e,s=r*c,p=t*c,l=r*e,u=t*e,h=a[0]*s+n[0]*p+i[0]*l+o[0]*u,d=a[1]*s+n[1]*p+i[1]*l+o[1]*u;return[h,d,Math.sqrt(h*h+d*d)]}function s(t){var e=null,a=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":a=t}}),function(t,e){var a=t.data,n=e.data;return{header:t.header,data:function(t){return[a[t],n[t]]},interpolate:i}}(e,a)}function o(n,i,t){function o(t,e){var a=n[Math.round(t)];return a&&a[Math.round(e)]||r}o.release=function(){n=[]},o.randomize=function(t){for(var e,a,n=0;null===o(e=Math.round(Math.floor(Math.random()*i.width)+i.x),a=Math.round(Math.floor(Math.random()*i.height)+i.y))[2]&&n++<30;);return t.x=e,t.y=a,t},t(i,o)}function p(t){return t/180*Math.PI}function l(t){return t/(Math.PI/180)}e(u),window.FRAME_TIME=1e3/d.FRAME_RATE;var f,m,h,y,g,M,w,v,E,R,r=[NaN,NaN,null],c=d.params.data,_=function(t,e){if(!m)return null;var a,n=A(t-y,360)/M,i=(g-e)/w,o=Math.floor(n),r=o+1,c=Math.floor(i),s=c+1;if(a=m[c]){var p=a[o],l=a[r];if(I(p)&&I(l)&&(a=m[s])){var u=a[o],h=a[r];if(I(u)&&I(h))return f.interpolate(n-o,i-c,p,l,u,h)}}return null},I=function(t){return null!=t},A=function(t,e){return t-e*Math.floor(t/e)},x=function(t,e,a,n,i,o){var r=2*Math.PI,c="EPSG:4326"===d.params.projection?5:Math.pow(10,-5.2),s=e<0?c:-c,p=a<0?c:-c,l=S(a,e+s,o),u=S(a+p,e,o),h=Math.cos(a/360*r);return[(l[0]-n)/s/h,(l[1]-i)/s/h,(u[0]-n)/p,(u[1]-i)/p]};function T(t){return Math.log(Math.tan(t/2+Math.PI/4))}function P(n,c){var e,a,s=(e=d.MIN_VELOCITY_INTENSITY,a=d.MAX_VELOCITY_INTENSITY,d.COLOR_SCALE.indexFor=function(t){return Math.max(0,Math.min(d.COLOR_SCALE.length-1,Math.round((t-e)/(a-e)*(d.COLOR_SCALE.length-1))))},d.COLOR_SCALE),p=s.map(function(){return[]}),t=Math.round(n.width*n.height*d.PARTICLE_MULTIPLIER);/android|blackberry|iemobile|ipad|iphone|ipod|opera mini|webos/i.test(navigator.userAgent)&&(t*=d.PARTICLE_REDUCTION);for(var i=[],o=0;o<t;o++)i.push(c.randomize({age:Math.floor(Math.random()*d.MAX_PARTICLE_AGE)+0}));var r=d.canvas.getContext("2d");r.lineWidth=d.PARTICLE_LINE_WIDTH,r.fillStyle="rgba(0, 0, 0, 0.97)",r.globalAlpha=.6;var l=Date.now();!function t(){C=requestAnimationFrame(t);var e=Date.now(),a=e-l;a>FRAME_TIME&&(l=e-a%FRAME_TIME,p.forEach(function(t){t.length=0}),i.forEach(function(t){t.age>d.MAX_PARTICLE_AGE&&(c.randomize(t).age=0);var e=t.x,a=t.y,n=c(e,a),i=n[2];if(null===i)t.age=d.MAX_PARTICLE_AGE;else{var o=e+n[0],r=a+n[1];null!==c(o,r)[2]?(t.xt=o,t.yt=r,p[s.indexFor(i)].push(t)):(t.x=o,t.y=r)}t.age+=1}),r.globalCompositeOperation="destination-in",r.fillRect(n.x,n.y,d.canvas.width,d.canvas.height),r.globalCompositeOperation="lighter",r.globalAlpha=.9,p.forEach(function(t,e){0<t.length&&(r.beginPath(),r.strokeStyle=s[e],t.forEach(function(t){r.moveTo(t.x*d.DEVICEPIXELRATIO,t.y*d.DEVICEPIXELRATIO),r.lineTo(t.xt*d.DEVICEPIXELRATIO,t.yt*d.DEVICEPIXELRATIO),t.x=t.xt,t.y=t.yt}),r.stroke())}),u.onDraw&&u.onDraw())}()}R="EPSG:4326"===d.params.projection?function(t,e,a){var n=a.east-a.west,i=a.south-a.north,o=l(a.north)+e/a.height*l(i);return[l(a.west)+t/a.width*l(n),o]}:function(t,e,a){var n=a.east-a.west,i=a.width/l(n)*360/(2*Math.PI),o=i/2*Math.log((1+Math.sin(a.south))/(1-Math.sin(a.south))),r=(a.height+o-e)/i,c=180/Math.PI*(2*Math.atan(Math.exp(r))-Math.PI/2);return[l(a.west)+t/a.width*l(n),c]};var C,S=function(t,e,a){var n=T(a.south),i=T(a.north),o=a.width/(a.east-a.west),r=a.height/(i-n),c=T(p(t));return[(p(e)-a.west)*o,c=(i-c)*r]},L=function(e,a,n,t){var i={south:p(t[0][1]),north:p(t[1][1]),east:p(t[1][0]),west:p(t[0][0]),width:a,height:n};b(),function(t,e){var a=(f=s(t)).header;y=a.lo1,g=Math.max(a.la2,a.la1),M=a.dx,w=a.dy,v=a.nx,E=a.ny,(h=new Date(a.refTime)).setHours(h.getHours()+a.forecastTime),m=[];for(var n=0,i=360<=Math.floor(v*M),o=0;o<E;o++){for(var r=[],c=0;c<v;c++,n++)r[c]=f.data(n);i&&r.push(r[0]),m[o]=r}e({date:h,interpolate:_})}(c,function(t){!function(M,w,v,a){var E={},t=(v.south-v.north)*(v.west-v.east),_=d.VELOCITY_SCALE*Math.pow(t,.4),I=[],n=w.x;function i(t){for(var e,a,n,i,o,r,c,s,p,l,u,h=[],d=w.y;d<=w.yMax;d+=2){var f=R(t,d,v);if(f){var m=f[0],y=f[1];if(isFinite(m)){var g=M.interpolate(m,y);g&&(e=E,a=m,n=y,i=t,o=d,r=_,s=v,void 0,p=(c=g)[0]*r,l=c[1]*r,u=x(e,a,n,i,o,s),c[0]=u[0]*p+u[2]*l,c[1]=u[1]*p+u[3]*l,g=c,h[d+1]=h[d]=g)}}}I[t+1]=I[t]=h}!function t(){for(var e=Date.now();n<w.width;)if(i(n),n+=2,1e3<Date.now()-e)return void setTimeout(t,25);o(I,w,a)}()}(t,function(t,e,a){var n=t[0],i=t[1],o=Math.round(n[0]),r=Math.max(Math.floor(n[1],0),0);Math.min(Math.ceil(i[0],e),e-1);return{x:o,y:r,xMax:e,yMax:Math.min(Math.ceil(i[1],a),a-1),width:e,height:a}}(e,a,n),i,function(t,e){O.field=e,P(t,e)})})},b=function(){O.field&&O.field.release(),C&&cancelAnimationFrame(C)},O={params:d.params,start:L,stop:b,update:function(t,e,a,n,i){delete d.params.data,d.params.data=t,i&&L(e,a,n,i)},shift:function(t,e){var a=d.canvas,n=a.width,i=a.height,o=a.getContext("2d");if(t<n&&e<i){var r=function(t,e){return Math.max(0,Math.min(t,e))},c=o.getImageData(r(n,-t),r(i,-e),r(n,n-t),r(i,i-e));o.clearRect(0,0,n,i),o.putImageData(c,r(n,t),r(i,e));for(var s=0,p=particles.length;s<p;s++)particles[s].x+=t,particles[s].y+=e}},createField:o,interpolatePoint:_,setData:function(t){c=t},updateParams:function(t){d.params=t,e(d.params)},getParams:function(){return d.params},buildParams:e};return O}window.requestAnimationFrame=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(t){return window.setTimeout(t,1e3/window.FRAME_RATE)},window.cancelAnimationFrame||(window.cancelAnimationFrame=function(t){clearTimeout(t)});var i={minVelocity:0,maxVelocity:10,velocityScale:.05,particleAge:90,lineWidth:1,particleMultiplier:1/300,colorScale:void 0,animate:!0},t=function(e){function t(){e.apply(this,arguments)}return e&&(t.__proto__=e),((t.prototype=Object.create(e&&e.prototype)).constructor=t).prototype._initialize=function(){var t=this,e=this.getMap(),a=this.layer.getParams(),n=this.layer.getSpatialReference();n||(n=e.getSpatialReference());var i=n.getProjection();this._windy=new o(Object.assign({},{canvas:this.canvas,data:this.layer.getData(),projection:i.code||"EPSG:4326",onDraw:function(){t.setCanvasUpdated()}},a))},t.prototype.draw=function(){var t,e=this._getWindExtents();this.prepareCanvas(),this._windy||this._initialize(),this._windy&&(t=this._windy).start.apply(t,e)},t.prototype._redraw=function(){this.prepareRender(),this.draw()},t.prototype.drawOnInteracting=function(){},t.prototype._getWindExtents=function(){var t=this.getMap();if(!t)return null;var e=t.getExtent(),a=e.xmin,n=0<e.xmax?e.xmax:360+e.xmax,i=e.ymin,o=e.ymax;return[[[0,0],[t.width,t.height]],t.width,t.height,[[a,i],[n,o]]]},t.prototype._updateParams=function(){if(this._windy){var t=this.layer.getParams();this._windy.updateParams(t),this.draw()}},t.prototype.needToRedraw=function(){var t=this.getMap();return!(t.isZooming()&&!t.getPitch())&&e.prototype.needToRedraw.call(this)},t.prototype.onZoomStart=function(){e.prototype.onZoomStart.apply(this,arguments)},t.prototype.onZoomEnd=function(){e.prototype.onZoomEnd.apply(this,arguments)},t.prototype.onDragRotateStart=function(){},t.prototype.onMoveStart=function(){},t.prototype.remove=function(){this._windy&&(this._windy.stop(),delete this._windy),e.prototype.remove.call(this)},t}(a.renderer.CanvasRenderer),e=function(n){function e(t,e,a){n.call(this,t,Object.assign(i,a)),this._data=e}return n&&(e.__proto__=n),((e.prototype=Object.create(n&&n.prototype)).constructor=e).prototype.getData=function(){return this._data},e.prototype.setData=function(t){return this._data=t,this.redraw(),this},e.prototype.updateParams=function(t){var e=this._getRenderer();if(this.options=Object.assign(this.options,t),e){var a=this.options,n=a.minVelocity,i=a.maxVelocity,o=a.velocityScale,r=a.particleAge,c=a.lineWidth,s=a.particleMultiplier,p=a.colorScale;e._updateParams({minVelocity:n,maxVelocity:i,velocityScale:o,particleAge:r,lineWidth:c,particleMultiplier:s,colorScale:p})}return this},e.prototype.getParams=function(){var t=this.options;return{minVelocity:t.minVelocity,maxVelocity:t.maxVelocity,velocityScale:t.velocityScale,particleAge:t.particleAge,lineWidth:t.lineWidth,particleMultiplier:t.particleMultiplier,colorScale:t.colorScale}},e.prototype.onResize=function(){},e.prototype.onZoomStart=function(){},e.prototype.onZooming=function(){},e.prototype.onZoomEnd=function(){},e.prototype.onMoveStart=function(){},e.prototype.onMoving=function(){},e.prototype.onMoveEnd=function(){},e.prototype.redraw=function(){var t=this._getRenderer();return t&&t._redraw(),this},e.prototype.toJSON=function(){return{type:"MaptalksWindy",id:this.getId(),options:this.config(),data:this.getData()}},e.prototype.getSpatialReference=function(){var t=this.getMap();return!t||this.options.spatialReference&&!a.SpatialReference.equals(this.options.spatialReference,t.options.spatialReference)?(this._sr=this._sr||new a.SpatialReference(this.options.spatialReference),this._sr):t.getSpatialReference()},e.fromJSON=function(t){return t&&"MaptalksWindy"===t.type?new e(t.id,t.data,t.options):null},e}(a.Layer);return e.registerRenderer("canvas",t),e});