Skip to content
Draft
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
18 changes: 17 additions & 1 deletion folium/plugins/groupedlayercontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class GroupedLayerControl(JSCSSMixin, MacroElement):
exclusive_groups : bool, default True
Whether to use radio buttons (default) or checkboxes.
If you want to use both, use two separate instances of this class.
base_layers : list, default None
Layer objects, typically tile layers, to show as base layers in this
control. These are removed from the regular LayerControl. Use the
layers' `show` argument to choose which one is shown on opening.
**kwargs
Additional (possibly inherited) options. See
https://leafletjs.com/reference.html#control-layers
Expand All @@ -45,7 +49,15 @@ class GroupedLayerControl(JSCSSMixin, MacroElement):
{% macro script(this,kwargs) %}

L.control.groupedLayers(
{%- if this.base_layers %}
{
{%- for key, val in this.base_layers.items() %}
{{ key|tojson }} : {{val}},
{%- endfor %}
},
{%- else %}
null,
{%- endif %}
{
{%- for group_name, overlays in this.grouped_overlays.items() %}
{{ group_name|tojson }} : {
Expand All @@ -65,12 +77,16 @@ class GroupedLayerControl(JSCSSMixin, MacroElement):
{% endmacro %}
""")

def __init__(self, groups, exclusive_groups=True, **kwargs):
def __init__(self, groups, exclusive_groups=True, base_layers=None, **kwargs):
super().__init__()
self._name = "GroupedLayerControl"
self.options = remove_empty(**kwargs)
if exclusive_groups:
self.options["exclusiveGroups"] = list(groups.keys())
self.base_layers = {}
for element in base_layers or []:
self.base_layers[element.layer_name] = element.get_name()
element.control = False
self.layers_untoggle = set()
self.grouped_overlays = {}
for group_name, sublist in groups.items():
Expand Down
32 changes: 32 additions & 0 deletions tests/plugins/test_grouped_layer_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,35 @@ def test_grouped_layer_control():
{fg2.get_name()}.remove();
""")
assert expected in out


def test_grouped_layer_control_base_layers():
m = folium.Map([40.0, 70.0], zoom_start=6, tiles=None)
osm = folium.TileLayer("OpenStreetMap", name="osm").add_to(m)
topo = folium.TileLayer("OpenTopoMap", name="topo", show=False).add_to(m)
fg1 = folium.FeatureGroup(name="g1").add_to(m)
fg2 = folium.FeatureGroup(name="g2").add_to(m)
groupedlayercontrol.GroupedLayerControl(
groups={"groups1": [fg1, fg2]}, base_layers=[osm, topo]
).add_to(m)
lc = folium.LayerControl().add_to(m)
out = normalize(m._parent.render())

expected = normalize(f"""
L.control.groupedLayers(
{{
"osm" : {osm.get_name()},
"topo" : {topo.get_name()},
}},
{{
"groups1" : {{
"g1" : {fg1.get_name()},
"g2" : {fg2.get_name()},
}},
}},
{{"exclusiveGroups": ["groups1",],}},
).addTo({m.get_name()});
""")
assert expected in out
# The base layers are no longer listed in the regular LayerControl.
assert not lc.base_layers
Loading