Folium related question on how to unpack the list in the dict while using GroupedLayerControl

40 views Asked by At

I am now working on a folium and I am using GroupedLayerControl to group the feature group in the map, as the group no. maybe varied base on the catergories no. I need to deal with, hence I am trying to use a for loop to automated this process This is the lists I am using for loop to create a dict

cat1 = ['Both groups', 'group 1', 'group 2']
fgslice = [['fg1', 'fg2', 'fg3', 'fg4'], ['fg5', 'fg6', 'fg7', 'fg8'], ['fg9', 'fg10', 'fg11', 'fg12']]

and the code of the for loop:

mapfg = {}
for a, fgsingle in zip(cat1, fglistslice):
    mapfg[a] = fgsing

Code to add GroupedLayerControl:

GroupedLayerControl(
    groups=mapfg,
    exclusive_groups=False,
    collapsed=False,
).add_to(my_map1)

however, when I tried to place it in the GroupedLayerControl's groups it reported "AttributeError: 'str' object has no attribute 'get_name'".

I am guessing it may be due to the list I am using in the dict which the folium cannot get the name of the feature group form it.

The result should look something like the following code

GroupedLayerControl(
    groups={'Both groups': [fg1, fg2,fg3,fg4], "group1":[fg5, fg6,fg7,fg8],"group 2":[fg9, fg10,fg11,fg12]},
    exclusive_groups=False,
    collapsed=False,
).add_to(my_map1)

Hence I would like to ask if there is any way I can make it work?

1

There are 1 answers

0
collab-with-tushar-raj On

I see a lot of compilation issues in your code which I understand cannot be found out when coding in Python.

Your logic of creating dictionaries is correct but I do not understand why you have not used the right variables when creating the same.

cat1 = ['Both groups', 'group 1', 'group 2']
fgslice = [['fg1', 'fg2', 'fg3', 'fg4'], ['fg5', 'fg6', 'fg7', 'fg8'], ['fg9', 'fg10', 'fg11', 'fg12']]

mapfg = {}
for a, fgsingle in zip(cat1, fglistslice): // From where does `fglistslice` came from? It should be `fgslice`
    mapfg[a] = fgsing // From where does `fgsing` came from? It should be `fgsingle`

Here is the corrected version of your logic which works fine and gives output as intended.

Sharing the corrected code here as well:

cat1 = ['Both groups', 'group 1', 'group 2']
fgslice = [['fg1', 'fg2', 'fg3', 'fg4'], ['fg5', 'fg6', 'fg7', 'fg8'], ['fg9', 'fg10', 'fg11', 'fg12']]

mapfg = {}
for a, fgsingle in zip(cat1, fgslice):
    mapfg[a] = fgsingle
    
print(mapfg)