Skip to content

Commit f21b1a4

Browse files
TheChyeahhhEl Williamspre-commit-ci[bot]
authored
Add LayerGroup support (#1034) (#2228)
* Add LayerGroup support (#1034) Add a LayerGroup class that mirrors FeatureGroup but uses L.layerGroup() instead of L.featureGroup() in the generated JavaScript, allowing custom options to be passed through to the Leaflet LayerGroup constructor. Closes #1034 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: El Williams <thechyeahhh@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 593ca47 commit f21b1a4

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

folium/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
FitOverlays,
3737
Icon,
3838
LayerControl,
39+
LayerGroup,
3940
Marker,
4041
Popup,
4142
Tooltip,
@@ -88,6 +89,7 @@
8889
"JsCode",
8990
"LatLngPopup",
9091
"LayerControl",
92+
"LayerGroup",
9193
"LinearColormap",
9294
"Link",
9395
"MacroElement",

folium/map.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,57 @@ def __init__(
185185
self.options = remove_empty(**kwargs)
186186

187187

188+
class LayerGroup(Layer):
189+
"""
190+
Create a LayerGroup layer ; you can put things in it and handle them
191+
as a single layer. For example, you can add a LayerControl to
192+
tick/untick the whole group.
193+
194+
LayerGroup is the base class of FeatureGroup in Leaflet.
195+
Unlike FeatureGroup, LayerGroup accepts custom options
196+
that can be used in custom JavaScript code.
197+
198+
Parameters
199+
----------
200+
name : str, default None
201+
The name of the layer group layer.
202+
It will be displayed in the LayerControl.
203+
If None get_name() will be called to get the technical (ugly) name.
204+
overlay : bool, default True
205+
Whether your layer will be an overlay (ticked with a check box in
206+
LayerControls) or a base layer (ticked with a radio button).
207+
control: bool, default True
208+
Whether the layer will be included in LayerControls.
209+
show: bool, default True
210+
Whether the layer will be shown on opening.
211+
**kwargs
212+
Additional (possibly inherited) options. See
213+
https://leafletjs.com/reference.html#layergroup
214+
215+
"""
216+
217+
_template = Template("""
218+
{% macro script(this, kwargs) %}
219+
var {{ this.get_name() }} = L.layerGroup(
220+
{{ this.options|tojavascript }}
221+
);
222+
{% endmacro %}
223+
""")
224+
225+
def __init__(
226+
self,
227+
name: Optional[str] = None,
228+
overlay: bool = True,
229+
control: bool = True,
230+
show: bool = True,
231+
**kwargs: TypeJsonValue,
232+
):
233+
super().__init__(name=name, overlay=overlay, control=control, show=show)
234+
self._name = "LayerGroup"
235+
self.tile_name = name if name is not None else self.get_name()
236+
self.options = remove_empty(**kwargs)
237+
238+
188239
class LayerControl(MacroElement):
189240
"""
190241
Creates a LayerControl object to be added on a folium map.

tests/test_folium.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,26 @@ def test_feature_group(self):
171171
bounds = m.get_bounds()
172172
assert bounds == [[45, -30], [45, 30]], bounds
173173

174+
def test_layer_group(self):
175+
"""Test LayerGroup."""
176+
177+
m = folium.Map()
178+
layer_group = folium.LayerGroup(
179+
name="my_layer_group", custom_option="test_value"
180+
)
181+
layer_group.add_child(folium.Marker([45, -30], popup=folium.Popup("-30")))
182+
layer_group.add_child(folium.Marker([45, 30], popup=folium.Popup("30")))
183+
m.add_child(layer_group)
184+
m.add_child(folium.LayerControl())
185+
186+
m._repr_html_()
187+
188+
bounds = m.get_bounds()
189+
assert bounds == [[45, -30], [45, 30]], bounds
190+
html = m._repr_html_()
191+
assert "L.layerGroup" in html
192+
assert "my_layer_group" in html
193+
174194
def test_topo_json_smooth_factor(self):
175195
"""Test topojson smooth factor method."""
176196
self.m = folium.Map([43, -100], zoom_start=4)

0 commit comments

Comments
 (0)