-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
add groupedlayercontrol plugin #1592
Conversation
There was a problem hiding this 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.
There was a problem hiding this 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.
Thank you @chansooligans! Nice to have this plugin added to folium. |
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.