|
14 | 14 | none_min, none_max, iter_points,
|
15 | 15 | )
|
16 | 16 | from branca.element import Element, Figure, JavascriptLink, CssLink, MacroElement
|
| 17 | +from branca.colormap import LinearColormap |
17 | 18 |
|
18 |
| -from .map import Layer, Icon, Marker, Popup |
| 19 | +from .map import Layer, Icon, Marker, Popup, FeatureGroup |
19 | 20 |
|
20 | 21 |
|
21 | 22 | class WmsTileLayer(Layer):
|
@@ -978,3 +979,57 @@ def __init__(self, icon_image, icon_size=None, icon_anchor=None,
|
978 | 979 | {{this._parent.get_name()}}.setIcon({{this.get_name()}});
|
979 | 980 | {% endmacro %}
|
980 | 981 | """) # 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)) |
0 commit comments