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

Change layer group visibility programmatically #1118

Open
giswqs opened this issue Jun 19, 2023 · 4 comments · May be fixed by #1129
Open

Change layer group visibility programmatically #1118

giswqs opened this issue Jun 19, 2023 · 4 comments · May be fixed by #1129

Comments

@giswqs
Copy link
Contributor

giswqs commented Jun 19, 2023

The layer group can be toggled on/off manually using the LayersControl. Is it possible to change the layer group visibility programmatically? layer_group.visible=False does not work.

from ipyleaflet import (
    Map, basemaps, basemap_to_tiles,
    Circle, Marker, Rectangle, LayerGroup, LayersControl
)

toner = basemap_to_tiles(basemaps.Stamen.Toner)

m = Map(layers=(toner, ), center=(50, 354), zoom=5)

# Create some layers
marker = Marker(location=(50, 354))
circle = Circle(location=(50, 370), radius=50000, color="yellow", fill_color="yellow")
rectangle = Rectangle(bounds=((54, 354), (55, 360)), color="orange", fill_color="orange")

# Create layer group
layer_group = LayerGroup(layers=(marker, circle), name='Layer Group')

m.add_layer(layer_group)

layer_group.add_layer(rectangle)

control = LayersControl(position='topright')
m.add_control(control)
m
@Nirdual
Copy link

Nirdual commented Jun 26, 2023

Hello,
I was trying to set a layer group not visible by default, which is easy to do with Folium but unfortunately does not seem to be implemented yet in ipyleaflet.
Anyway, i managed to do so by creating a custom layer control, hopefully it can solve your problem as well.

# Add WidgetControl in import
from ipyleaflet import (
    Map, basemaps, basemap_to_tiles,
    Circle, Marker, Rectangle, LayerGroup, LayersControl, WidgetControl
)
# Add some ipywidgets
from ipywidgets import Checkbox, Layout, ToggleButton

toner = basemap_to_tiles(basemaps.Stamen.Toner)
m = Map(layers=(toner, ), center=(50, 354), zoom=5)

# Create some layers
marker = Marker(location=(50, 354))
circle = Circle(location=(50, 370), radius=50000, color="yellow", fill_color="yellow")`
rectangle = Rectangle(bounds=((54, 354), (55, 360)), color="orange", fill_color="orange")

# Create layer group
layer_group = LayerGroup(layers=(marker, circle), name='Layer Group')
layer_group.add_layer(rectangle)

# the observed function
def toggle(change):
    if change.new == False:
        m.remove(layer_group)
    else:
        m.add_layer(layer_group)

# Add layer_group to map if your checkbox is set to True by default
# do not add to map otherwise, or it will not be coherent.
m.add_layer(layer_group)

# Our custom layer control (a checkbox here)
check = Checkbox(value = True,
                 description = 'test', 
                 indent = False,
                 layout = Layout(width = '100%', 
                                 margin = '0px 5px 0px 5px'))

# We observe the checkbox's value, whenever the value changes, it triggers the 'toggle' function
check.observe(toggle, 'value')

# We add the checkbox to the map
m.add(WidgetControl(widget = check, position = 'topright'))

# I programmatically set the tickbox to False (You can still interact manually)
# This is where you want to do your more specific stuff.
check.value = False

# The layer control is made redundant by the tickbox so i commented it.
# control = LayersControl(position='topright')
# m.add_control(control)

m

@giswqs
Copy link
Contributor Author

giswqs commented Jun 27, 2023

I though about this workaround that removes and adds layer program programmatically. It is a bit slow when the layer group has many features. I might also change the layer order if some layers are added after the layer group. I would prefer a native solution that can set layer group visibility like other layers.

@12rambau
Copy link

@giswqs, can what I suggested for MarkerCluster herebe adapted for your need ?

@giswqs
Copy link
Contributor Author

giswqs commented Jul 25, 2023

@12rambau Yes. It looks good to me. Thank you

@12rambau 12rambau linked a pull request Jul 27, 2023 that will close this issue
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

Successfully merging a pull request may close this issue.

3 participants