Skip to content

Commit b8535f8

Browse files
author
Martin Journois
committed
Removed ColorScale in favor of colormap
1 parent b95aa21 commit b8535f8

5 files changed

Lines changed: 87 additions & 111 deletions

File tree

examples/GeoJSON and choropleth.ipynb

Lines changed: 61 additions & 40 deletions
Large diffs are not rendered by default.

folium/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
from folium.map import (FeatureGroup, FitBounds, Icon, LayerControl, Marker,
88
Popup, TileLayer)
99

10-
from folium.features import (ClickForMarker, ColorScale, CustomIcon, DivIcon,
10+
from folium.features import (ClickForMarker, CustomIcon, DivIcon,
1111
GeoJson, LatLngPopup,
1212
MarkerCluster, MultiPolyLine, PolyLine, Vega,
1313
RegularPolygonMarker, TopoJson, WmsTileLayer)
1414

15+
import folium.colormap as colormap
16+
1517
__version__ = '0.2.0.dev'
1618

1719
__all__ = ['Map',
@@ -25,7 +27,7 @@
2527
'Popup',
2628
'TileLayer',
2729
'ClickForMarker',
28-
'ColorScale',
30+
'colormap',
2931
'CustomIcon',
3032
'DivIcon',
3133
'GeoJson',

folium/features.py

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from jinja2 import Template
99
import json
1010

11-
from .utilities import (color_brewer, _parse_size, legend_scaler,
11+
from .utilities import (_parse_size,
1212
_locations_mirror, _locations_tolist, image_to_url,
1313
text_type, binary_type,
1414
none_min, none_max, iter_points,
@@ -487,36 +487,6 @@ def _get_self_bounds(self):
487487
]
488488

489489

490-
class ColorScale(MacroElement):
491-
def __init__(self, color_domain, color_code, caption=""):
492-
"""
493-
TODO docstring here
494-
495-
"""
496-
super(ColorScale, self).__init__()
497-
self._name = 'ColorScale'
498-
499-
self.color_domain = color_domain
500-
self.color_range = color_brewer(color_code, n=len(color_domain))
501-
self.tick_labels = legend_scaler(self.color_domain)
502-
503-
self.caption = caption
504-
self.fill_color = color_code
505-
506-
self._template = self._env.get_template('color_scale.js')
507-
508-
def render(self, **kwargs):
509-
super(ColorScale, self).render(**kwargs)
510-
511-
figure = self.get_root()
512-
assert isinstance(figure, Figure), ("You cannot render this Element "
513-
"if it's not in a Figure.")
514-
515-
figure.header.add_children(
516-
JavascriptLink("https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"), # noqa
517-
name='d3')
518-
519-
520490
class MarkerCluster(Layer):
521491
"""Adds a MarkerCluster layer on the map."""
522492
def __init__(self, name=None, overlay=True, control=True):

folium/folium.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
from .map import Icon, Marker, Popup, FitBounds
1818
from .features import (WmsTileLayer, RegularPolygonMarker, Vega, GeoJson,
1919
CircleMarker, LatLngPopup,
20-
ClickForMarker, ColorScale, TopoJson, PolyLine,
21-
MultiPolyLine)
20+
ClickForMarker, TopoJson, PolyLine, MultiPolyLine,
21+
)
22+
from .colormap import StepColormap
2223
from .utilities import color_brewer
2324

2425

@@ -422,29 +423,6 @@ def add_plugin(self, plugin):
422423
FutureWarning, stacklevel=2)
423424
self.add_children(plugin)
424425

425-
# def _auto_bounds(self):
426-
# if 'fit_bounds' in self.template_vars:
427-
# return
428-
# # Get count for each feature type
429-
# ft_names = ["marker", "line", "circle", "polygon", "multiline"]
430-
# ft_names = [i for i in ft_names if i in self.mark_cnt]
431-
#
432-
# # Make a comprehensive list of all the features we want to fit
433-
# feat_str = ["{name}_{count}".format(name=ft_name,
434-
# count=self.mark_cnt[ft_name])
435-
# for ft_name in ft_names for
436-
# count in range(1, self.mark_cnt[ft_name]+1)]
437-
# feat_str = "[" + ', '.join(feat_str) + "]"
438-
#
439-
# fit_bounds = self.env.get_template('fit_bounds.js')
440-
# fit_bounds_str = fit_bounds.render({
441-
# 'autobounds': not self.location,
442-
# 'features': feat_str,
443-
# 'fit_bounds_options': json.dumps({'padding': [30, 30]}),
444-
# })
445-
#
446-
# self.template_vars.update({'fit_bounds': fit_bounds_str.strip()})
447-
448426
def geo_json(self, *args, **kwargs):
449427
"""This method is deprecated and will be removed in v0.2.1. See
450428
`Map.choropleth` instead.
@@ -626,10 +604,15 @@ def style_function(x):
626604

627605
self.add_children(geo_json)
628606

629-
# Create ColorScale.
607+
# Create ColorMap.
630608
if color_domain:
631-
color_scale = ColorScale(color_domain, fill_color,
632-
caption=legend_name)
609+
brewed = color_brewer(fill_color, n=len(color_domain))
610+
color_scale = StepColormap(
611+
brewed[1:len(color_domain)],
612+
index=color_domain,
613+
vmin=color_domain[0],
614+
vmax=color_domain[-1],
615+
)
633616
self.add_children(color_scale)
634617

635618
def image_overlay(self, data, opacity=0.25, min_lat=-90.0, max_lat=90.0,

tests/test_folium.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import base64
2222
from folium.six import PY3
2323
from folium.map import Popup, Marker, FitBounds, FeatureGroup
24-
from folium.features import (GeoJson, ColorScale, TopoJson, PolyLine,
25-
MultiPolyLine)
24+
from folium.features import GeoJson, TopoJson, PolyLine, MultiPolyLine
25+
from folium.colormap import ColorMap
2626
from folium.plugins import ImageOverlay
2727

2828
rootpath = os.path.abspath(os.path.dirname(__file__))
@@ -450,12 +450,12 @@ def test_geo_json_data_binding(self):
450450
path = os.path.join(rootpath, 'us-counties.json')
451451

452452
# With DataFrame data binding, default threshold scale.
453-
self.map.geo_json(geo_path=path, data=data,
454-
threshold_scale=[4.0, 1000.0, 3000.0,
455-
5000.0, 9000.0],
456-
columns=['FIPS_Code', 'Unemployed_2011'],
457-
key_on='feature.id', fill_color='YlGnBu',
458-
reset=True)
453+
self.map.choropleth(geo_path=path, data=data,
454+
threshold_scale=[4.0, 1000.0, 3000.0,
455+
5000.0, 9000.0],
456+
columns=['FIPS_Code', 'Unemployed_2011'],
457+
key_on='feature.id', fill_color='YlGnBu',
458+
reset=True)
459459

460460
out = self.map._parent.render()
461461

@@ -464,8 +464,8 @@ def test_geo_json_data_binding(self):
464464
palette = folium.utilities.color_brewer('YlGnBu')
465465
d3range = palette[0: len(domain) + 2]
466466
colorscale_obj = [val for key, val in self.map._children.items() if
467-
isinstance(val, ColorScale)][0]
468-
colorscale_temp = self.env.get_template('d3_threshold.js')
467+
isinstance(val, ColorMap)][0]
468+
colorscale_temp = self.env.get_template('color_scale.js')
469469
colorscale = colorscale_temp.render({
470470
'this': colorscale_obj,
471471
'domain': domain,

0 commit comments

Comments
 (0)