Skip to content

Commit 21d8895

Browse files
Adapt velocity layer re-building the color_scale list from a linear colormap of Branca. This enables to easily use a ColormapControl with this layer.
1 parent d929208 commit 21d8895

File tree

2 files changed

+194
-19
lines changed

2 files changed

+194
-19
lines changed

examples/Velocity_with_colormap.ipynb

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"You will need to install the following packages:\n",
8+
"- `ipyleaflet`\n",
9+
"- `requests`\n",
10+
"- `xarray`\n",
11+
"- `netcdf4`"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Set up for JupyterLite\n",
21+
"try:\n",
22+
" import piplite\n",
23+
" await piplite.install(['ipyleaflet')\n",
24+
"except ImportError:\n",
25+
" pass"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 1,
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"import ipyleaflet\n",
35+
"from ipyleaflet.velocity import Velocity\n",
36+
"import xarray as xr\n",
37+
"from branca.colormap import linear"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 2,
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"center = [44.33956524809713, -130.60546875000003]\n",
47+
"zoom = 3\n",
48+
"m = ipyleaflet.Map(\n",
49+
" center=center,\n",
50+
" zoom=zoom,\n",
51+
" interpolation=\"nearest\",\n",
52+
" basemap=ipyleaflet.basemaps.CartoDB.DarkMatter,\n",
53+
")"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 3,
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"import os\n",
63+
"\n",
64+
"if not os.path.exists(\"wind-global.nc\"):\n",
65+
" url = \"https://github.com/benbovy/xvelmap/raw/master/notebooks/wind-global.nc\"\n",
66+
" import requests\n",
67+
"\n",
68+
" r = requests.get(url)\n",
69+
" wind_data = r.content\n",
70+
" with open(\"wind-global.nc\", \"wb\") as f:\n",
71+
" f.write(wind_data)"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": 4,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"ds = xr.open_dataset(\"wind-global.nc\")"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": 5,
86+
"metadata": {},
87+
"outputs": [],
88+
"source": [
89+
"colormap = linear.viridis"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 6,
95+
"metadata": {},
96+
"outputs": [],
97+
"source": [
98+
"display_options = {\n",
99+
" \"velocityType\": \"Global Wind\",\n",
100+
" \"displayPosition\": \"bottomleft\",\n",
101+
" \"displayEmptyString\": \"No wind data\",\n",
102+
"}\n",
103+
"wind = Velocity(\n",
104+
" data=ds,\n",
105+
" zonal_speed=\"u_wind\",\n",
106+
" meridional_speed=\"v_wind\",\n",
107+
" latitude_dimension=\"lat\",\n",
108+
" longitude_dimension=\"lon\",\n",
109+
" velocity_scale=0.01,\n",
110+
" max_velocity=20,\n",
111+
" colormap = colormap,\n",
112+
" display_options=display_options,\n",
113+
")"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 7,
119+
"metadata": {},
120+
"outputs": [
121+
{
122+
"data": {
123+
"application/vnd.jupyter.widget-view+json": {
124+
"model_id": "935083f2ac9046bb822298214e4f541d",
125+
"version_major": 2,
126+
"version_minor": 0
127+
},
128+
"text/plain": [
129+
"Map(center=[44.33956524809713, -130.60546875000003], controls=(ZoomControl(options=['position', 'zoom_in_text'…"
130+
]
131+
},
132+
"metadata": {},
133+
"output_type": "display_data"
134+
}
135+
],
136+
"source": [
137+
"colormap_control = ipyleaflet.ColormapControl(\n",
138+
" caption='Unemployment rate',\n",
139+
" colormap=colormap,\n",
140+
" value_min=wind.min_velocity,\n",
141+
" value_max=wind.max_velocity,\n",
142+
" position='topright',\n",
143+
" transparent_bg=False\n",
144+
")\n",
145+
"m.add(wind)\n",
146+
"m.add(colormap_control)"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": null,
152+
"metadata": {},
153+
"outputs": [],
154+
"source": []
155+
}
156+
],
157+
"metadata": {
158+
"kernelspec": {
159+
"display_name": "Python 3 (ipykernel)",
160+
"language": "python",
161+
"name": "python3"
162+
},
163+
"language_info": {
164+
"codemirror_mode": {
165+
"name": "ipython",
166+
"version": 3
167+
},
168+
"file_extension": ".py",
169+
"mimetype": "text/x-python",
170+
"name": "python",
171+
"nbconvert_exporter": "python",
172+
"pygments_lexer": "ipython3",
173+
"version": "3.9.13"
174+
}
175+
},
176+
"nbformat": 4,
177+
"nbformat_minor": 4
178+
}

ipyleaflet/velocity.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#
44

55
from traittypes import Dataset
6-
from traitlets import Unicode, Bool, Dict, Float, List
7-
6+
from traitlets import Unicode, Bool, Dict, Float, List, Any, default
87
from .leaflet import Layer
98
from .xarray_ds import ds_x_to_json
9+
from branca.colormap import linear
1010

1111

1212
class Velocity(Layer):
@@ -67,20 +67,17 @@ class Velocity(Layer):
6767
min_velocity = Float(0).tag(sync=True, o=True)
6868
max_velocity = Float(10).tag(sync=True, o=True)
6969
velocity_scale = Float(0.005).tag(sync=True, o=True)
70-
color_scale = List([
71-
"rgb(36,104, 180)",
72-
"rgb(60,157, 194)",
73-
"rgb(128,205,193)",
74-
"rgb(151,218,168)",
75-
"rgb(198,231,181)",
76-
"rgb(238,247,217)",
77-
"rgb(255,238,159)",
78-
"rgb(252,217,125)",
79-
"rgb(255,182,100)",
80-
"rgb(252,150,75)",
81-
"rgb(250,112,52)",
82-
"rgb(245,64,32)",
83-
"rgb(237,45,28)",
84-
"rgb(220,24,32)",
85-
"rgb(180,0,35)"
86-
]).tag(sync=True, o=True)
70+
colormap = Any(linear.YlOrRd_04)
71+
color_scale = List([]).tag(sync=True, o=True)
72+
73+
@default('color_scale')
74+
def _default_color_scale(self):
75+
self.color_scale = []
76+
77+
for i in range(len(self.colormap.colors)):
78+
rgb_tuple = tuple(str(int(x * 256)) for x in self.colormap.colors[i][:3])
79+
rgb_tuple_str = " , ".join(rgb_tuple)
80+
rgb_str = 'rgb(' + rgb_tuple_str + ')'
81+
self.color_scale.append(rgb_str)
82+
83+
return self.color_scale

0 commit comments

Comments
 (0)