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

add groupedlayercontrol plugin #1592

Merged

Conversation

chansooligans
Copy link
Contributor

@chansooligans chansooligans commented May 19, 2022

This PR adds the leaflet-groupedlayercontrol plugin. Also addresses a closed issue that requested this PR: #1025.

Purpose of plugin is to allow grouping overlays and allowing within-group radio buttons. My motivation for this was because I had created folium maps with several choropleths (e.g. population, density, demographics) where it would make no sense to have multiple choropleths on simultaneously and confuse some users.

Current implementation creates a second layer control but it would be possible to change so that base, grouped overlays, and ungrouped overlays are all contained in a single layer control.

The template and logic are modified copies of the parent class LayerControl.

folium/plugins/__init__.py Show resolved Hide resolved
folium/plugins/groupedlayercontrol.py Outdated Show resolved Hide resolved
tests/plugins/test_grouped_layer_control.py Show resolved Hide resolved
tests/plugins/test_grouped_layer_control.py Outdated Show resolved Hide resolved
tests/plugins/test_grouped_layer_control.py Outdated Show resolved Hide resolved
tests/plugins/test_grouped_layer_control.py Outdated Show resolved Hide resolved
tests/plugins/test_grouped_layer_control.py Outdated Show resolved Hide resolved
folium/plugins/groupedlayercontrol.py Outdated Show resolved Hide resolved
folium/plugins/groupedlayercontrol.py Outdated Show resolved Hide resolved
folium/plugins/groupedlayercontrol.py Outdated Show resolved Hide resolved
folium/plugins/groupedlayercontrol.py Outdated Show resolved Hide resolved
Copy link
Member

@Conengmo Conengmo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[edit: this comment is actually about a different plugin that was mentioned in a related issue]

Though I'd like to include the Leaflet.Control.Layers.Tree plugin in folium, I don't think this PR is the way we should do it, for three reasons:

  • The code in this PR contains a specific use case of that JS plugin: radio buttons in separate groups. The JS plugin itself doesn't only contain that, but accepts a generic layer tree.
  • It contains custom code in Python as well as the JS template. Ideally, a folium plugin should only wrap the JS plugin. But maybe some is unavoidable here.
  • It partially reimplements code of the normal LayerControl class.

IMO an adoption of this JS plugin should have as little code on the folium side as possible and expose the full feature. It should either replace the existing LayerControl class or be compatible with it.

I wonder how that can be done best in folium, since on the Python-side we structure layers a bit different than Leaflet. My best suggestion would be to pass a dictionary with layers to this plugin, then pass that to the JS plugin. And then not use the normal layer control. Something like:

layer1 = Layer()
layer2 = FeatureGroup()
layer 3 = Marker()
GroupedLayerControl(
    base_layers=[layer1],
    overlays={
        'Group': [layer2, layer3]
    }
).add_to(m)

Note that this mimics the arguments of the JS plugin. That means little code on the folium side. Also note that this doesn't use the overlay and control parameters of the folium objects, because the tree structure passed to GroupedLayerControl` defines how the layers are used.

I'd be interested to hear how others think about this.

Copy link
Member

@Conengmo Conengmo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worked on this for a bit:

  • Make it work together with LayerControl, so we don't have to re-implement that here.
  • Make radio buttons optional, enabled by default.
  • Make the user pass in objects instead of layer names. I think that's easier and less error prone.
  • In the test use the rendered template.

https://nbviewer.org/github/chansooligans/folium/blob/plugins-groupedlayercontrol/examples/plugin-GroupedLayerControl.ipynb

@Conengmo Conengmo added the ready PR is ready for merging label Nov 17, 2022
@Conengmo Conengmo merged commit 45c92b6 into python-visualization:main Nov 17, 2022
@Conengmo
Copy link
Member

Thank you @chansooligans! Nice to have this plugin added to folium.

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

Successfully merging this pull request may close these issues.

None yet

4 participants