Skip to content

Commit

Permalink
Patch HeatLayer for better rendering
Browse files Browse the repository at this point in the history
Patch comes from Leaflet/Leaflet.heat#78

We'll remove it when this PR get merged and released.
  • Loading branch information
yohanboniface committed Oct 5, 2023
1 parent ca72576 commit 5bfa2fc
Showing 1 changed file with 78 additions and 8 deletions.
86 changes: 78 additions & 8 deletions umap/static/umap/js/umap.layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,6 @@ L.U.Layer.Heat = L.HeatLayer.extend({
this.setLatLngs([])
},

redraw: function () {
// setlalngs call _redraw through setAnimFrame, thus async, so this
// can ends with race condition if we remove the layer very faslty after.
// Remove me when https://github.com/Leaflet/Leaflet.heat/pull/53 is released.
if (!this._map) return
L.HeatLayer.prototype.redraw.call(this)
},

getFeatures: function () {
return {}
},
Expand Down Expand Up @@ -190,6 +182,84 @@ L.U.Layer.Heat = L.HeatLayer.extend({
}
this._updateOptions()
},

_redraw: function () {
// Import patch from https://github.com/Leaflet/Leaflet.heat/pull/78
// Remove me when this get merged and released.
if (!this._map) {
return
}
var data = [],
r = this._heat._r,
size = this._map.getSize(),
bounds = new L.Bounds(L.point([-r, -r]), size.add([r, r])),
cellSize = r / 2,
grid = [],
panePos = this._map._getMapPanePos(),
offsetX = panePos.x % cellSize,
offsetY = panePos.y % cellSize,
i,
len,
p,
cell,
x,
y,
j,
len2

this._max = 1

for (i = 0, len = this._latlngs.length; i < len; i++) {
p = this._map.latLngToContainerPoint(this._latlngs[i])
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
}

// 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 && bounds.contains(cell.p)) {
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
},
})

L.U.DataLayer = L.Evented.extend({
Expand Down

0 comments on commit 5bfa2fc

Please sign in to comment.