Skip to content

Commit 89cf9d3

Browse files
committed
Merge pull request #242 from BibMartin/feature_group
Create and test FeatureGroup
2 parents 72c419b + 83206ef commit 89cf9d3

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

folium/map.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,34 @@ def __init__(self, tiles='OpenStreetMap', name=None,
217217
{% endmacro %}
218218
""")
219219

220+
class FeatureGroup(TileLayer):
221+
def __init__(self, name=None, overlay=True):
222+
"""Create a FeatureGroup layer ; you can put things in it and handle them as a
223+
single layer. For example, you can add a LayerControl to tick/untick the whole group.
224+
225+
Parameters
226+
----------
227+
name : str, default None
228+
The name of the featureGroup layer. It will be displayed in the LayerControl.
229+
If None, get_name() will be called to get the object's technical (ugly) name.
230+
overlay : bool, default True
231+
Whether your layer will be an overlay (ticked with a checkbox in LayerControls)
232+
or a base layer (ticked with a radio button).
233+
"""
234+
super(TileLayer, self).__init__()
235+
self._name = 'FeatureGroup'
236+
237+
self.tile_name = name if name is not None else self.get_name()
238+
239+
self.overlay = overlay
240+
241+
self._template = Template(u"""
242+
{% macro script(this, kwargs) %}
243+
var {{this.get_name()}} = L.featureGroup(
244+
).addTo({{this._parent.get_name()}});
245+
{% endmacro %}
246+
""")
247+
220248
class LayerControl(MacroElement):
221249
"""Adds a layer control to the map."""
222250
def __init__(self):

tests/test_folium.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from folium.six import PY3
2323
from folium.plugins import ScrollZoomToggler, MarkerCluster
2424
from folium.element import Html
25-
from folium.map import Popup, Marker, Icon, FitBounds
25+
from folium.map import Popup, Marker, Icon, FitBounds, FeatureGroup
2626
from folium.features import (DivIcon, CircleMarker, LatLngPopup, GeoJson,
2727
GeoJsonStyle, ColorScale, TopoJson, PolyLine,
2828
MultiPolyLine, ImageOverlay)
@@ -157,6 +157,18 @@ def test_wms_layer(self):
157157
assert (''.join(wms.split())[:-1] in
158158
''.join(map.get_root().render().split()))
159159

160+
def test_feature_group(self):
161+
"""Test FeatureGroup."""
162+
163+
map = folium.Map()
164+
feature_group = FeatureGroup()
165+
feature_group.add_children(Marker([45,-30],popup=Popup('-30')))
166+
feature_group.add_children(Marker([45, 30],popup=Popup('30')))
167+
map.add_children(feature_group)
168+
map.add_children(folium.map.LayerControl())
169+
170+
map._repr_html_()
171+
160172
def test_simple_marker(self):
161173
"""Test simple marker addition."""
162174

0 commit comments

Comments
 (0)