Skip to content

Commit 562fe8c

Browse files
committed
Merge pull request #134 from themiurgo/autobounds
Autobounds
2 parents ed69f9e + 713c292 commit 562fe8c

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

folium/folium.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,14 @@ def __init__(self, location=None, width='100%', height='100%',
128128
self.json_data = {}
129129
self.plugins = {}
130130

131-
# Location.
132-
if not location:
133-
raise ValueError('You must pass a Lat/Lon location to initialize'
134-
' your map')
131+
# No location means we will use automatic bounds and ignore zoom
135132
self.location = location
136133

134+
# If location is not passed, we center the map at 0,0
135+
if not location:
136+
location = [0, 0]
137+
zoom_start = min_zoom
138+
137139
# Map Size Parameters.
138140
try:
139141
if isinstance(width, int):
@@ -651,6 +653,31 @@ def fit_bounds(self, bounds, padding_top_left=None,
651653
self.template_vars.update({'fit_bounds': fit_bounds_str})
652654

653655

656+
def _auto_bounds(self):
657+
if 'fit_bounds' in self.template_vars:
658+
return
659+
# Get count for each feature type
660+
ft_names = ["marker", "line", "circle", "polygon", "multiline"]
661+
ft_names = [i for i in ft_names if i in self.mark_cnt]
662+
663+
# Make a comprehensive list of all the features we want to fit
664+
feat_str = ["{name}_{count}".format(name=ft_name,
665+
count=self.mark_cnt[ft_name])
666+
for ft_name in ft_names
667+
for count in range(1, self.mark_cnt[ft_name]+1)
668+
]
669+
feat_str = "[" + ', '.join(feat_str) + "]"
670+
671+
fit_bounds = self.env.get_template('fit_bounds.js')
672+
fit_bounds_str = fit_bounds.render({
673+
'autobounds': not self.location,
674+
'features': feat_str,
675+
'fit_bounds_options': json.dumps({'padding' : [30, 30]}),
676+
})
677+
678+
self.template_vars.update({'fit_bounds': fit_bounds_str.strip()})
679+
680+
654681
def _popup_render(self, popup=None, mk_name=None, count=None,
655682
width=300):
656683
"""Popup renderer: either text or Vincent/Vega.
@@ -922,6 +949,7 @@ def json_style(style_cnt, line_color, line_weight, line_opacity,
922949
self.template_vars.setdefault('gjson_layers', []).append(layer)
923950

924951
def _build_map(self, html_templ=None, templ_type='string'):
952+
self._auto_bounds()
925953
"""Build HTML/JS/CSS from Templates given current map type."""
926954
if html_templ is None:
927955
map_types = {'base': 'fol_template.html',

folium/templates/fit_bounds.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
{% if autobounds %}
2+
var autobounds = L.featureGroup({{ features }}).getBounds()
3+
{% if not bounds %}
4+
{% set bounds = "autobounds" %}
5+
{% endif %}
6+
{% endif %}
7+
{% if bounds %}
18
map.fitBounds({{ bounds }},
29
{{ fit_bounds_options }}
310
);
11+
{% endif %}

folium/templates/fol_template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149

150150
{{ click_pop }}
151151

152-
{{ fit_bounds }}
152+
{% if fit_bounds %}{{ fit_bounds }}{% endif %}
153153

154154
</script>
155155

tests/folium_tests.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ def test_init(self):
5858
assert self.map.location == [45.5236, -122.6750]
5959
assert self.map.map_size == {'width': 900, 'height': 400}
6060

61-
nt.assert_raises(ValueError, callableObj=folium.Map)
62-
6361
tmpl = {'Tiles': 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
6462
'attr': ('Map data (c) <a href="http://openstreetmap.org">'
6563
'OpenStreetMap</a> contributors'),

0 commit comments

Comments
 (0)