Skip to content

Commit 36514c2

Browse files
author
Martin Journois
committed
Add get_bounds in (almost) each plugin test
1 parent 3276d8d commit 36514c2

12 files changed

Lines changed: 95 additions & 14 deletions

folium/plugins/boat_marker.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010
import json
1111
from jinja2 import Template
1212

13-
from folium.element import JavascriptLink, MacroElement, Figure
13+
from folium.element import JavascriptLink, Figure
14+
from folium.map import Marker
1415

15-
16-
class BoatMarker(MacroElement):
16+
class BoatMarker(Marker):
1717
"""Adds a BoatMarker layer on the map."""
18-
def __init__(self, position=None, heading=0,
19-
wind_heading=None, wind_speed=0, **kwargs):
18+
def __init__(self, location, popup=None, icon=None,
19+
heading=0, wind_heading=None, wind_speed=0, **kwargs):
2020
"""Creates a BoatMarker plugin to append into a map with
2121
Map.add_plugin.
2222
2323
Parameters
2424
----------
25-
position: tuple of length 2, default None
25+
location: tuple of length 2, default None
2626
The latitude and longitude of the marker.
2727
If None, then the middle of the map is used.
2828
@@ -36,9 +36,8 @@ def __init__(self, position=None, heading=0,
3636
wind_speed: int, default 0
3737
Speed of the wind in knots.
3838
"""
39-
super(BoatMarker, self).__init__()
39+
super(BoatMarker, self).__init__(location, popup=popup, icon=icon)
4040
self._name = 'BoatMarker'
41-
self.position = None if position is None else tuple(position)
4241
self.heading = heading
4342
self.wind_heading = wind_heading
4443
self.wind_speed = wind_speed
@@ -47,7 +46,7 @@ def __init__(self, position=None, heading=0,
4746
self._template = Template(u"""
4847
{% macro script(this, kwargs) %}
4948
var {{this.get_name()}} = L.boatMarker(
50-
[{{this.position[0]}},{{this.position[1]}}],
49+
[{{this.location[0]}},{{this.location[1]}}],
5150
{{this.kwargs}}).addTo({{this._parent.get_name()}});
5251
{{this.get_name()}}.setHeadingWind({{this.heading}}, {{this.wind_speed}}, {{this.wind_heading}});
5352
{% endmacro %}

folium/plugins/heat_map.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from folium.element import JavascriptLink, Figure
1313
from folium.map import TileLayer
14-
14+
from folium.utilities import none_min, none_max
1515

1616
class HeatMap(TileLayer):
1717
def __init__(self, data, name=None, min_opacity=0.5, max_zoom=18,
@@ -79,3 +79,21 @@ def render(self, **kwargs):
7979
figure.header.add_children(
8080
JavascriptLink("https://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"), # noqa
8181
name='leaflet-heat.js')
82+
83+
def _get_self_bounds(self):
84+
"""Computes the bounds of the object itself (not including it's children)
85+
in the form [[lat_min, lon_min], [lat_max, lon_max]]
86+
"""
87+
bounds = [[None,None],[None,None]]
88+
for point in self.data:
89+
bounds = [
90+
[
91+
none_min(bounds[0][0], point[1]),
92+
none_min(bounds[0][1], point[0]),
93+
],
94+
[
95+
none_max(bounds[1][0], point[1]),
96+
none_max(bounds[1][1], point[0]),
97+
],
98+
]
99+
return bounds

folium/plugins/image_overlay.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,9 @@ def __init__(self, image, bounds, opacity=1., attr=None,
136136
).addTo({{this._parent.get_name()}});
137137
{% endmacro %}
138138
""")
139+
140+
def _get_self_bounds(self):
141+
"""Computes the bounds of the object itself (not including it's children)
142+
in the form [[lat_min, lon_min], [lat_max, lon_max]]
143+
"""
144+
return self.bounds

folium/plugins/timestamped_geo_json.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from jinja2 import Template
2424

2525
from folium.element import MacroElement, Figure, JavascriptLink, CssLink
26-
26+
from folium.utilities import none_min, none_max, iter_points
2727

2828
class TimestampedGeoJson(MacroElement):
2929
def __init__(self, data, transition_time=200, loop=True, auto_play=True):
@@ -87,10 +87,13 @@ def __init__(self, data, transition_time=200, loop=True, auto_play=True):
8787

8888
# self.template = self.env.get_template('timestamped_geo_json.tpl')
8989
if 'read' in dir(data):
90+
self.embed = True
9091
self.data = data.read()
9192
elif type(data) is dict:
93+
self.embed = True
9294
self.data = json.dumps(data)
9395
else:
96+
self.embed = False
9497
self.data = data
9598
self.transition_time = int(transition_time)
9699
self.loop = bool(loop)
@@ -145,3 +148,33 @@ def render(self, **kwargs):
145148
figure.header.add_children(
146149
CssLink("http://apps.socib.es/Leaflet.TimeDimension/dist/leaflet.timedimension.control.min.css"), # noqa
147150
name='leaflet.timedimension_css')
151+
152+
def _get_self_bounds(self):
153+
"""Computes the bounds of the object itself (not including it's children)
154+
in the form [[lat_min, lon_min], [lat_max, lon_max]]
155+
"""
156+
if not self.embed:
157+
raise ValueError('Cannot compute bounds of non-embedded GeoJSON.')
158+
159+
data = json.loads(self.data)
160+
if 'features' not in data.keys():
161+
# Catch case when GeoJSON is just a single Feature or a geometry.
162+
if not (isinstance(data, dict) and 'geometry' in data.keys()):
163+
# Catch case when GeoJSON is just a geometry.
164+
data = {'type' : 'Feature', 'geometry' : data}
165+
data = {'type' : 'FeatureCollection', 'features' : [data]}
166+
167+
bounds = [[None,None],[None,None]]
168+
for feature in data['features']:
169+
for point in iter_points(feature.get('geometry',{}).get('coordinates',{})):
170+
bounds = [
171+
[
172+
none_min(bounds[0][0], point[1]),
173+
none_min(bounds[0][1], point[0]),
174+
],
175+
[
176+
none_max(bounds[1][0], point[1]),
177+
none_max(bounds[1][1], point[0]),
178+
],
179+
]
180+
return bounds

tests/plugins/test_boat_marker.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ def test_boat_marker():
3838
# We verify that the script part is correct
3939
tmpl = Template("""
4040
var {{this.get_name()}} = L.boatMarker(
41-
[{{this.position[0]}},{{this.position[1]}}],
41+
[{{this.location[0]}},{{this.location[1]}}],
4242
{{this.kwargs}}).addTo({{this._parent.get_name()}});
4343
{{this.get_name()}}.setHeadingWind({{this.heading}}, {{this.wind_speed}}, {{this.wind_heading}});
4444
""")
4545

4646
assert tmpl.render(this=bm1) in out
4747
assert tmpl.render(this=bm2) in out
48+
49+
bounds = m.get_bounds()
50+
assert bounds == [[34, -43], [46, -30]], bounds

tests/plugins/test_heat_map.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from folium import plugins
1212

1313
def test_heat_map():
14+
np.random.seed(3141592)
1415
data = (np.random.normal(size=(100, 2)) * np.array([[1, 1]]) +
1516
np.array([[48, 5]])).tolist()
1617
m = folium.Map([48., 5.], tiles='stamentoner', zoom_start=6)
@@ -39,3 +40,7 @@ def test_heat_map():
3940
""")
4041

4142
assert tmpl.render(this=hm)
43+
44+
bounds = m.get_bounds()
45+
assert bounds == [[3.0302801394447734, 46.218566840847025],
46+
[7.132453997672826, 50.75345011431167]], bounds

tests/plugins/test_image_overlay.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ def test_image_overlay():
3535
).addTo({{this._parent.get_name()}});
3636
""")
3737
assert tmpl.render(this=io) in out
38+
39+
bounds = m.get_bounds()
40+
assert bounds == [[0, -180], [90, 180]], bounds

tests/plugins/test_marker_cluster.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
def test_marker_cluster():
1414
N = 100
15+
np.random.seed(seed=26082009)
1516
data = np.array([
1617
np.random.uniform(low=35, high=60, size=N), # Random latitudes.
1718
np.random.uniform(low=-12, high=30, size=N), # Random longitudes.
@@ -49,3 +50,7 @@ def test_marker_cluster():
4950
{% endfor %}
5051
""")
5152
assert ''.join(tmpl.render(this=mc).split()) in ''.join(out.split())
53+
54+
bounds = m.get_bounds()
55+
assert bounds == [[35.147332572663785, -11.520684337300109],
56+
[59.839718052359274, 29.94931046497927]], bounds

tests/plugins/test_scroll_zoom_toggler.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,7 @@ def test_scroll_zoom_toggler():
6060
6161
{{this._parent.get_name()}}.toggleScroll();
6262
""")
63-
assert ''.join(tmpl.render(this=szt).split()) in ''.join(out.split())
63+
assert ''.join(tmpl.render(this=szt).split()) in ''.join(out.split())
64+
65+
bounds = m.get_bounds()
66+
assert bounds == [[None, None], [None, None]], bounds

tests/plugins/test_terminator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ def test_terminator():
2020
# Verify that the script is okay.
2121
tmpl = Template('L.terminator().addTo({{this._parent.get_name()}});')
2222
assert ''.join(tmpl.render(this=t).split()) in ''.join(out.split())
23+
24+
bounds = m.get_bounds()
25+
assert bounds == [[None, None], [None, None]], bounds

0 commit comments

Comments
 (0)