5

I am trying to display several layers with information on a Choropleth Map having as base layer the Stamen Terrain and displaying the layers as FeatureGroup. Here is my initial code without overlay control:

map = folium.Map(location=[38.58, 0], zoom_start=3, tiles="Stamen Terrain")

fg_tc = folium.FeatureGroup(name="Totalc")

fg_tc.add_child(folium.GeoJson(data=open('world.json', 'r',
                                          encoding='utf-8-sig').read(),
                               style_function=lambda x: {
                                   'fillColor': get_at_once(x['properties']['NAME'], 'totalc'),
                                   'color': 'black',
                                   'weight': 1,
                                   'dashArray': '5, 5',
                                   'fillOpacity': 0.75}))

fg_nc = folium.FeatureGroup(name="New", show=False)

fg_nc.add_child(folium.GeoJson(data=open('world.json', 'r',
                                          encoding='utf-8-sig').read(),
                               style_function=lambda x: {
                                   'fillColor': get_at_once(x['properties']['NAME'], 'new'),
                                   'color': 'black',
                                   'weight': 1,
                                   'dashArray': '5, 5',
                                   'fillOpacity': 0.75}))

fg_td = folium.FeatureGroup(name="Totald", show=False)

fg_td.add_child(folium.GeoJson(data=open('world.json', 'r',
                                          encoding='utf-8-sig').read(),
                               style_function=lambda x: {
                                   'fillColor': get_at_once(x['properties']['NAME'], 'totald'),
                                   'color': 'black',
                                   'weight': 1,
                                   'dashArray': '5, 5',
                                   'fillOpacity': 0.75}))

fg_ac = folium.FeatureGroup(name="Active", show=False)

fg_ac.add_child(folium.GeoJson(data=open('world.json', 'r',
                                          encoding='utf-8-sig').read(),
                               style_function=lambda x: {
                                   'fillColor': get_at_once(x['properties']['NAME'], 'active_cases'),
                                   'color': 'black',
                                   'weight': 1,
                                   'dashArray': '5, 5',
                                   'fillOpacity': 0.75}))


map.add_child(fg_tc)

map.add_child(fg_nc)

map.add_child(fg_td)

map.add_child(fg_ac)

map.add_child(folium.LayerControl())

map.save("map1.html")

my problem is that the FeatureGroup layers are selectable by checkbox allowing more than one layer to be selected at a certain time. In my case I would need at one time to have displayed the Stamen Terrain as background and only one active layer and I do not know how to do this.

I have also tried to control the overlay, but then only one single layer can be visible and I am loosing the Stamen Terrain as a background, i.e. the layers are controlled by radio buttons but there is only one group which includes the Stamen Terrain and the data FeatureGroup layers...

Any idea how I could display, as desired: Stamen Terrain as background and only one active at a moment data layer? I am not fixed to use the FeatureGroup, but at the moment my knowledge is limited to this and so far my google fu failed me...

Thanks!

p.s. here are also some printscreens with the try outs: 1&2 are without overlay control and one can see what happens in '2' where more layers are selected. 3&4 are with overlay=False and then only one layer can be active.

printscreen with the try out results

Vlad
  • 197
  • 1
  • 9

1 Answers1

3

I had similar problem. If you didn't figure it out yet this is more or less what I did:

map = folium.Map(location=[ , ], zoom_start= , tiles=None)
base_map = folium.FeatureGroup(name='Basemap', overlay=True, control=False)
folium.TileLayer(tiles='OpenStreetMap').add_to(base_map)
base_map.add_to(map)

layer1 = folium.FeatureGroup(name='layer1', overlay=False)
layer1.add_to(map)

This resolved the issue for me.

Pawcio
  • 31
  • 2
  • please see my p.s. and the examples 3 and 4 from there. What you are proposing is hiding the base map. My desired result is basemap to stay and for the rest of the layers to have radio buttons instead of checkboxes... – Vlad Jul 31 '20 at 09:59
  • Hi, that is what it does. The base layer which is the tile you use you set to overlay=True so it can overlay with other layers so like number 1 on your examples, but you set control=False so it is always on, then other layers you set overlay=False and control=True, so you have radio buttons like in 3&4, but because base layer is set to overlay=True it is always there. So you have radio buttons for geojson layers and no choice for base layer because it is always on. It works for me. – Pawcio Aug 01 '20 at 15:27