Guest User

Untitled

a guest
Mar 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. ymaps.modules.define("Heatmap",["option.Manager","Monitor","Layer","heatmap.component.dataConverter","heatmap.component.TileUrlsGenerator"],function(a,b,c,d,e,f){var g=function(a,c){this._unprocessedPoints=[],a&&this.setData(a),this.options=new b(c)};g.prototype.getData=function(){return this._data||null},g.prototype.setData=function(a){this._data=a;var b=e.convert(a);return this._tileUrlsGenerator?(this._tileUrlsGenerator.setPoints(b),this._refresh()):this._unprocessedPoints=b,this},g.prototype.getMap=function(){return this._map},g.prototype.setMap=function(a){return this._map!=a&&(this._layer&&(this._map.layers.remove(this._layer),this._destroyLayer()),this._map=a,a&&(this._setupLayer(),this._map.layers.add(this._layer))),this},g.prototype.destroy=function(){this._data=null,this.setMap(null)},g.prototype._refresh=function(){return this._layer&&this._layer.update(),this},g.prototype._setupLayer=function(){this._setupTileUrlsGenerator();var a=this._tileUrlsGenerator.getTileUrl.bind(this._tileUrlsGenerator);return this._layer=new d(a,{tileTransparent:!0}),this._setupOptionMonitor(),this._layer},g.prototype._destroyLayer=function(){this._destroyTileUrlsGenerator(),this._destroyOptionMonitor(),this._layer=null},g.prototype._setupTileUrlsGenerator=function(){return this._tileUrlsGenerator=new f(this._map.options.get("projection"),this._unprocessedPoints),this._unprocessedPoints=null,this._tileUrlsGenerator.options.setParent(this.options),this._tileUrlsGenerator},g.prototype._destroyTileUrlsGenerator=function(){this._unprocessedPoints=this._tileUrlsGenerator.getPoints(),this._tileUrlsGenerator.destroy(),this._tileUrlsGenerator=null},g.prototype._setupOptionMonitor=function(){return this._optionMonitor=new c(this.options),this._optionMonitor.add(["radius","dissipating","opacity","intensityOfMidpoint","gradient"],this._refresh,this)},g.prototype._destroyOptionMonitor=function(){this._optionMonitor.removeAll(),this._optionMonitor={}},a(g)}),ymaps.modules.define("heatmap.component.dataConverter",[],function(a){var b={};b.convert=function(a){var b=[];if("string"==typeof object&&(a=JSON.parse(a)),this._isJsonFeatureCollection(a))for(var c=0,d=a.features.length;d>c;c++)b=b.concat(this.convert(a.features[c]));else if(this._isCoordinates(a))b.push(this._convertCoordinatesToPoint(a));else for(var e,f=[].concat(a),c=0,d=f.length;d>c;c++)if(e=f[c],this._isCoordinates(e))b.push(this._convertCoordinatesToPoint(e));else if(this._isJsonGeometry(e)&&"Point"==e.type)b.push(this._convertCoordinatesToPoint(e.coordinates));else if(this._isJsonFeature(e)&&"Point"==e.geometry.type)b.push(this._convertJsonFeatureToPoint(e));else if(this._isGeoObject(e)&&"Point"==e.geometry.getType())b.push(this._convertGeoObjectToPoint(e));else if(this._isCollection(e))for(var g,h=e.getIterator();(g=h.getNext())!=h.STOP_ITERATION;)b=b.concat(this.convert(g));return b},b._isJsonFeature=function(a){return"Feature"==a.type},b._convertJsonFeatureToPoint=function(a){var b=1;return a.properties&&a.properties.weight&&(b=a.properties.weight),{coordinates:a.geometry.coordinates,weight:b}},b._isJsonFeatureCollection=function(a){return"FeatureCollection"==a.type},b._isCoordinates=function(a){return"[object Array]"==Object.prototype.toString.call(a)&&"number"==typeof a[0]&&"number"==typeof a[1]},b._convertCoordinatesToPoint=function(a){return{coordinates:a,weight:1}},b._isJsonGeometry=function(a){return Boolean(a.type&&a.coordinates)},b._isGeoObject=function(a){return Boolean(a.geometry&&a.getOverlay)},b._convertGeoObjectToPoint=function(a){return{coordinates:a.geometry.getCoordinates(),weight:a.properties.get("weight")||1}},b._isCollection=function(a){return Boolean(a.getIterator)},a(b)}),ymaps.modules.define("heatmap.component.TileUrlsGenerator",["option.Manager","heatmap.component.Canvas"],function(a,b,c){function d(a){return Math.pow(a/10,1.1)}function e(a){var b=a.sort(f),c=b.length/2;return c!==Math.floor(c)?b[Math.floor(c)]:(b[c-1]+b[c])/2}function f(a,b){return a-b}var g=[256,256],h=function(a,d){this._projection=a,this._canvas=new c(g),this.options=new b({}),this._canvas.options.setParent(this.options),this.setPoints(d||[])};h.prototype.setPoints=function(a){this._points=[];for(var b=[],c=0,d=a.length;d>c;c++)this._points.push({coordinates:this._projection.toGlobalPixels(a[c].coordinates,0),weight:a[c].weight}),b.push(a[c].weight);return this._canvas.options.set("medianaOfWeights",e(b)),this},h.prototype.getPoints=function(){for(var a=[],b=0,c=this._points.length;c>b;b++)a.push({coordinates:this._projection.fromGlobalPixels(this._points[b].coordinates,0),weight:this._points[b].weight});return a},h.prototype.getTileUrl=function(a,b){var c=this._canvas.options.get("radiusFactor");if(this.options.get("dissipating")){var e=d(b);c!=e&&this._canvas.options.set("radiusFactor",e)}else c&&this._canvas.options.unset("radiusFactor");for(var f,h=Math.pow(2,b),i=[[a[0]*g[0]/h,a[1]*g[1]/h],[(a[0]+1)*g[0]/h,(a[1]+1)*g[1]/h]],j=this._canvas.getBrushRadius()/h,k=[],l=0,m=this._points.length;m>l;l++)f=this._points[l].coordinates,this._contains(i,f,j)&&k.push({coordinates:[(f[0]-i[0][0])*h,(f[1]-i[0][1])*h],weight:this._points[l].weight});return this._canvas.generateDataURLHeatmap(k)},h.prototype.destroy=function(){this._canvas.destroy(),this._canvas=null,this._projection=null},h.prototype._contains=function(a,b,c){return b[0]>=a[0][0]-c&&b[0]<=a[1][0]+c&&b[1]>=a[0][1]-c&&b[1]<=a[1][1]+c},a(h)}),ymaps.modules.define("heatmap.component.Canvas",["option.Manager","Monitor"],function(a,b,c){var d={radius:10,radiusFactor:1,opacity:.8,intensityOfMidpoint:.2,medianaOfWeights:1,gradient:{.1:"rgba(128, 255, 0, 0.7)",.2:"rgba(255, 255, 0, 0.8)",.7:"rgba(234, 72, 58, 0.9)",1:"rgba(162, 36, 25, 1)"}},e=function(a){this._canvas=document.createElement("canvas"),this._canvas.width=a[0],this._canvas.height=a[1],this._context=this._canvas.getContext("2d"),this.options=new b({}),this._setupDrawTools(),this._setupOptionMonitor()};e.prototype.getBrushRadius=function(){return this.options.get("radius",d.radius)*this.options.get("radiusFactor",d.radiusFactor)},e.prototype.generateDataURLHeatmap=function(a){return this._drawHeatmap(a||[]),this._canvas.toDataURL()},e.prototype.destroy=function(){this._destroyOptionMonitor(),this._destroyDrawTools()},e.prototype._setupOptionMonitor=function(){return this._optionMonitor=new c(this.options),this._optionMonitor.add(["radius","radiusFactor","gradient"],this._setupDrawTools,this)},e.prototype._destroyOptionMonitor=function(){this._optionMonitor.removeAll(),this._optionMonitor={}},e.prototype._setupDrawTools=function(){return this._brush=this._createBrush(),this._gradient=this._createGradient(),this},e.prototype._destroyDrawTools=function(){this._brush=null,this._gradient=null},e.prototype._createBrush=function(){var a=document.createElement("canvas"),b=a.getContext("2d"),c=this.getBrushRadius(),d=b.createRadialGradient(c,c,0,c,c,c);return a.width=2*c,a.height=2*c,d.addColorStop(0,"rgba(0,0,0,1)"),d.addColorStop(1,"rgba(0,0,0,0)"),b.fillStyle=d,b.fillRect(0,0,2*c,2*c),a},e.prototype._createGradient=function(){var a=document.createElement("canvas"),b=a.getContext("2d"),c=b.createLinearGradient(0,0,0,256);a.width=1,a.height=256;var e=this.options.get("gradient",d.gradient);for(var f in e)e.hasOwnProperty(f)&&c.addColorStop(f,e[f]);return b.fillStyle=c,b.fillRect(0,0,1,256),b.getImageData(0,0,1,256).data},e.prototype._drawHeatmap=function(a){var b=this._context,c=this.getBrushRadius(),e=this.options.get("intensityOfMidpoint",d.intensityOfMidpoint),f=this.options.get("medianaOfWeights",d.medianaOfWeights),g=e/f;b.clearRect(0,0,this._canvas.width,this._canvas.height);for(var h=0,i=a.length;i>h;h++)b.globalAlpha=Math.min(a[h].weight*g,1),b.drawImage(this._brush,a[h].coordinates[0]-c,a[h].coordinates[1]-c);var j=b.getImageData(0,0,this._canvas.width,this._canvas.height);return this._colorize(j.data),b.putImageData(j,0,0),this},e.prototype._colorize=function(a){for(var b,c=this.options.get("opacity",d.opacity),e=3,f=a.length;f>e;e+=4)a[e]&&(b=4*a[e],a[e-3]=this._gradient[b],a[e-2]=this._gradient[b+1],a[e-1]=this._gradient[b+2],a[e]=c*(this._gradient[b+3]||255))},a(e)});
Add Comment
Please, Sign In to add comment