Repository navigation
Speed up HeatMap redraws for large datasets - #2351
Open
KhadeerBasha1232 wants to merge 1 commit into
Open
KhadeerBasha1232 wants to merge 1 commit into
KhadeerBasha1232 wants to merge 1 commit into
Conversation
Leaflet.heat's _redraw re-projected every point with map.latLngToContainerPoint on every pan and zoom, stored its grid in sparse arrays whose indices grow with the zoom level, and then scanned those arrays end to end. At high zoom with points spread over the world, that final scan alone took seconds. Patch the hosted leaflet_heat.min.js so that it: - projects each point once and caches it, applying only Leaflet's per-zoom scaling on redraw (same arithmetic, same rounding); - keeps the grid in Maps instead of sparse arrays; - only visits the on-screen part of the grid in the final pass. The drawn heatmap is unchanged: the same points, weights, maximum and pixels as before. Maps whose CRS overrides latLngToPoint keep using the original code. Closes python-visualization#1430 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
KhadeerBasha1232
temporarily deployed
to
pypi
November 16, 2026 12:51 — with
GitHub Actions
Inactive
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Speed up
HeatMapredraws for large datasetsCloses #1430
Summary
HeatMapgets slow with many points, especially when zoomed in: with 20,000 points spread over the world, a single redraw at zoom 13 took ~5 seconds. This PR patches theleaflet_heat.min.jsfile folium hosts so redraws are much faster, without changing what is drawn.Why it was slow
Every pan or zoom runs
_redraw, which:map.latLngToContainerPoint, re-running the map projection (trigonometry plus several object allocations per point) even though a point's projected position never changes.What this PR changes
The patch is an addition at the end of the file (the original code is untouched). It overrides
_redrawto:latLngToContainerPoint, so pixel positions are bit-identical. The cache is rebuilt bysetLatLngsand extended byaddLatLng.Maps instead of sparse arrays.What stays the same
_max) is still computed from all points at the current zoom, as the weights patch (Leaflet.heat#78) in this file expects. I deliberately did not add the spatial index (R-tree) the issue suggests: skipping off-screen points would change_max, so colours would shift as you pan.latLngToPoint(e.g. some Proj4Leaflet setups) fall back to the original_redraw.folium/plugins/heat_map.pyis unchanged.Benchmarks
Measured in Chromium against the current file, with seeded random data (80% clustered around six cities, 20% spread over the world, random weights). Each view compares the exact point list passed to
simpleheat, the computed_max, and a hash of the canvas pixels.20,000 points
1,000,000 points
* The old code already took ~5 s at these zoom levels with only 20k points, so I didn't run it with 1M.
At world zoom, drawing the canvas dominates, so the gain there is small.
How I verified it
_maxand canvas pixels.addLatLng,setLatLngs,L.LatLngobjects withaltweights, points without weights,L.CRS.SimpleandL.CRS.EPSG4326.latLngToPointcorrectly falls back to the original_redraw.HeatMap(50k points) loading the patched file draws correctly while zooming and panning, with no console errors.Notes for reviewers
heat_map.pyloads this file fromcdn.jsdelivr.net/gh/python-visualization/folium@main/..., so once merged it applies to allHeatMaps right away, not only after the next release.setLatLngs/addLatLng. Code that editslayer._latlngs[i]in place would need to callsetLatLngsafterwards.pre-commit.cifailure (if it shows) comes fromtests/test_features.pyonmainand is fixed in Fix black formatting in test_features.py #2350.