From f42613c01175dcdaa9f82294034d4bc09262c8f5 Mon Sep 17 00:00:00 2001 From: Frank Date: Tue, 27 Mar 2018 21:51:55 +0200 Subject: [PATCH 1/5] Addition to Map that keeps certain object on top --- folium/folium.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/folium/folium.py b/folium/folium.py index 4f34031e3a..46939b8580 100644 --- a/folium/folium.py +++ b/folium/folium.py @@ -216,6 +216,8 @@ def __init__(self, location=None, width='100%', height='100%', disable_3d ) + self.objects_to_stay_in_front = [] + if tiles: self.add_tile_layer( tiles=tiles, min_zoom=min_zoom, max_zoom=max_zoom, @@ -259,6 +261,14 @@ def __init__(self, location=None, width='100%', height='100%', crs: L.CRS.{{this.crs}} }); {% if this.control_scale %}L.control.scale().addTo({{this.get_name()}});{% endif %} + + {% if this.objects_to_stay_in_front %} + {{ this.get_name() }}.on("overlayadd", function(event) { + {% for obj in this.objects_to_stay_in_front %} + {{ obj.get_name() }}.bringToFront(); + {% endfor %} + }); + {% endif %} {% endmacro %} """) # noqa @@ -600,3 +610,20 @@ def highlight_function(x): caption=legend_name, ) self.add_child(color_scale) + + def keep_in_front(self, obj): + """Pass an object that must stay in front when overlays are toggled. + + Note that this does not put the object in front at load. That's up to + the ordering in which the user added them to the map. The last one added + is on top. + + Parameters + ---------- + obj : object + Any folium object that counts as an overlay. For example a + FeatureGroup or a vector object. + """ + self.objects_to_stay_in_front.append(obj) + + From 219ee66fcccd716c0df7ade97d00b03e6c0813f5 Mon Sep 17 00:00:00 2001 From: Frank Date: Mon, 2 Apr 2018 12:33:50 +0200 Subject: [PATCH 2/5] Put objects in front at page load --- folium/folium.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/folium/folium.py b/folium/folium.py index 46939b8580..bda435b187 100644 --- a/folium/folium.py +++ b/folium/folium.py @@ -263,11 +263,13 @@ def __init__(self, location=None, width='100%', height='100%', {% if this.control_scale %}L.control.scale().addTo({{this.get_name()}});{% endif %} {% if this.objects_to_stay_in_front %} - {{ this.get_name() }}.on("overlayadd", function(event) { + function objects_in_front() { {% for obj in this.objects_to_stay_in_front %} {{ obj.get_name() }}.bringToFront(); {% endfor %} - }); + }; + {{ this.get_name() }}.on("overlayadd", objects_in_front); + $(document).ready(objects_in_front); {% endif %} {% endmacro %} """) # noqa From 806fb32258dfc65a9fcc31a204c7394341d7e128 Mon Sep 17 00:00:00 2001 From: Frank Date: Mon, 2 Apr 2018 12:51:48 +0200 Subject: [PATCH 3/5] Accept multiple objects, better docstring --- folium/folium.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/folium/folium.py b/folium/folium.py index bda435b187..28c400c6e6 100644 --- a/folium/folium.py +++ b/folium/folium.py @@ -613,19 +613,18 @@ def highlight_function(x): ) self.add_child(color_scale) - def keep_in_front(self, obj): - """Pass an object that must stay in front when overlays are toggled. + def keep_in_front(self, *args): + """Pass one or multiples object that must stay in front. - Note that this does not put the object in front at load. That's up to - the ordering in which the user added them to the map. The last one added - is on top. + The ordering matters, the last one is put on top. Parameters ---------- - obj : object - Any folium object that counts as an overlay. For example a - FeatureGroup or a vector object. + *args : + Variable length argument list. Any folium object that counts as an + overlay. For example FeatureGroup or a vector object such as Marker. """ - self.objects_to_stay_in_front.append(obj) + for obj in args: + self.objects_to_stay_in_front.append(obj) From f01cee329d1ba7048255282a841f0b64a939d7bb Mon Sep 17 00:00:00 2001 From: Frank Date: Mon, 2 Apr 2018 13:04:59 +0200 Subject: [PATCH 4/5] Fix lint errors --- folium/folium.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/folium/folium.py b/folium/folium.py index 28c400c6e6..b783360c38 100644 --- a/folium/folium.py +++ b/folium/folium.py @@ -615,16 +615,14 @@ def highlight_function(x): def keep_in_front(self, *args): """Pass one or multiples object that must stay in front. - + The ordering matters, the last one is put on top. - + Parameters ---------- - *args : - Variable length argument list. Any folium object that counts as an + *args : + Variable length argument list. Any folium object that counts as an overlay. For example FeatureGroup or a vector object such as Marker. """ for obj in args: self.objects_to_stay_in_front.append(obj) - - From 37f14167008a6f446b1274b8868962eebe315244 Mon Sep 17 00:00:00 2001 From: Filipe Date: Mon, 7 May 2018 17:54:10 -0300 Subject: [PATCH 5/5] fix bad rebase --- folium/folium.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/folium/folium.py b/folium/folium.py index b499a4960b..d810f4a99e 100644 --- a/folium/folium.py +++ b/folium/folium.py @@ -561,10 +561,14 @@ def get_by_key(obj, key): '.'.join(key.split('.')[1:]))) def color_scale_fun(x): - return color_range[len( - [u for u in color_domain if - get_by_key(x, key_on) in color_data and - u <= color_data[get_by_key(x, key_on)]])] + idx = len( + [ + u for u in color_domain if + get_by_key(x, key_on) in color_data and + u <= color_data[get_by_key(x, key_on)] + ] + ) + return color_range[idx-1] else: def color_scale_fun(x): return fill_color