Skip to content

Commit b1880ce

Browse files
committed
Add FeatureGroupSubGroup plugin
This plugin [0] allows can be used to either: * create nested overlays that can be turned on or off together or separately, ```python fg = folium.FeatureGroup() m.add_child(fg) g1 = folium.plugins.FeatureGroupSubGroup(fg, 'g1') m.add_child(g1) g2 = folium.plugins.FeatureGroupSubGroup(fg, 'g2') m.add_child(g2) l = folium.LayerControl().add_to(m) ``` * make multiple overlays part of the same MarkerCluster, ```python mcg = folium.plugins.MarkerCluster(control=False) m.add_child(mcg) g1 = folium.plugins.FeatureGroupSubGroup(mcg, 'g1') m.add_child(g1) g2 = folium.plugins.FeatureGroupSubGroup(mcg, 'g2') m.add_child(g2) l = folium.LayerControl().add_to(m) ``` [0] https://github.com/ghybs/Leaflet.FeatureGroup.SubGroup Signed-off-by: Olivier Mehani <[email protected]>
1 parent f90d9f3 commit b1880ce

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

folium/plugins/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from folium.plugins.boat_marker import BoatMarker
1515
from folium.plugins.draw import Draw
1616
from folium.plugins.fast_marker_cluster import FastMarkerCluster
17+
from folium.plugins.feature_group_sub_group import FeatureGroupSubGroup
1718
from folium.plugins.float_image import FloatImage
1819
from folium.plugins.fullscreen import Fullscreen
1920
from folium.plugins.heat_map import HeatMap
@@ -33,6 +34,7 @@
3334
'BoatMarker',
3435
'Draw',
3536
'FastMarkerCluster',
37+
'FeatureGroupSubGroup',
3638
'FloatImage',
3739
'Fullscreen',
3840
'HeatMap',
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from __future__ import (absolute_import, division, print_function)
4+
5+
from branca.element import Figure, JavascriptLink
6+
7+
from folium.map import Layer
8+
9+
from jinja2 import Template
10+
11+
12+
class FeatureGroupSubGroup(Layer):
13+
"""
14+
Creates a Feature Group that adds its child layers into a parent group when
15+
added to a map (e.g. through LayerControl). Useful to create nested groups,
16+
or cluster markers from multiple overlays. From [0].
17+
18+
[0] https://github.com/ghybs/Leaflet.FeatureGroup.SubGroup
19+
20+
Parameters
21+
----------
22+
group : Layer
23+
The MarkerCluster or FeatureGroup containing this subgroup.
24+
25+
name : string, default None
26+
The name of the Layer, as it will appear in LayerControls
27+
overlay : bool, default True
28+
Adds the layer as an optional overlay (True) or the base layer (False).
29+
control : bool, default True
30+
Whether the Layer will be included in LayerControls.
31+
show: bool, default True
32+
Whether the layer will be shown on opening (only for overlays).
33+
34+
Examples
35+
-------
36+
37+
Nested groups
38+
=============
39+
>>> fg = folium.FeatureGroup() # Main group
40+
>>> g1 = folium.plugins.FeatureGroupSubGroup(fg, 'g1') # First subgroup of fg
41+
>>> g2 = folium.plugins.FeatureGroupSubGroup(fg, 'g2') # Second subgroup of fg
42+
>>> m.add_child(fg)
43+
>>> m.add_child(g1)
44+
>>> m.add_child(g2)
45+
>>> g1.add_child(folium.Marker([0,0]))
46+
>>> g2.add_child(folium.Marker([0,1]))
47+
>>> l = folium.LayerControl().add_to(m)
48+
49+
Multiple overlays part of the same cluster group
50+
=====================================================
51+
>>> mcg = folium.plugins.MarkerCluster(control=False) # Marker Cluster, hidden in controls
52+
>>> g1 = folium.plugins.FeatureGroupSubGroup(mcg, 'g1') # First group, in mcg
53+
>>> g2 = folium.plugins.FeatureGroupSubGroup(mcg, 'g2') # Second group, in mcg
54+
>>> m.add_child(mcg)
55+
>>> m.add_child(g1)
56+
>>> m.add_child(g2)
57+
>>> g1.add_child(folium.Marker([0,0]))
58+
>>> g2.add_child(folium.Marker([0,1]))
59+
>>> l = folium.LayerControl().add_to(m)
60+
"""
61+
_template = Template(u"""
62+
{% macro script(this, kwargs) %}
63+
var {{this.get_name()}} = L.featureGroup.subGroup({{this._group.get_name()}});
64+
{{this.get_name()}}.addTo({{this._parent.get_name()}});
65+
{% endmacro %}
66+
""")
67+
68+
def __init__(self, group, name=None, overlay=True, control=True, show=True):
69+
super(FeatureGroupSubGroup, self).__init__(name=name, overlay=overlay,
70+
control=control, show=show)
71+
72+
self._group = group
73+
self._name = 'FeatureGroupSubGroup'
74+
75+
def render(self, **kwargs):
76+
super(FeatureGroupSubGroup, self).render(**kwargs)
77+
78+
figure = self.get_root()
79+
assert isinstance(figure, Figure), ('You cannot render this Element '
80+
'if it is not in a Figure.')
81+
82+
figure.header.add_child(
83+
JavascriptLink('https://unpkg.com/[email protected]/dist/leaflet.featuregroup.subgroup.js'), # noqa
84+
name='featuregroupsubgroupjs')

0 commit comments

Comments
 (0)