1

I want to make an "interactive" map with multiple layers using geopandas explore() function and folium. I was able to generate exactly what I aim for, with one exception: the constraint that only one layer would be allowed at a time. In other words, I want that if someone click on the layer "Adaptation climat ☀❄️", then the layer that was previously selected is automatically unselected and only "Adaptation climat ☀❄️" is displayed.

I looked online for hours and did not find a solution. I guess it's at the LayerControl() level of Folium but I can't find the solution.

I found FeatureGroup layer control in Folium - only one active layer that is related but does not give an answer.

Thanks in advance for your help!

The dataframe is of the form: enter image description here

The html map obtained looks like: enter image description here

And the code is:

list_var = ['note_vetuste', 'note_encadrement', 'note_climat', 'note_cantine', 'note_abord_securise']
list_var_display = ['Vétusté des locaux ', 'Moyens humains ‍', 'Adaptation climat ☀❄️', 'Cantine ', 'Sécurisation des abords ‍♀️']

m = gdf[['ecole', list_var[0], list_var[0]+'_count', 'geometry']].explore(
          column=list_var[0],
          cmap = 'RdYlGn',
          marker_kwds=dict(radius=10, fill=True),
          legend=False,
          tooltip=False,
          popup=True,
          k=5, # use 10 bins
          vmin=1, vmax=5,
          tiles=None,
          legend_kwds=dict(caption='',colorbar=True, fmt="{:5.2f}"),
          name=list_var_display[0],
          missing_kwds={'color': 'darkgrey'}
     )

     list_var.pop(0)
     list_var_display.pop(0)
     for k, var in enumerate(list_var): 
          gdf[['ecole', var, var+'_count', 'geometry']].explore(
               m=m,
               column=var,
               marker_kwds=dict(radius=10, fill=True),
               cmap='RdYlGn',
               tooltip=False,
               popup=True,
               legend=False,
               k=5, # use 10 bins
               vmin=1, vmax=5,
               name=list_var_display[k], # name of the layer in the map
               missing_kwds={'color': 'darkgrey'},
               show=False
          )

     folium.TileLayer('cartodbpositron', control=False).add_to(m)  # use folium to add alternative tiles
     folium.LayerControl(position="topleft", collapsed=False).add_to(m)  # use folium to add layer control
     m.save("carte.html")
shora
  • 131
  • 11

1 Answers1

3

It seems to be possible that you differ that behaviour with the overlay-status of the layers. Now we need to find a possibility to forward overlay=False to your map as you include/create the map with geopandas (like described in your referenced answer https://stackoverflow.com/a/63189269/13843906 ). Perhaps try passing it with your explore statements:

    ...explore(... ,
               overlay=False,
               ...)

(Code updated after feedback in comments, thx!)

or

Another info: In this github issue is mentioned that this functionality will be added in folium 0.14: https://github.com/python-visualization/folium/issues/1025 Looking through the topic you will find a link to a jupyter where the new behaviour seems described: https://github.com/python-visualization/folium/pull/1592#pullrequestreview-1184241705

Try updating your folium version and add these lines to your code (perhaps use list_var_displayinstead of list_var:

from folium.plugins import GroupedLayerControl

GroupedLayerControl(
    groups={'groups1': list_var},
    collapsed=False,
).add_to(m) 

Not sure if it works as you do not use pure folium, but it is worth a try

flipSTAR
  • 579
  • 1
  • 4
  • 18
  • I tried this and the solution is close to what you suggested: I add to add overlay=False to all my explore() and the result is exactly what I wanted. Thanks – shora Nov 24 '22 at 18:03