Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions folium/templates/leaflet_heat.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;t<i;t++){a=this._map.latLngToContainerPoint(this._latlngs[t]),e=Math.floor((a.x-f)/m)+2,n=Math.floor((a.y-g)/m)+2;var p=void 0!==this._latlngs[t].alt?this._latlngs[t].alt:void 0!==this._latlngs[t][2]?+this._latlngs[t][2]:1;c[n]=c[n]||[],(s=c[n][e])?(s[0]=(s[0]*s[2]+a.x*p)/(s[2]+p),s[1]=(s[1]*s[2]+a.y*p)/(s[2]+p),s[2]+=p):(s=c[n][e]=[a.x,a.y,p]).p=a,s[2]>this._max&&(this._max=s[2])}for(this._heat.max(this._max),t=0,i=c.length;t<i;t++)if(c[t])for(h=0,o=c[t].length;h<o;h++)(s=c[t][h])&&l.contains(s.p)&&r.push([Math.round(s[0]),Math.round(s[1]),Math.min(s[2],this._max)]);this._heat.data(r).draw(this.options.minOpacity),this._frame=null}},_animateZoom:function(t){var i=this._map.getZoomScale(t.zoom),a=this._map._getCenterOffset(t.center)._multiplyBy(-i).subtract(this._map._getMapPanePos());L.DomUtil.setTransform?L.DomUtil.setTransform(this._canvas,a,i):this._canvas.style[L.DomUtil.TRANSFORM]=L.DomUtil.getTranslateString(a)+" scale("+i+")"}}),L.heatLayer=function(t,i){return new L.HeatLayer(t,i)};
/*
folium patch (#1430): faster redraws for large datasets.
The original _redraw calls map.latLngToContainerPoint for every point on every
pan and zoom, re-running the map projection each time. This version projects
each point once, caches it, and on each redraw applies only the zoom scaling,
using the same arithmetic as Leaflet so the drawn heatmap is unchanged.
The grid of cells is kept in Maps instead of sparse arrays, and the final
pass only visits the on-screen part of it.
Maps with a CRS that overrides latLngToPoint keep using the original _redraw.
*/
(function () {
var originalRedraw = L.HeatLayer.prototype._redraw;

L.HeatLayer.include({
_getProjectedLatLngs: function (crs) {
var latlngs = this._latlngs,
cache = this._projected;
if (!cache || cache.latlngs !== latlngs || cache.crs !== crs || cache.length > 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;
}
});
})();
Loading