Skip to content

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 1 commit into from
Oct 31, 2015
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions folium/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
from .terminator import Terminator
from .boat_marker import BoatMarker
from .timestamped_geo_json import TimestampedGeoJson
from .heat_map import HeatMap
80 changes: 80 additions & 0 deletions folium/plugins/heat_map.py
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')
6 changes: 6 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,9 @@ def test_timestamped_geo_json(self):
mape = folium.Map([47, 3], zoom_start=1)
mape.add_children(plugins.TimestampedGeoJson(data))
mape._repr_html_()

def test_heat_map(self):
data = (np.random.normal(size=(100,2))*np.array([[1,1]])+np.array([[48,5]])).tolist()
mapa = folium.Map([48., 5.], tiles='stamentoner', zoom_start=6)
mapa.add_children(plugins.HeatMap(data))
mapa._repr_html_()
Copy link
Member

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.

Copy link
Contributor Author

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 ?).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is a heavy procedure and I'm not sure this is needed for all plugins (am I lazy ?).

Heavy? Yes.
Are you lazy? No!
Should someone else do that? (Me 😒)