Skip to content

Commit 7d8e7c5

Browse files
authored
Merge pull request #449 from BibMartin/feature/color_line
Create features.ColorLine
2 parents 386900f + d4f2f62 commit 7d8e7c5

File tree

4 files changed

+187
-54
lines changed

4 files changed

+187
-54
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
~~~~~
33

44
- Add fullscreen plugin (sanga #437)
5+
- Add ColorLine object (bibmartin #449)
56

67
Bug Fixes
78

examples/Features.ipynb

Lines changed: 116 additions & 53 deletions
Large diffs are not rendered by default.

folium/features.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
none_min, none_max, iter_points,
1515
)
1616
from branca.element import Element, Figure, JavascriptLink, CssLink, MacroElement
17+
from branca.colormap import LinearColormap
1718

18-
from .map import Layer, Icon, Marker, Popup
19+
from .map import Layer, Icon, Marker, Popup, FeatureGroup
1920

2021

2122
class WmsTileLayer(Layer):
@@ -978,3 +979,57 @@ def __init__(self, icon_image, icon_size=None, icon_anchor=None,
978979
{{this._parent.get_name()}}.setIcon({{this.get_name()}});
979980
{% endmacro %}
980981
""") # noqa
982+
983+
984+
class ColorLine(FeatureGroup):
985+
"""Draw data on a map with specified colors.
986+
987+
Parameters
988+
----------
989+
positions: tuple or list
990+
The list of points latitude and longitude
991+
colors: tuple or list
992+
The list of segments colors.
993+
It must have length equal to `len(positions)-1`.
994+
colormap: branca.colormap.Colormap or list or tuple
995+
The colormap to use.
996+
If a list or tuple of colors is provided, a LinearColormap will be created
997+
from it.
998+
nb_steps: int, default 12
999+
To have lighter output, the colormap will be discretized to that number
1000+
of colors.
1001+
opacity: float, default 1
1002+
Line opacity, scale 0-1
1003+
weight: int, default 2
1004+
Stroke weight in pixels
1005+
**kwargs
1006+
Further parameters available. See folium.map.FeatureGroup
1007+
1008+
Returns
1009+
-------
1010+
A ColorLine object that you can `add_to` a Map.
1011+
"""
1012+
def __init__(self, positions, colors, colormap=None, nb_steps=12,
1013+
weight=None, opacity=None, **kwargs):
1014+
super(ColorLine, self).__init__(**kwargs)
1015+
self._name = 'ColorLine'
1016+
1017+
if colormap is None:
1018+
cm = LinearColormap(['green', 'yellow', 'red'],
1019+
vmin=min(colors),
1020+
vmax=max(colors),
1021+
).to_step(nb_steps)
1022+
elif isinstance(colormap, LinearColormap):
1023+
cm = colormap.to_step(nb_steps)
1024+
elif isinstance(colormap, list) or isinstance(colormap, tuple):
1025+
cm = LinearColormap(colormap,
1026+
vmin=min(colors),
1027+
vmax=max(colors),
1028+
).to_step(nb_steps)
1029+
else:
1030+
cm = colormap
1031+
out = {}
1032+
for (lat1, lng1), (lat2, lng2), color in zip(positions[:-1], positions[1:], colors):
1033+
out.setdefault(cm(color), []).append([[lat1, lng1], [lat2, lng2]])
1034+
for key, val in out.items():
1035+
self.add_child(MultiPolyLine(val, color=key, weight=weight, opacity=opacity))

tests/test_features.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,17 @@ def test_wms_service():
119119

120120
bounds = m.get_bounds()
121121
assert bounds == [[None, None], [None, None]], bounds
122+
123+
124+
# ColorLine.
125+
def test_color_line():
126+
m = Map([22.5, 22.5], zoom_start=3)
127+
color_line = features.ColorLine(
128+
[[0, 0], [0, 45], [45, 45], [45, 0], [0, 0]],
129+
[0, 1, 2, 3],
130+
colormap=['b', 'g', 'y', 'r'],
131+
nb_steps=4,
132+
weight=10,
133+
opacity=1)
134+
m.add_child(color_line)
135+
m._repr_html_()

0 commit comments

Comments
 (0)