Skip to content

Commit 01b1689

Browse files
authored
Merge pull request #424 from sgvandijk/feature/leaflet-global-switches
Leaflet global switches
2 parents 19916a0 + 7e8d0b6 commit 01b1689

4 files changed

Lines changed: 72 additions & 2 deletions

File tree

folium/folium.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ class Map(LegacyMap):
7777
(going from bottom to top).
7878
control_scale : bool, default False
7979
Whether to add a control scale on the map.
80+
prefer_canvas : bool, default False
81+
Forces Leaflet to use the Canvas back-end (if available) for
82+
vector layers instead of SVG. This can increase performance
83+
considerably in some cases (e.g. many thousands of circle
84+
markers on the map).
85+
no_touch : bool, default False
86+
Forces Leaflet to not use touch events even if it detects them.
87+
disable_3d : bool, default False
88+
Forces Leaflet to not use hardware-accelerated CSS 3D
89+
transforms for positioning (which may cause glitches in some
90+
rare environments) even if they're supported.
8091
8192
Returns
8293
-------

folium/map.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ class LegacyMap(MacroElement):
118118
(going from bottom to top).
119119
control_scale : bool, default False
120120
Whether to add a control scale on the map.
121+
prefer_canvas : bool, default False
122+
Forces Leaflet to use the Canvas back-end (if available) for
123+
vector layers instead of SVG. This can increase performance
124+
considerably in some cases (e.g. many thousands of circle
125+
markers on the map).
126+
no_touch : bool, default False
127+
Forces Leaflet to not use touch events even if it detects them.
128+
disable_3d : bool, default False
129+
Forces Leaflet to not use hardware-accelerated CSS 3D
130+
transforms for positioning (which may cause glitches in some
131+
rare environments) even if they're supported.
121132
122133
Returns
123134
-------
@@ -142,7 +153,8 @@ def __init__(self, location=None, width='100%', height='100%',
142153
zoom_start=10, continuous_world=False, world_copy_jump=False,
143154
no_wrap=False, attr=None, min_lat=-90, max_lat=90,
144155
min_lon=-180, max_lon=180, max_bounds=True,
145-
detect_retina=False, crs='EPSG3857', control_scale=False):
156+
detect_retina=False, crs='EPSG3857', control_scale=False,
157+
prefer_canvas=False, no_touch=False, disable_3d=False):
146158
super(LegacyMap, self).__init__()
147159
self._name = 'Map'
148160
self._env = ENV
@@ -176,6 +188,8 @@ def __init__(self, location=None, width='100%', height='100%',
176188
self.crs = crs
177189
self.control_scale = control_scale
178190

191+
self.global_switches = GlobalSwitches(prefer_canvas, no_touch, disable_3d)
192+
179193
if tiles:
180194
self.add_tile_layer(
181195
tiles=tiles, min_zoom=min_zoom, max_zoom=max_zoom,
@@ -254,6 +268,9 @@ def render(self, **kwargs):
254268
assert isinstance(figure, Figure), ("You cannot render this Element "
255269
"if it's not in a Figure.")
256270

271+
# Set global switches
272+
figure.header.add_child(self.global_switches, name='global_switches')
273+
257274
# Import Javascripts
258275
for name, url in _default_js:
259276
figure.header.add_child(JavascriptLink(url), name=name)
@@ -284,6 +301,24 @@ def render(self, **kwargs):
284301
super(LegacyMap, self).render(**kwargs)
285302

286303

304+
class GlobalSwitches(Element):
305+
def __init__(self, prefer_canvas=False, no_touch=False, disable_3d=False):
306+
super(GlobalSwitches, self).__init__()
307+
self._name = 'GlobalSwitches'
308+
309+
self.prefer_canvas = prefer_canvas
310+
self.no_touch = no_touch
311+
self.disable_3d = disable_3d
312+
313+
self._template = Template(
314+
'<script>'
315+
'L_PREFER_CANVAS = {% if this.prefer_canvas %}true{% else %}false{% endif %}; '
316+
'L_NO_TOUCH = {% if this.no_touch %}true{% else %}false{% endif %}; '
317+
'L_DISABLE_3D = {% if this.disable_3d %}true{% else %}false{% endif %};'
318+
'</script>'
319+
)
320+
321+
287322
class Layer(MacroElement):
288323
"""An abstract class for everything that is a Layer on the map.
289324
It will be used to define whether an object will be included in

folium/templates/fol_template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<head>
33
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
4-
4+
<script>L_PREFER_CANVAS=false; L_NO_TOUCH=false; L_DISABLE_3D=false;</script>
55
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
66
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
77
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

tests/test_folium.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ def test_init(self):
8080
assert self.map.width == (900, 'px')
8181
assert self.map.left == (0, '%')
8282
assert self.map.top == (0, '%')
83+
assert self.map.global_switches.prefer_canvas is False
84+
assert self.map.global_switches.no_touch is False
85+
assert self.map.global_switches.disable_3d is False
8386
assert self.map.to_dict() == {
8487
"name": "Map",
8588
"id": "00000000000000000000000000000000",
@@ -422,3 +425,24 @@ def test_tile_layer(self):
422425

423426
bounds = self.map.get_bounds()
424427
assert bounds == [[None, None], [None, None]], bounds
428+
429+
def test_global_switches(self):
430+
mapa = folium.Map(prefer_canvas=True)
431+
assert (mapa.global_switches.prefer_canvas is True and
432+
mapa.global_switches.no_touch is False and
433+
mapa.global_switches.disable_3d is False)
434+
435+
mapb = folium.Map(no_touch=True)
436+
assert (mapb.global_switches.prefer_canvas is False and
437+
mapb.global_switches.no_touch is True and
438+
mapb.global_switches.disable_3d is False)
439+
440+
mapc = folium.Map(disable_3d=True)
441+
assert (mapc.global_switches.prefer_canvas is False and
442+
mapc.global_switches.no_touch is False and
443+
mapc.global_switches.disable_3d is True)
444+
445+
mapd = folium.Map(prefer_canvas=True, no_touch=True, disable_3d=True)
446+
assert (mapd.global_switches.prefer_canvas is True and
447+
mapd.global_switches.no_touch is True and
448+
mapd.global_switches.disable_3d is True)

0 commit comments

Comments
 (0)