Skip to content

Commit d660a52

Browse files
author
Martin Journois
committed
Set get_bounds method
1 parent 0c02b72 commit d660a52

File tree

4 files changed

+77
-9
lines changed

4 files changed

+77
-9
lines changed

folium/element.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import base64
1414

1515
from .six import urlopen, text_type, binary_type
16-
from .utilities import _camelify, _parse_size
16+
from .utilities import _camelify, _parse_size, none_min, none_max
1717

1818

1919
ENV = Environment(loader=PackageLoader('folium', 'templates'))
@@ -40,6 +40,32 @@ def __init__(self, template=None, template_name=None):
4040
def get_name(self):
4141
return _camelify(self._name) + '_' + self._id
4242

43+
def _get_self_bounds(self):
44+
"""Computes the bounds of the object itself (not including it's children)
45+
in the form [[lat_min, lon_min], [lat_max, lon_max]]
46+
"""
47+
return [[None,None],[None,None]]
48+
49+
def get_bounds(self):
50+
"""Computes the bounds of the object and all it's children
51+
in the form [[lat_min, lon_min], [lat_max, lon_max]].
52+
"""
53+
bounds = self._get_self_bounds()
54+
55+
for child in self._children.values():
56+
child_bounds = child.get_bounds()
57+
bounds = [
58+
[
59+
none_min(bounds[0][0], child_bounds[0][0]),
60+
none_min(bounds[0][1], child_bounds[0][1]),
61+
],
62+
[
63+
none_max(bounds[1][0], child_bounds[1][0]),
64+
none_max(bounds[1][1], child_bounds[1][1]),
65+
],
66+
]
67+
return bounds
68+
4369
def add_children(self, child, name=None, index=None):
4470
"""Add a children."""
4571
if name is None:

folium/features.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ def render(self, **kwargs):
126126
JavascriptLink("https://cdnjs.cloudflare.com/ajax/libs/leaflet-dvf/0.2/leaflet-dvf.markers.min.js"), # noqa
127127
name='dvf_js')
128128

129-
130129
class Vega(Element):
131130
def __init__(self, data, width=None, height=None,
132131
left="0%", top="0%", position='relative'):
@@ -277,6 +276,12 @@ def style_data(self):
277276
self.style_function(feature))
278277
return json.dumps(self.data)
279278

279+
def _get_self_bounds(self):
280+
"""Computes the bounds of the object itself (not including it's children)
281+
in the form [[lat_min, lon_min], [lat_max, lon_max]]
282+
"""
283+
raise NotImplementedError
284+
280285
class TopoJson(MacroElement):
281286
def __init__(self, data, object_path):
282287
"""
@@ -315,6 +320,13 @@ def render(self, **kwargs):
315320
JavascriptLink("https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"), # noqa
316321
name='topojson')
317322

323+
def _get_self_bounds(self):
324+
"""Computes the bounds of the object itself (not including it's children)
325+
in the form [[lat_min, lon_min], [lat_max, lon_max]]
326+
"""
327+
raise NotImplementedError
328+
return [[self.location[0],self.location[1]],[self.location[0],self.location[1]]]
329+
318330
class ColorScale(MacroElement):
319331
def __init__(self, color_domain, color_code, caption=""):
320332
"""
@@ -344,7 +356,6 @@ def render(self, **kwargs):
344356
JavascriptLink("https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"), # noqa
345357
name='d3')
346358

347-
348359
class MarkerCluster(Layer):
349360
"""Adds a MarkerCluster layer on the map."""
350361
def __init__(self, overlay=True, control=True):
@@ -569,6 +580,11 @@ def __init__(self, locations, color=None, weight=None,
569580
{% endmacro %}
570581
""") # noqa
571582

583+
def _get_self_bounds(self):
584+
"""Computes the bounds of the object itself (not including it's children)
585+
in the form [[lat_min, lon_min], [lat_max, lon_max]]
586+
"""
587+
raise NotImplementedError
572588

573589
class MultiPolyLine(MacroElement):
574590
def __init__(self, locations, color=None, weight=None,
@@ -616,6 +632,11 @@ def __init__(self, locations, color=None, weight=None,
616632
{{this._parent.get_name()}}.addLayer({{this.get_name()}});
617633
{% endmacro %}
618634
""") # noqa
635+
def _get_self_bounds(self):
636+
"""Computes the bounds of the object itself (not including it's children)
637+
in the form [[lat_min, lon_min], [lat_max, lon_max]]
638+
"""
639+
raise NotImplementedError
619640

620641
class CustomIcon(Icon):
621642
def __init__(self, icon_image, icon_size=None, icon_anchor=None,

folium/map.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from .element import Element, Figure, MacroElement, Html
1818

19-
2019
class Map(MacroElement):
2120
def __init__(self, location=None, width='100%', height='100%',
2221
left="0%", top="0%", position='relative',
@@ -168,6 +167,12 @@ def add_tile_layer(self, tiles='OpenStreetMap', name=None,
168167
detect_retina=detect_retina)
169168
self.add_children(tile_layer, name=tile_layer.tile_name)
170169

170+
def _get_self_bounds(self):
171+
"""Computes the bounds of the object itself (not including it's children)
172+
in the form [[lat_min, lon_min], [lat_max, lon_max]]
173+
"""
174+
return [[self.location[0],self.location[1]],[self.location[0],self.location[1]]]
175+
171176
class Layer(MacroElement):
172177
"""An abstract class for everything that is a Layer on the map.
173178
It will be used to define whether an object will be included in LayerControls.
@@ -243,7 +248,6 @@ def __init__(self, tiles='OpenStreetMap', name=None,
243248
{% endmacro %}
244249
""")
245250

246-
247251
class FeatureGroup(Layer):
248252
def __init__(self, name=None, overlay=True, control=True):
249253
"""
@@ -273,7 +277,6 @@ def __init__(self, name=None, overlay=True, control=True):
273277
{% endmacro %}
274278
""")
275279

276-
277280
class LayerControl(MacroElement):
278281
"""Adds a layer control to the map."""
279282
def __init__(self):
@@ -374,7 +377,6 @@ def __init__(self, color='blue', icon_color='white', icon='info-sign',
374377
{% endmacro %}
375378
""")
376379

377-
378380
class Marker(MacroElement):
379381
def __init__(self, location, popup=None, icon=None):
380382
"""Create a simple stock Leaflet marker on the map, with optional
@@ -421,6 +423,11 @@ def __init__(self, location, popup=None, icon=None):
421423
{% endmacro %}
422424
""")
423425

426+
def _get_self_bounds(self):
427+
"""Computes the bounds of the object itself (not including it's children)
428+
in the form [[lat_min, lon_min], [lat_max, lon_max]]
429+
"""
430+
return [[self.location[0],self.location[1]],[self.location[0],self.location[1]]]
424431

425432
class Popup(Element):
426433
def __init__(self, html=None, max_width=300):
@@ -469,7 +476,6 @@ def render(self, **kwargs):
469476
self._template.render(this=self, kwargs=kwargs)),
470477
name=self.get_name())
471478

472-
473479
class FitBounds(MacroElement):
474480
def __init__(self, bounds, padding_top_left=None,
475481
padding_bottom_right=None, padding=None, max_zoom=None):
@@ -491,7 +497,6 @@ def __init__(self, bounds, padding_top_left=None,
491497
the same value.
492498
max_zoom: int, default None
493499
Maximum zoom to be used.
494-
495500
"""
496501
super(FitBounds, self).__init__()
497502
self._name = 'FitBounds'

folium/utilities.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,19 @@ def _locations_tolist(x):
428428
return list(map(_locations_tolist, x))
429429
else:
430430
return x
431+
432+
def none_min(x,y):
433+
if x is None:
434+
return y
435+
elif y is None:
436+
return x
437+
else:
438+
return min(x,y)
439+
440+
def none_max(x,y):
441+
if x is None:
442+
return y
443+
elif y is None:
444+
return x
445+
else:
446+
return max(x,y)

0 commit comments

Comments
 (0)