Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/folium/plugins/heat_map.py
1719 views
1
import warnings
2
3
import numpy as np
4
5
from folium.elements import JSCSSMixin
6
from folium.map import Layer
7
from folium.template import Template
8
from folium.utilities import (
9
if_pandas_df_convert_to_numpy,
10
none_max,
11
none_min,
12
remove_empty,
13
validate_location,
14
)
15
16
17
class HeatMap(JSCSSMixin, Layer):
18
"""
19
Create a Heatmap layer
20
21
Parameters
22
----------
23
data : list of points of the form [lat, lng] or [lat, lng, weight]
24
The points you want to plot.
25
You can also provide a numpy.array of shape (n,2) or (n,3).
26
name : string, default None
27
The name of the Layer, as it will appear in LayerControls.
28
min_opacity : default 1.
29
The minimum opacity the heat will start at.
30
max_zoom : default 18
31
Zoom level where the points reach maximum intensity (as intensity
32
scales with zoom), equals maxZoom of the map by default
33
radius : int, default 25
34
Radius of each "point" of the heatmap
35
blur : int, default 15
36
Amount of blur
37
gradient : dict, default None
38
Color gradient config. Defaults to
39
{.4: "blue", .6: "cyan", .7: "lime", .8: "yellow", 1: "red"}
40
overlay : bool, default True
41
Adds the layer as an optional overlay (True) or the base layer (False).
42
control : bool, default True
43
Whether the Layer will be included in LayerControls.
44
show: bool, default True
45
Whether the layer will be shown on opening.
46
"""
47
48
_template = Template(
49
"""
50
{% macro script(this, kwargs) %}
51
var {{ this.get_name() }} = L.heatLayer(
52
{{ this.data|tojson }},
53
{{ this.options|tojavascript }}
54
);
55
{% endmacro %}
56
"""
57
)
58
59
default_js = [
60
(
61
"leaflet-heat.js",
62
"https://cdn.jsdelivr.net/gh/python-visualization/folium@main/folium/templates/leaflet_heat.min.js",
63
),
64
]
65
66
def __init__(
67
self,
68
data,
69
name=None,
70
min_opacity=0.5,
71
max_zoom=18,
72
radius=25,
73
blur=15,
74
gradient=None,
75
overlay=True,
76
control=True,
77
show=True,
78
**kwargs
79
):
80
super().__init__(name=name, overlay=overlay, control=control, show=show)
81
self._name = "HeatMap"
82
data = if_pandas_df_convert_to_numpy(data)
83
self.data = [
84
[*validate_location(line[:2]), *line[2:]] for line in data # noqa: E999
85
]
86
if np.any(np.isnan(self.data)):
87
raise ValueError("data may not contain NaNs.")
88
if kwargs.pop("max_val", None):
89
warnings.warn(
90
"The `max_val` parameter is no longer necessary. "
91
"The largest intensity is calculated automatically.",
92
stacklevel=2,
93
)
94
self.options = remove_empty(
95
min_opacity=min_opacity,
96
max_zoom=max_zoom,
97
radius=radius,
98
blur=blur,
99
gradient=gradient,
100
**kwargs
101
)
102
103
def _get_self_bounds(self):
104
"""
105
Computes the bounds of the object itself (not including it's children)
106
in the form [[lat_min, lon_min], [lat_max, lon_max]].
107
108
"""
109
110
bounds = [[None, None], [None, None]]
111
for point in self.data:
112
bounds = [
113
[
114
none_min(bounds[0][0], point[0]),
115
none_min(bounds[0][1], point[1]),
116
],
117
[
118
none_max(bounds[1][0], point[0]),
119
none_max(bounds[1][1], point[1]),
120
],
121
]
122
return bounds
123
124