Open SCantergiani opened 10 months ago
Did you already already find a solution for that? I think I'm facing the same problem
Did you already already find a solution for that? I think I'm facing the same problem
@MoritzOff
I found a workaround.
You must use folium.Choroplet and then access the underlying geojson and cmap. Here is the code I used, you can modify it with your own GeoJson dataframe:
mapita = folium.Map()
fg_lst = []
# Solamente nos interesan las columnas de data
for i, col in enumerate(regiones_data.columns):
if col in ["CUT_REG", "REGION"]:
continue
choropleth = folium.Choropleth(
geo_data=regiones,
name=col,
data=regiones_data,
columns=["CUT_REG", col],
key_on="feature.properties.CUT_REG",
fill_color="YlOrRd",
fill_opacity=0.7,
line_opacity=0.2,
legend_name=col,
highlight=True,
line_color="white",
)
folium.GeoJsonTooltip(
fields=["REGION_x", col],
aliases=[field.title() for field in ["REGION_x", col]],
style="background-color: white; color: #333333; font-family: arial; font-size: 12px; padding: 10px;",
localize=True,
).add_to(choropleth.geojson)
fg = folium.FeatureGroup(col)
choropleth.geojson.add_to(fg)
mapita.add_child(fg)
mapita.add_child(choropleth.color_scale)
mapita.add_child(BindColormap(fg, choropleth.color_scale))
fg_lst.append(fg)
GroupedLayerControl(
groups={"Groups": fg_lst},
collapsed=False,
).add_to(mapita)
mapita
I'm trying to replicate your issue, but not succeeding so far. Can you provide a minimal, standalone code snippet that reproduces your issue?
Here's what I tried what worked for me:
m = folium.Map()
for i in range(4):
fg = folium.FeatureGroup(name=str(i)).add_to(m)
folium.GeoJson(
"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_states.json",
name=f"geojson {i}",
highlight_function=lambda x: {
"fillOpacity": i / 10,
},
).add_to(fg)
folium.LayerControl(collapsed=False).add_to(m)
m.show_in_browser()
So for some reason if I try to add multiple groups via a for loop, it just keep the last one. Different story if I add them one by one.
This is my code: