|
| 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