Skip to content

Commit

Permalink
Merge pull request #1 from Harvinator/patch-1
Browse files Browse the repository at this point in the history
Leaflet#43 Patch to fix intensity/altitude issue (auto max)
  • Loading branch information
svancise authored Apr 21, 2017
2 parents 5885ec0 + dcf67f1 commit 5b283c9
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions src/HeatLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ L.HeatLayer = (L.Layer ? L.Layer : L.Class).extend({
if (this.options.gradient) {
this._heat.gradient(this.options.gradient);
}
if (this.options.max) {
this._heat.max(this.options.max);
}
},

_reset: function () {
Expand Down Expand Up @@ -135,52 +132,54 @@ L.HeatLayer = (L.Layer ? L.Layer : L.Class).extend({
bounds = new L.Bounds(
L.point([-r, -r]),
size.add([r, r])),

max = this.options.max === undefined ? 1 : this.options.max,
maxZoom = this.options.maxZoom === undefined ? this._map.getMaxZoom() : this.options.maxZoom,
v = 1 / Math.pow(2, Math.max(0, Math.min(maxZoom - this._map.getZoom(), 12))),
cellSize = r / 2,
grid = [],
panePos = this._map._getMapPanePos(),
offsetX = panePos.x % cellSize,
offsetY = panePos.y % cellSize,
i, len, p, cell, x, y, j, len2, k;
i, len, p, cell, x, y, j, len2;

this._max = 1;

// console.time('process');
for (i = 0, len = this._latlngs.length; i < len; i++) {
p = this._map.latLngToContainerPoint(this._latlngs[i]);
if (bounds.contains(p)) {
x = Math.floor((p.x - offsetX) / cellSize) + 2;
y = Math.floor((p.y - offsetY) / cellSize) + 2;

var alt =
this._latlngs[i].alt !== undefined ? this._latlngs[i].alt :
this._latlngs[i][2] !== undefined ? +this._latlngs[i][2] : 1;
k = alt * v;

grid[y] = grid[y] || [];
cell = grid[y][x];

if (!cell) {
grid[y][x] = [p.x, p.y, k];
x = Math.floor((p.x - offsetX) / cellSize) + 2;
y = Math.floor((p.y - offsetY) / cellSize) + 2;

var alt =
this._latlngs[i].alt !== undefined ? this._latlngs[i].alt :
this._latlngs[i][2] !== undefined ? +this._latlngs[i][2] : 1;

grid[y] = grid[y] || [];
cell = grid[y][x];

if (!cell) {
cell = grid[y][x] = [p.x, p.y, alt];
cell.p = p;
} else {
cell[0] = (cell[0] * cell[2] + p.x * alt) / (cell[2] + alt); // x
cell[1] = (cell[1] * cell[2] + p.y * alt) / (cell[2] + alt); // y
cell[2] += alt; // cumulated intensity value
}

} else {
cell[0] = (cell[0] * cell[2] + p.x * k) / (cell[2] + k); // x
cell[1] = (cell[1] * cell[2] + p.y * k) / (cell[2] + k); // y
cell[2] += k; // cumulated intensity value
}
// Set the max for the current zoom level
if (cell[2] > this._max) {
this._max = cell[2];
}
}

this._heat.max(this._max);

for (i = 0, len = grid.length; i < len; i++) {
if (grid[i]) {
for (j = 0, len2 = grid[i].length; j < len2; j++) {
cell = grid[i][j];
if (cell) {
if (cell && bounds.contains(cell.p)) {
data.push([
Math.round(cell[0]),
Math.round(cell[1]),
Math.min(cell[2], max)
Math.min(cell[2], this._max)
]);
}
}
Expand Down

0 comments on commit 5b283c9

Please sign in to comment.