Skip to content

Commit 21e2757

Browse files
committed
introduce free scale for geo_json
1 parent 41e8726 commit 21e2757

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

folium/folium.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ def geo_json(self, geo_path=None, geo_str=None, data_out='data.json',
737737
data=None, columns=None, key_on=None, threshold_scale=None,
738738
fill_color='blue', fill_opacity=0.6, line_color='black',
739739
line_weight=1, line_opacity=1, legend_name=None,
740-
topojson=None, reset=False):
740+
topojson=None, reset=False, freescale=False):
741741
"""Apply a GeoJSON overlay to the map.
742742
743743
Plot a GeoJSON overlay on the base map. There is no requirement
@@ -804,6 +804,9 @@ def geo_json(self, geo_path=None, geo_str=None, data_out='data.json',
804804
keyword argument will enable conversion to GeoJSON.
805805
reset: boolean, default False
806806
Remove all current geoJSON layers, start with new layer
807+
freescale: if True use free format for the scale, where min and max values
808+
are taken from the data. It also allow to plot allow to plot values < 0
809+
and float legend labels.
807810
808811
Output
809812
------
@@ -892,8 +895,9 @@ def json_style(style_cnt, line_color, line_weight, line_opacity,
892895

893896
# D3 Color scale.
894897
series = data[columns[1]]
895-
if threshold_scale and len(threshold_scale) > 6:
896-
raise ValueError
898+
if freescale == False:
899+
if threshold_scale and len(threshold_scale) > 6:
900+
raise ValueError
897901
domain = threshold_scale or utilities.split_six(series=series)
898902
if len(domain) > 253:
899903
raise ValueError('The threshold scale must be length <= 253')
@@ -913,9 +917,21 @@ def json_style(style_cnt, line_color, line_weight, line_opacity,
913917
# Create legend.
914918
name = legend_name or columns[1]
915919
leg_templ = self.env.get_template('d3_map_legend.js')
916-
legend = leg_templ.render({'lin_max': int(domain[-1]*1.1),
917-
'tick_labels': tick_labels,
918-
'caption': name})
920+
921+
if not freescale:
922+
legend = leg_templ.render({'lin_min': 0,
923+
'lin_max': int(domain[-1]*1.1),
924+
'tick_labels': tick_labels,
925+
'caption': name})
926+
927+
928+
else:
929+
legend = leg_templ.render({'lin_min': domain[0],
930+
'lin_max': domain[-1],
931+
'tick_labels': tick_labels,
932+
'caption': name})
933+
934+
919935
self.template_vars.setdefault('map_legends', []).append(legend)
920936

921937
# Style with color brewer colors.

folium/templates/d3_map_legend.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
legend.addTo(map);
66

77
var x = d3.scale.linear()
8-
.domain([0, {{ lin_max }}])
8+
.domain([{{ lin_min }}, {{ lin_max }}])
99
.range([0, 400]);
1010

1111
var xAxis = d3.svg.axis()

folium/utilities.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def type_check(value):
273273
return json_data
274274

275275

276-
def split_six(series=None):
276+
def split_six(series=None, freescale=False):
277277
"""
278278
Given a Pandas Series, get a domain of values from zero to the 90%
279279
quantile rounded to the nearest order-of-magnitude integer.
@@ -302,10 +302,14 @@ def base(x):
302302
else:
303303
return 0
304304

305-
quants = [0, 50, 75, 85, 90]
306305
# Some weirdness in series quantiles a la 0.13.
307306
arr = series.values
308-
return [base(np.percentile(arr, x)) for x in quants]
307+
if not freescale:
308+
quants = [0, 50, 75, 85, 90]
309+
return [base(np.percentile(arr, x)) for x in quants]
310+
else:
311+
quants = [0, 25, 50, 75, 85, 90]
312+
return [np.percentile(arr, x) for x in quants]
309313

310314

311315
def write_png(array):

0 commit comments

Comments
 (0)