diff --git a/folium/templates/leaflet_heat.min.js b/folium/templates/leaflet_heat.min.js index 08bacdf775..02e7015eee 100644 --- a/folium/templates/leaflet_heat.min.js +++ b/folium/templates/leaflet_heat.min.js @@ -11,3 +11,114 @@ Leaflet.heat, a tiny and fast heatmap plugin for Leaflet. https://github.com/Leaflet/Leaflet.heat */L.HeatLayer=(L.Layer?L.Layer:L.Class).extend({initialize:function(t,i){this._latlngs=t,L.setOptions(this,i)},setLatLngs:function(t){return this._latlngs=t,this.redraw()},addLatLng:function(t){return this._latlngs.push(t),this.redraw()},setOptions:function(t){return L.setOptions(this,t),this._heat&&this._updateOptions(),this.redraw()},redraw:function(){return this._heat&&!this._frame&&this._map&&!this._map._animating&&(this._frame=L.Util.requestAnimFrame(this._redraw,this)),this},onAdd:function(t){this._map=t,this._canvas||this._initCanvas(),this.options.pane?this.getPane().appendChild(this._canvas):t._panes.overlayPane.appendChild(this._canvas),t.on("moveend",this._reset,this),t.options.zoomAnimation&&L.Browser.any3d&&t.on("zoomanim",this._animateZoom,this),this._reset()},onRemove:function(t){this.options.pane?this.getPane().removeChild(this._canvas):t.getPanes().overlayPane.removeChild(this._canvas),t.off("moveend",this._reset,this),t.options.zoomAnimation&&t.off("zoomanim",this._animateZoom,this)},addTo:function(t){return t.addLayer(this),this},_initCanvas:function(){var t=this._canvas=L.DomUtil.create("canvas","leaflet-heatmap-layer leaflet-layer"),i=L.DomUtil.testProp(["transformOrigin","WebkitTransformOrigin","msTransformOrigin"]);t.style[i]="50% 50%";var a=this._map.getSize();t.width=a.x,t.height=a.y;var s=this._map.options.zoomAnimation&&L.Browser.any3d;L.DomUtil.addClass(t,"leaflet-zoom-"+(s?"animated":"hide")),this._heat=simpleheat(t),this._updateOptions()},_updateOptions:function(){this._heat.radius(this.options.radius||this._heat.defaultRadius,this.options.blur),this.options.gradient&&this._heat.gradient(this.options.gradient)},_reset:function(){var t=this._map.containerPointToLayerPoint([0,0]);L.DomUtil.setPosition(this._canvas,t);var i=this._map.getSize();this._heat._width!==i.x&&(this._canvas.width=this._heat._width=i.x),this._heat._height!==i.y&&(this._canvas.height=this._heat._height=i.y),this._redraw()},_redraw:function(){if(this._map){var t,i,a,s,e,n,h,o,r=[],_=this._heat._r,d=this._map.getSize(),l=new L.Bounds(L.point([-_,-_]),d.add([_,_])),m=_/2,c=[],u=this._map._getMapPanePos(),f=u.x%m,g=u.y%m;for(this._max=1,t=0,i=this._latlngs.length;tthis._max&&(this._max=s[2])}for(this._heat.max(this._max),t=0,i=c.length;t latlngs.length) { + cache = this._projected = {latlngs: latlngs, crs: crs, length: 0, x: [], y: []}; + } + for (var i = cache.length; i < latlngs.length; i++) { + var p = crs.projection.project(L.latLng(latlngs[i])); + cache.x[i] = p.x; + cache.y[i] = p.y; + } + cache.length = latlngs.length; + return cache; + }, + + _redraw: function () { + if (!this._map) { + return; + } + var map = this._map, + crs = map.options.crs; + if (crs.latLngToPoint !== L.CRS.latLngToPoint || !crs.projection || !crs.transformation) { + return originalRedraw.call(this); + } + + var data = [], + r = this._heat._r, + size = map.getSize(), + bounds = new L.Bounds(L.point([-r, -r]), size.add([r, r])), + cellSize = r / 2, + grid = new Map(), + panePos = map._getMapPanePos(), + offsetX = panePos.x % cellSize, + offsetY = panePos.y % cellSize, + origin = map.getPixelOrigin(), + scale = crs.scale(map.getZoom()), + tr = crs.transformation, + latlngs = this._latlngs, + projected = this._getProjectedLatLngs(crs), + i, len, x, y, px, py, k, ll, row, cell; + + this._max = 1; + for (i = 0, len = latlngs.length; i < len; i++) { + // Same steps as map.latLngToContainerPoint: transform, round, + // subtract the pixel origin, add the map pane position. + px = Math.round(scale * (tr._a * projected.x[i] + tr._b)) - origin.x + panePos.x; + py = Math.round(scale * (tr._c * projected.y[i] + tr._d)) - origin.y + panePos.y; + x = Math.floor((px - offsetX) / cellSize) + 2; + y = Math.floor((py - offsetY) / cellSize) + 2; + ll = latlngs[i]; + k = ll.alt !== undefined ? ll.alt : ll[2] !== undefined ? +ll[2] : 1; + row = grid.get(y); + if (!row) { + grid.set(y, row = new Map()); + } + cell = row.get(x); + if (cell) { + cell[0] = (cell[0] * cell[2] + px * k) / (cell[2] + k); + cell[1] = (cell[1] * cell[2] + py * k) / (cell[2] + k); + cell[2] += k; + } else { + row.set(x, cell = [px, py, k]); + cell.px = px; + cell.py = py; + } + if (cell[2] > this._max) { + this._max = cell[2]; + } + } + + this._heat.max(this._max); + + // A cell is drawn when its first point lies inside bounds, so only + // the grid rows and columns that bounds covers can contain one. + var minX = bounds.min.x, maxX = bounds.max.x, + minY = bounds.min.y, maxY = bounds.max.y, + firstRow = Math.max(0, Math.floor((minY - offsetY) / cellSize) + 2), + lastRow = Math.floor((maxY - offsetY) / cellSize) + 2, + firstCol = Math.max(0, Math.floor((minX - offsetX) / cellSize) + 2), + lastCol = Math.floor((maxX - offsetX) / cellSize) + 2; + for (y = firstRow; y <= lastRow; y++) { + row = grid.get(y); + if (!row) { + continue; + } + for (x = firstCol; x <= lastCol; x++) { + cell = row.get(x); + if (cell && cell.px >= minX && cell.px <= maxX && cell.py >= minY && cell.py <= maxY) { + data.push([Math.round(cell[0]), Math.round(cell[1]), Math.min(cell[2], this._max)]); + } + } + } + this._heat.data(data).draw(this.options.minOpacity); + this._frame = null; + } + }); +})();