Python folium – Markercluster not iterable with GroupedLayerControl

Question:

I would like to group my 2 marker cluster layers, where one is reliant on the other by providing a separate styling. Hence the second one is set as control=False.
Nevertheless, I want to have it disappear when the first one is switched off.

Along with the new Python folium issue v.0.14 I found, that the new feature has been provided, which potentially could resolve my issue:

https://github.com/ikoojoshi/Folium-GroupedLayerControl

Allow only one layer at a time in Folium LayerControl

and I’ve applied the following code:

df = pd.read_csv("or_geo.csv")
fo=FeatureGroup(name="OR")
or_cluster = MarkerCluster(name="Or", overlay=True, visible=True).add_to(map)

or_status = MarkerCluster(overlay=True,
                            control=False,
                             visible=False,
                             disableClusteringAtZoom=16,
                             ).add_to(map)

 GroupedLayerControl(
 groups={'OrB': or_cluster, 'OrC': or_status},
 collapsed=False,
 ).add_to(map)

and the console throws the following error:

TypeError: ‘MarkerCluster’ object is not iterable

How could I switch off 2 layer groups at once?

enter image description here

UPDATE:

The answer below provides the code, which seems to work but not in the way I need.

df = pd.read_csv("or_geo.csv")
fo=FeatureGroup(name="Or",overlay = True)
or_cluster = MarkerCluster(name="Or").add_to(map)

or_status = MarkerCluster(control=False,
                             visible=True,
                             disableClusteringAtZoom=16,
                             ).add_to(map)

# definition of or_marker
# definition of or_stat_marker

or_cluster.add_child(or_marker)
or_status.add_child(or_stat_marker)

GroupedLayerControl(
groups={"Or": [or_cluster, or_status]},
collapsed=False,
exclusive_group=False,
    ).add_to(map)

enter image description here

I have a separate box instead, but what is worst I can just switch between one layer and another whereas I would like to have them reliant on the main group. The exclusive_groups option allows me to untick both of them but I am looking for something, which would allow me to switch off two of them at once (place the thick box on the major group instead). Is it possible to have something like this?

Asked By: MKR

||

Answers:

Try passing your markerclusters as a list to the GroupedLayerControl, not one by one. This is described here:
https://nbviewer.org/github/chansooligans/folium/blob/plugins-groupedlayercontrol/examples/plugin-GroupedLayerControl.ipynb

 GroupedLayerControl(
 groups={'OrB': [or_cluster, or_status]},
 collapsed=False,
 ).add_to(map)

Update I

I see what you mean, that was definitely nonsense as it splits groups instead of joining them. so, back to topic

We had a similar discussion here and I am still convinced that the FeatureSubGroup should solve this issue. I use it in exact that way that I enable/disable a MarkerCluster in the legend and multiple FeatureGroupSubGroups (which are added not to the map but to the MarkerCluster) appear/disappear. Perhaps you try that again

Answered By: flipSTAR

This is my first time to answer on stackoverflow, please feel free to let me know if I am not clear enough.

Folium=0.14.0.

Question

  1. Two layers are reliant on the main group
  2. Switch off two of them at once

 

Here you will have two LayerControl Panel on topright of the map.

The first panel will have openstreetmap (radio button) and main (checkbox); the second panel will have group name "Or" with sub1 and sub2 (radio button) that are reliant on each other.

Answer 1.
The two layers are reliant on each other, so we use radio button at the second panel.

Answer 2.
You can click on "main" at the first panel to switch two of the sub-layer at once.

Example Folium Map

df = pd.read_csv("or_geo.csv")

fmap = Map(...)

main_layer = FeatureGroup(name="main").add_to(fmap)
sub_layer1 = FeatureGroup(name="sub1").add_to(main_layer)
sub_layer2 = FeatureGroup(name="sub2").add_to(main_layer)

or_cluster = MarkerCluster(name="Or").add_to(sub_layer1)
or_status = MarkerCluster(control=False,
                          visible=True,
                          disableClusteringAtZoom=16,
                          ).add_to(sub_layer2)

# definition of or_marker
# definition of or_stat_marker

or_cluster.add_child(or_marker)
or_status.add_child(or_stat_marker)

LayerControl(collapsed=False).add_to(fmap)
GroupedLayerControl(groups={"Or": [sub_layer1, sub_layer2]},
                    collapsed=False,
                    exclusive_group=True
                    ).add_to(fmap)
Answered By: RobertChang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.