-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Create HeatMap as suggested in #213 #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Heat map | ||
-------- | ||
|
||
Create a HeatMap layer | ||
""" | ||
import json | ||
from jinja2 import Template | ||
|
||
from folium.element import JavascriptLink, Figure | ||
from folium.map import TileLayer | ||
|
||
class HeatMap(TileLayer): | ||
def __init__(self, data, name=None, | ||
min_opacity = .5, max_zoom=18, max_val=1., | ||
radius=25, blur=15, gradient=None, | ||
overlay = True): | ||
"""Create a Heatmap layer | ||
|
||
Parameters | ||
---------- | ||
data : list of points of the form [lat, lng] or [lat, lng, weight] | ||
The points you want to plot. | ||
You can also provide a numpy.array of shape (n,2) or (n,3). | ||
name : str | ||
The name of the layer that will be created. | ||
min_opacity : default 1. | ||
The minimum opacity the heat will start at. | ||
max_zoom : default 18 | ||
Zoom level where the points reach maximum intensity (as intensity scales with zoom), | ||
equals maxZoom of the map by default | ||
max_val : float, default 1. | ||
Maximum point intensity | ||
radius : int, default 25 | ||
Radius of each "point" of the heatmap | ||
blur : int, default 15 | ||
Amount of blur | ||
gradient : dict, default None | ||
Color gradient config. e.g. {0.4: 'blue', 0.65: 'lime', 1: 'red'} | ||
""" | ||
super(TileLayer, self).__init__() | ||
self._name = 'HeatMap' | ||
self.tile_name = name if name is not None else self.get_name() | ||
|
||
self.data = [[x for x in line] for line in data] | ||
self.min_opacity = min_opacity | ||
self.max_zoom = max_zoom | ||
self.max_val = max_val | ||
self.radius = radius | ||
self.blur = blur | ||
self.gradient = json.dumps(gradient, sort_keys=True) if gradient is not None else "null" | ||
self.overlay = overlay | ||
|
||
self._template = Template(u""" | ||
{% macro script(this, kwargs) %} | ||
var {{this.get_name()}} = L.heatLayer( | ||
{{this.data}}, | ||
{ | ||
minOpacity: {{this.min_opacity}}, | ||
maxZoom: {{this.max_zoom}}, | ||
max: {{this.max_val}}, | ||
radius: {{this.radius}}, | ||
blur: {{this.blur}}, | ||
gradient: {{this.gradient}} | ||
}) | ||
.addTo({{this._parent.get_name()}}); | ||
{% endmacro %} | ||
""") | ||
|
||
def render(self, **kwargs): | ||
super(TileLayer, self).render() | ||
|
||
figure = self.get_root() | ||
assert isinstance(figure,Figure), ("You cannot render this Element " | ||
"if it's not in a Figure.") | ||
|
||
figure.header.add_children(\ | ||
JavascriptLink("https://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"), | ||
name='leaflet-heat.js') |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test only if the HTML is created, correct? I guess that all the plugins are tested like that... Not sure if we can do better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct.
We can do a bit better in doing the same thing as
test_folium.py
: compare the output of.render()
with code generated from a validation template.But this is a heavy procedure and I'm not sure this is needed for all plugins (am I lazy ?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heavy? Yes.
Are you lazy? No!
Should someone else do that? (Me 😒)