Looping layers in Folium layer control, but it only adds one

76 views Asked by At

I've been trying to fix this for a while, but with no luck. I succesfully managed to make it with 2 layers, but as soon I use a for loop it doesn't work anymore. So far switching the colormap bar whenever I change the selection, but the map is not changing at all:

Here is my code:

from folium.plugins import GroupedLayerControl

mapa = folium.Map(location=[-33.458725289553634, -70.65252746454252], zoom_start=4)
fig_layers = []

for i, col in enumerate(df_reg.columns):
    colormap = ColorMapLayers(df_reg, col)

    # Capa
    layer = folium.GeoJson(
        data=df_sub_reg,  # es el dataframe consolidado (GeoDataFrame)
        name=col,
        overlay=True,
        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": 0.5,
            "Opacity": 0.8,
            "fillOpacity": 0.8,
            "fillColor": colormap(
                df_reg[col][
                    feature["properties"]["CUT_REG"]
                ]  # Superficie es el valor el cual se pinta el mapa
            ),
        },
        # 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=["REGION", col],
            aliases=["REGION", col],
            style=(
                "background-color: white; color: #333333; font-family: arial; font-size: 8px; padding: 10px;"
            ),
        ),
    )

    # AƱadiendo grupo
    fg = folium.FeatureGroup(col.lower())
    layer.add_to(fg)

    mapa.add_child(fg)
    mapa.add_child(colormap)
    mapa.add_child(BindColormap(fg, colormap))

    fig_layers.append(fg)

GroupedLayerControl(
    groups={"Groups": fig_layers},
    collapsed=False,
).add_to(mapa)

mapa

enter image description here

0

There are 0 answers