Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem adding layers by for loop #1857

Open
SCantergiani opened this issue Jan 6, 2024 · 3 comments
Open

Problem adding layers by for loop #1857

SCantergiani opened this issue Jan 6, 2024 · 3 comments

Comments

@SCantergiani
Copy link

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:

mapita = folium.Map(location=[-33.458725289553634, -70.65252746454252], zoom_start=4.4, tiles=None)

fig_layer = []

for i, col in enumerate(color_dict.columns):
    cm = ColorMapLayers(color_dict, col)
    fg = folium.FeatureGroup(col.lower())
    folium.GeoJson(
        data=regiones,  # es el dataframe consolidado (GeoDataFrame)
        name=f"regiones_{col}",
        zoom_on_click=False,  # Hace zoom o no al hacer click en un vector,
        # Como queremos que se vean nuestros vectores
        style_function=lambda feature: {
            "color": "white",
            "weight": 1,
            "Opacity": 0.8,
            "fillColor": "orange",
            "fillOpacity": 0.8,
            "fillColor": cm(color_dict[col][feature["properties"]["CUT_REG"]]),
        },
        # Queremos agregar highligh en hover
        highlight_function=lambda x: {
            "fillColor": "#000000",
            "color": "#000000",
            "fillOpacity": 0.50,
            "weight": 0.1,
        },
        # Hover tooltip
        tooltip=folium.features.GeoJsonTooltip(
            fields=["CUT_REG", "REGION", col],
            aliases=["CUT_REG", "REGION", col],
            style=(
                "background-color: white; color: #333333; font-family: arial; font-size: 8px; padding: 10px;"
            ),
        ),
    ).add_to(fg)

    mapita.add_child(fg)

    fig_layer.append(fg)


GroupedLayerControl(
    groups={"Groups": fig_layer},
    collapsed=False,
).add_to(mapita)

mapita
@MoritzOff
Copy link

MoritzOff commented Jan 20, 2024

Did you already already find a solution for that?
I think I'm facing the same problem

@SCantergiani
Copy link
Author

SCantergiani commented Jan 20, 2024

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

@Conengmo
Copy link
Member

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants