From 48a3804f89582f514183ad8595d23d196c994a05 Mon Sep 17 00:00:00 2001 From: Florence Haudin Date: Thu, 4 Aug 2022 15:56:03 +0200 Subject: [PATCH 01/13] 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. --- examples/Velocity_with_colormap.ipynb | 178 ++++++++++++++++++++++++++ ipyleaflet/velocity.py | 35 +++-- 2 files changed, 194 insertions(+), 19 deletions(-) create mode 100644 examples/Velocity_with_colormap.ipynb diff --git a/examples/Velocity_with_colormap.ipynb b/examples/Velocity_with_colormap.ipynb new file mode 100644 index 000000000..dd16f87d2 --- /dev/null +++ b/examples/Velocity_with_colormap.ipynb @@ -0,0 +1,178 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will need to install the following packages:\n", + "- `ipyleaflet`\n", + "- `requests`\n", + "- `xarray`\n", + "- `netcdf4`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Set up for JupyterLite\n", + "try:\n", + " import piplite\n", + " await piplite.install(['ipyleaflet')\n", + "except ImportError:\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import ipyleaflet\n", + "from ipyleaflet.velocity import Velocity\n", + "import xarray as xr\n", + "from branca.colormap import linear" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "center = [44.33956524809713, -130.60546875000003]\n", + "zoom = 3\n", + "m = ipyleaflet.Map(\n", + " center=center,\n", + " zoom=zoom,\n", + " interpolation=\"nearest\",\n", + " basemap=ipyleaflet.basemaps.CartoDB.DarkMatter,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "\n", + "if not os.path.exists(\"wind-global.nc\"):\n", + " url = \"https://github.com/benbovy/xvelmap/raw/master/notebooks/wind-global.nc\"\n", + " import requests\n", + "\n", + " r = requests.get(url)\n", + " wind_data = r.content\n", + " with open(\"wind-global.nc\", \"wb\") as f:\n", + " f.write(wind_data)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "ds = xr.open_dataset(\"wind-global.nc\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "colormap = linear.viridis" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "display_options = {\n", + " \"velocityType\": \"Global Wind\",\n", + " \"displayPosition\": \"bottomleft\",\n", + " \"displayEmptyString\": \"No wind data\",\n", + "}\n", + "wind = Velocity(\n", + " data=ds,\n", + " zonal_speed=\"u_wind\",\n", + " meridional_speed=\"v_wind\",\n", + " latitude_dimension=\"lat\",\n", + " longitude_dimension=\"lon\",\n", + " velocity_scale=0.01,\n", + " max_velocity=20,\n", + " colormap = colormap,\n", + " display_options=display_options,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "935083f2ac9046bb822298214e4f541d", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Map(center=[44.33956524809713, -130.60546875000003], controls=(ZoomControl(options=['position', 'zoom_in_text'…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "colormap_control = ipyleaflet.ColormapControl(\n", + " caption='Unemployment rate',\n", + " colormap=colormap,\n", + " value_min=wind.min_velocity,\n", + " value_max=wind.max_velocity,\n", + " position='topright',\n", + " transparent_bg=False\n", + ")\n", + "m.add(wind)\n", + "m.add(colormap_control)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/ipyleaflet/velocity.py b/ipyleaflet/velocity.py index 3197ce9f5..33811da3c 100644 --- a/ipyleaflet/velocity.py +++ b/ipyleaflet/velocity.py @@ -3,10 +3,10 @@ # from traittypes import Dataset -from traitlets import Unicode, Bool, Dict, Float, List - +from traitlets import Unicode, Bool, Dict, Float, List, Any, default from .leaflet import Layer from .xarray_ds import ds_x_to_json +from branca.colormap import linear class Velocity(Layer): @@ -67,20 +67,17 @@ class Velocity(Layer): min_velocity = Float(0).tag(sync=True, o=True) max_velocity = Float(10).tag(sync=True, o=True) velocity_scale = Float(0.005).tag(sync=True, o=True) - color_scale = List([ - "rgb(36,104, 180)", - "rgb(60,157, 194)", - "rgb(128,205,193)", - "rgb(151,218,168)", - "rgb(198,231,181)", - "rgb(238,247,217)", - "rgb(255,238,159)", - "rgb(252,217,125)", - "rgb(255,182,100)", - "rgb(252,150,75)", - "rgb(250,112,52)", - "rgb(245,64,32)", - "rgb(237,45,28)", - "rgb(220,24,32)", - "rgb(180,0,35)" - ]).tag(sync=True, o=True) + colormap = Any(linear.YlOrRd_04) + color_scale = List([]).tag(sync=True, o=True) + + @default('color_scale') + def _default_color_scale(self): + self.color_scale = [] + + for i in range(len(self.colormap.colors)): + rgb_tuple = tuple(str(int(x * 256)) for x in self.colormap.colors[i][:3]) + rgb_tuple_str = " , ".join(rgb_tuple) + rgb_str = 'rgb(' + rgb_tuple_str + ')' + self.color_scale.append(rgb_str) + + return self.color_scale From e4f15545174c5434d73718d24d811a3ae6b78319 Mon Sep 17 00:00:00 2001 From: HaudinFlorence <99649086+HaudinFlorence@users.noreply.github.com> Date: Thu, 4 Aug 2022 16:52:51 +0200 Subject: [PATCH 02/13] Update ipyleaflet/velocity.py Co-authored-by: David Brochart --- ipyleaflet/velocity.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ipyleaflet/velocity.py b/ipyleaflet/velocity.py index 33811da3c..f33464ed1 100644 --- a/ipyleaflet/velocity.py +++ b/ipyleaflet/velocity.py @@ -74,10 +74,9 @@ class Velocity(Layer): def _default_color_scale(self): self.color_scale = [] - for i in range(len(self.colormap.colors)): - rgb_tuple = tuple(str(int(x * 256)) for x in self.colormap.colors[i][:3]) - rgb_tuple_str = " , ".join(rgb_tuple) - rgb_str = 'rgb(' + rgb_tuple_str + ')' + for color in self.colormap.colors: + rgb_tuple = tuple(int(x * 256) for x in color[:3]) + rgb_str = f'rgb({" , ".join(rgb_tuple)})' self.color_scale.append(rgb_str) return self.color_scale From 33f2e0d4f9a0eea41ada958fdbd554d9b09f4858 Mon Sep 17 00:00:00 2001 From: Florence Haudin Date: Thu, 11 Aug 2022 11:55:18 +0200 Subject: [PATCH 03/13] Fix a bug in defining rgb_str in velocity.py. --- examples/Velocity_with_colormap.ipynb | 41 +++++++-------------------- ipyleaflet/velocity.py | 2 +- 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/examples/Velocity_with_colormap.ipynb b/examples/Velocity_with_colormap.ipynb index dd16f87d2..1a0a79f6a 100644 --- a/examples/Velocity_with_colormap.ipynb +++ b/examples/Velocity_with_colormap.ipynb @@ -27,7 +27,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -39,7 +39,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -82,7 +82,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -91,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -115,24 +115,9 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "935083f2ac9046bb822298214e4f541d", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Map(center=[44.33956524809713, -130.60546875000003], controls=(ZoomControl(options=['position', 'zoom_in_text'…" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "colormap_control = ipyleaflet.ColormapControl(\n", " caption='Unemployment rate',\n", @@ -143,15 +128,9 @@ " transparent_bg=False\n", ")\n", "m.add(wind)\n", - "m.add(colormap_control)" + "m.add(colormap_control)\n", + "m" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/ipyleaflet/velocity.py b/ipyleaflet/velocity.py index f33464ed1..c6e34ffca 100644 --- a/ipyleaflet/velocity.py +++ b/ipyleaflet/velocity.py @@ -76,7 +76,7 @@ def _default_color_scale(self): for color in self.colormap.colors: rgb_tuple = tuple(int(x * 256) for x in color[:3]) - rgb_str = f'rgb({" , ".join(rgb_tuple)})' + rgb_str = f'rgb{str(rgb_tuple)}' self.color_scale.append(rgb_str) return self.color_scale From 1b04500f924cafb240537a540a8f116b02e7f006 Mon Sep 17 00:00:00 2001 From: HaudinFlorence <99649086+HaudinFlorence@users.noreply.github.com> Date: Thu, 11 Aug 2022 12:07:28 +0200 Subject: [PATCH 04/13] Update ipyleaflet/velocity.py Co-authored-by: David Brochart --- ipyleaflet/velocity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipyleaflet/velocity.py b/ipyleaflet/velocity.py index c6e34ffca..54cccb71b 100644 --- a/ipyleaflet/velocity.py +++ b/ipyleaflet/velocity.py @@ -76,7 +76,7 @@ def _default_color_scale(self): for color in self.colormap.colors: rgb_tuple = tuple(int(x * 256) for x in color[:3]) - rgb_str = f'rgb{str(rgb_tuple)}' + rgb_str = f"rgb{rgb_tuple}" self.color_scale.append(rgb_str) return self.color_scale From 0df1c37ea76e4c50fccd19f55395a86d6539ce4d Mon Sep 17 00:00:00 2001 From: HaudinFlorence <99649086+HaudinFlorence@users.noreply.github.com> Date: Thu, 11 Aug 2022 14:09:15 +0200 Subject: [PATCH 05/13] Update examples/Velocity_with_colormap.ipynb Co-authored-by: David Brochart --- examples/Velocity_with_colormap.ipynb | 2 +- ipyleaflet/velocity.py | 31 ++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/examples/Velocity_with_colormap.ipynb b/examples/Velocity_with_colormap.ipynb index 1a0a79f6a..fb1174c5e 100644 --- a/examples/Velocity_with_colormap.ipynb +++ b/examples/Velocity_with_colormap.ipynb @@ -20,7 +20,7 @@ "# Set up for JupyterLite\n", "try:\n", " import piplite\n", - " await piplite.install(['ipyleaflet')\n", + " await piplite.install('ipyleaflet')\n", "except ImportError:\n", " pass" ] diff --git a/ipyleaflet/velocity.py b/ipyleaflet/velocity.py index 54cccb71b..78c3853ca 100644 --- a/ipyleaflet/velocity.py +++ b/ipyleaflet/velocity.py @@ -68,7 +68,25 @@ class Velocity(Layer): max_velocity = Float(10).tag(sync=True, o=True) velocity_scale = Float(0.005).tag(sync=True, o=True) colormap = Any(linear.YlOrRd_04) - color_scale = List([]).tag(sync=True, o=True) + #color_scale = List([]).tag(sync=True, o=True) + colors = [ + "rgb(36,104, 180)", + "rgb(60,157, 194)", + "rgb(128,205,193)", + "rgb(151,218,168)", + "rgb(198,231,181)", + "rgb(238,247,217)", + "rgb(255,238,159)", + "rgb(252,217,125)", + "rgb(255,182,100)", + "rgb(252,150,75)", + "rgb(250,112,52)", + "rgb(245,64,32)", + "rgb(237,45,28)", + "rgb(220,24,32)", + "rgb(180,0,35)" + ] + color_scale = List(colors).tag(sync=True, o=True) @default('color_scale') def _default_color_scale(self): @@ -80,3 +98,14 @@ def _default_color_scale(self): self.color_scale.append(rgb_str) return self.color_scale + + @default('colormap') + def _default_colormap(self): + + + for color in self.colormap.colors: + rgb_tuple = tuple(int(x * 256) for x in color[:3]) + rgb_str = f"rgb{rgb_tuple}" + self.color_scale.append(rgb_str) + + return self.color_scale \ No newline at end of file From 75f3f86698344fd73077f3f9846fcc7360c537d6 Mon Sep 17 00:00:00 2001 From: Florence Haudin Date: Fri, 12 Aug 2022 11:59:07 +0200 Subject: [PATCH 06/13] Change color_scale into a private attribute, listen to its change in in Velocity.js, change default colormap to OrRd_06 to fit other default values in ipyleaflet, update docstrings. --- examples/Velocity_with_colormap.ipynb | 15 ++------- ipyleaflet/velocity.py | 48 ++++++--------------------- js/src/layers/Velocity.js | 10 +++++- 3 files changed, 23 insertions(+), 50 deletions(-) diff --git a/examples/Velocity_with_colormap.ipynb b/examples/Velocity_with_colormap.ipynb index fb1174c5e..ceffacbfe 100644 --- a/examples/Velocity_with_colormap.ipynb +++ b/examples/Velocity_with_colormap.ipynb @@ -20,7 +20,7 @@ "# Set up for JupyterLite\n", "try:\n", " import piplite\n", - " await piplite.install('ipyleaflet')\n", + " await piplite.install(['ipyleaflet')\n", "except ImportError:\n", " pass" ] @@ -80,15 +80,6 @@ "ds = xr.open_dataset(\"wind-global.nc\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "colormap = linear.viridis" - ] - }, { "cell_type": "code", "execution_count": null, @@ -108,7 +99,7 @@ " longitude_dimension=\"lon\",\n", " velocity_scale=0.01,\n", " max_velocity=20,\n", - " colormap = colormap,\n", + " colormap = linear.viridis,\n", " display_options=display_options,\n", ")" ] @@ -121,7 +112,7 @@ "source": [ "colormap_control = ipyleaflet.ColormapControl(\n", " caption='Unemployment rate',\n", - " colormap=colormap,\n", + " colormap=wind.colormap,\n", " value_min=wind.min_velocity,\n", " value_max=wind.max_velocity,\n", " position='topright',\n", diff --git a/ipyleaflet/velocity.py b/ipyleaflet/velocity.py index 78c3853ca..62e03023f 100644 --- a/ipyleaflet/velocity.py +++ b/ipyleaflet/velocity.py @@ -38,8 +38,11 @@ class Velocity(Layer): Used to align color scale. velocity_scale: float, 0.005 To be modified for particle animations. - color_scale: array, default [] - Array of hex/rgb colors for user-specified color scale. + branca.colormap.LinearColorMap instance, default linear.OrRd_06 + The colormap used for displaying the Velocity data + _color_scale: array, default [] + Array of hex/rgb colors for user-specified color scale, it is private and defined from the colormap. + """ _view_name = Unicode('LeafletVelocityView').tag(sync=True) @@ -67,45 +70,16 @@ class Velocity(Layer): min_velocity = Float(0).tag(sync=True, o=True) max_velocity = Float(10).tag(sync=True, o=True) velocity_scale = Float(0.005).tag(sync=True, o=True) - colormap = Any(linear.YlOrRd_04) - #color_scale = List([]).tag(sync=True, o=True) - colors = [ - "rgb(36,104, 180)", - "rgb(60,157, 194)", - "rgb(128,205,193)", - "rgb(151,218,168)", - "rgb(198,231,181)", - "rgb(238,247,217)", - "rgb(255,238,159)", - "rgb(252,217,125)", - "rgb(255,182,100)", - "rgb(252,150,75)", - "rgb(250,112,52)", - "rgb(245,64,32)", - "rgb(237,45,28)", - "rgb(220,24,32)", - "rgb(180,0,35)" - ] - color_scale = List(colors).tag(sync=True, o=True) + colormap = Any(linear.OrRd_06) + _color_scale = List([]).tag(sync=True, o=True) - @default('color_scale') + @default('_color_scale') def _default_color_scale(self): - self.color_scale = [] - - for color in self.colormap.colors: - rgb_tuple = tuple(int(x * 256) for x in color[:3]) - rgb_str = f"rgb{rgb_tuple}" - self.color_scale.append(rgb_str) - - return self.color_scale - - @default('colormap') - def _default_colormap(self): - + self._color_scale = [] for color in self.colormap.colors: rgb_tuple = tuple(int(x * 256) for x in color[:3]) rgb_str = f"rgb{rgb_tuple}" - self.color_scale.append(rgb_str) + self._color_scale.append(rgb_str) - return self.color_scale \ No newline at end of file + return self._color_scale diff --git a/js/src/layers/Velocity.js b/js/src/layers/Velocity.js index 30663d37c..908d6e7be 100644 --- a/js/src/layers/Velocity.js +++ b/js/src/layers/Velocity.js @@ -25,7 +25,7 @@ export class LeafletVelocityModel extends layer.LeafletLayerModel { minVelocity: 0, maxVelocity: 10, velocityScale: 0.005, - colorScale: [], + _colorScale: [] }; } } @@ -60,5 +60,13 @@ export class LeafletVelocityView extends layer.LeafletLayerView { }, this ); + this.listenTo( + this.model, + 'change:_colorScale', + function () { + this.layer_views.update(this.model.get('_colorScale')); + }, + this + ); } } From f3340e0990bd8fb8194e7ff20335a13fb97e91c5 Mon Sep 17 00:00:00 2001 From: Florence Haudin Date: Fri, 12 Aug 2022 16:27:52 +0200 Subject: [PATCH 07/13] Revert the change of making color_scale a private attribute that is observed in Velocity.js. --- examples/Velocity_with_colormap.ipynb | 2 +- ipyleaflet/velocity.py | 10 +++++----- js/src/layers/Velocity.js | 10 +--------- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/examples/Velocity_with_colormap.ipynb b/examples/Velocity_with_colormap.ipynb index ceffacbfe..94983ffbc 100644 --- a/examples/Velocity_with_colormap.ipynb +++ b/examples/Velocity_with_colormap.ipynb @@ -20,7 +20,7 @@ "# Set up for JupyterLite\n", "try:\n", " import piplite\n", - " await piplite.install(['ipyleaflet')\n", + " await piplite.install('ipyleaflet')\n", "except ImportError:\n", " pass" ] diff --git a/ipyleaflet/velocity.py b/ipyleaflet/velocity.py index 62e03023f..559edade1 100644 --- a/ipyleaflet/velocity.py +++ b/ipyleaflet/velocity.py @@ -71,15 +71,15 @@ class Velocity(Layer): max_velocity = Float(10).tag(sync=True, o=True) velocity_scale = Float(0.005).tag(sync=True, o=True) colormap = Any(linear.OrRd_06) - _color_scale = List([]).tag(sync=True, o=True) + color_scale = List([]).tag(sync=True, o=True) - @default('_color_scale') + @default('color_scale') def _default_color_scale(self): - self._color_scale = [] + self.color_scale = [] for color in self.colormap.colors: rgb_tuple = tuple(int(x * 256) for x in color[:3]) rgb_str = f"rgb{rgb_tuple}" - self._color_scale.append(rgb_str) + self.color_scale.append(rgb_str) - return self._color_scale + return self.color_scale diff --git a/js/src/layers/Velocity.js b/js/src/layers/Velocity.js index 908d6e7be..b605ce964 100644 --- a/js/src/layers/Velocity.js +++ b/js/src/layers/Velocity.js @@ -25,7 +25,7 @@ export class LeafletVelocityModel extends layer.LeafletLayerModel { minVelocity: 0, maxVelocity: 10, velocityScale: 0.005, - _colorScale: [] + colorScale: [] }; } } @@ -60,13 +60,5 @@ export class LeafletVelocityView extends layer.LeafletLayerView { }, this ); - this.listenTo( - this.model, - 'change:_colorScale', - function () { - this.layer_views.update(this.model.get('_colorScale')); - }, - this - ); } } From 6dec92f9ccaeb2e94607d9aa080b51b39b89b33d Mon Sep 17 00:00:00 2001 From: Florence Haudin Date: Fri, 23 Sep 2022 17:27:38 +0200 Subject: [PATCH 08/13] Rebase with master and update to include the logics of subitems, namely a colormap subitem attached to the velocity layer. --- examples/Velocity_with_colormap.ipynb | 12 ++++-- ipyleaflet/velocity.py | 54 ++++++++++++++++++--------- js/src/layers/Velocity.js | 3 +- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/examples/Velocity_with_colormap.ipynb b/examples/Velocity_with_colormap.ipynb index 94983ffbc..9824f9a14 100644 --- a/examples/Velocity_with_colormap.ipynb +++ b/examples/Velocity_with_colormap.ipynb @@ -116,12 +116,18 @@ " value_min=wind.min_velocity,\n", " value_max=wind.max_velocity,\n", " position='topright',\n", - " transparent_bg=False\n", + " transparent_bg=False,\n", ")\n", "m.add(wind)\n", - "m.add(colormap_control)\n", "m" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -140,7 +146,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.12" } }, "nbformat": 4, diff --git a/ipyleaflet/velocity.py b/ipyleaflet/velocity.py index 559edade1..901ed0be4 100644 --- a/ipyleaflet/velocity.py +++ b/ipyleaflet/velocity.py @@ -4,7 +4,7 @@ from traittypes import Dataset from traitlets import Unicode, Bool, Dict, Float, List, Any, default -from .leaflet import Layer +from .leaflet import Layer, ColormapControl from .xarray_ds import ds_x_to_json from branca.colormap import linear @@ -45,35 +45,42 @@ class Velocity(Layer): """ - _view_name = Unicode('LeafletVelocityView').tag(sync=True) - _model_name = Unicode('LeafletVelocityModel').tag(sync=True) + _view_name = Unicode("LeafletVelocityView").tag(sync=True) + _model_name = Unicode("LeafletVelocityModel").tag(sync=True) - zonal_speed = Unicode('', help='Name of the zonal speed in the dataset') - meridional_speed = Unicode('', help='Name of the meridional speed in the dataset') - latitude_dimension = Unicode('latitude', help='Name of the latitude dimension in the dataset') - longitude_dimension = Unicode('longitude', help='Name of the longitude dimension in the dataset') + zonal_speed = Unicode("", help="Name of the zonal speed in the dataset") + meridional_speed = Unicode("", help="Name of the meridional speed in the dataset") + latitude_dimension = Unicode( + "latitude", help="Name of the latitude dimension in the dataset" + ) + longitude_dimension = Unicode( + "longitude", help="Name of the longitude dimension in the dataset" + ) units = Unicode(None, allow_none=True) data = Dataset().tag(dtype=None, sync=True, to_json=ds_x_to_json) # Options display_values = Bool(True).tag(sync=True, o=True) - display_options = Dict({ - 'velocityType': 'Global Wind', - 'position': 'bottomleft', - 'emptyString': 'No velocity data', - 'angleConvention': 'bearingCW', - 'displayPosition': 'bottomleft', - 'displayEmptyString': 'No velocity data', - 'speedUnit': 'kt' - }).tag(sync=True) + display_options = Dict( + { + "velocityType": "Global Wind", + "position": "bottomleft", + "emptyString": "No velocity data", + "angleConvention": "bearingCW", + "displayPosition": "bottomleft", + "displayEmptyString": "No velocity data", + "speedUnit": "kt", + } + ).tag(sync=True) min_velocity = Float(0).tag(sync=True, o=True) max_velocity = Float(10).tag(sync=True, o=True) velocity_scale = Float(0.005).tag(sync=True, o=True) colormap = Any(linear.OrRd_06) color_scale = List([]).tag(sync=True, o=True) + caption = Unicode("").tag(sync=True, o=True) - @default('color_scale') + @default("color_scale") def _default_color_scale(self): self.color_scale = [] @@ -83,3 +90,16 @@ def _default_color_scale(self): self.color_scale.append(rgb_str) return self.color_scale + + @default("subitems") + def _default_subitems(self): + colormap_control = ColormapControl( + caption=self.caption, + colormap=self.colormap, + value_min=self.min_velocity, + value_max=self.max_velocity, + position="topright", + transparent_bg=False, + ) + self.subitems = (colormap_control,) + return self.subitems diff --git a/js/src/layers/Velocity.js b/js/src/layers/Velocity.js index b605ce964..a01f9d547 100644 --- a/js/src/layers/Velocity.js +++ b/js/src/layers/Velocity.js @@ -25,7 +25,8 @@ export class LeafletVelocityModel extends layer.LeafletLayerModel { minVelocity: 0, maxVelocity: 10, velocityScale: 0.005, - colorScale: [] + colorScale: [], + caption:'' }; } } From 7c5a4823cc14b166fc8281f7ee1f1fbed3ebc3e9 Mon Sep 17 00:00:00 2001 From: Florence Haudin Date: Fri, 23 Sep 2022 17:52:43 +0200 Subject: [PATCH 09/13] Update Velocity_with_colormap.ipynb --- examples/Velocity_with_colormap.ipynb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/Velocity_with_colormap.ipynb b/examples/Velocity_with_colormap.ipynb index 9824f9a14..e8daf336d 100644 --- a/examples/Velocity_with_colormap.ipynb +++ b/examples/Velocity_with_colormap.ipynb @@ -101,6 +101,7 @@ " max_velocity=20,\n", " colormap = linear.viridis,\n", " display_options=display_options,\n", + " caption='wind velocity (m/s)',\n", ")" ] }, @@ -111,7 +112,7 @@ "outputs": [], "source": [ "colormap_control = ipyleaflet.ColormapControl(\n", - " caption='Unemployment rate',\n", + " caption=wind.caption,\n", " colormap=wind.colormap,\n", " value_min=wind.min_velocity,\n", " value_max=wind.max_velocity,\n", From bd851e35a8716061e4759f5321df5485cb171344 Mon Sep 17 00:00:00 2001 From: Florence Haudin <99649086+HaudinFlorence@users.noreply.github.com> Date: Mon, 26 Sep 2022 09:39:49 +0200 Subject: [PATCH 10/13] Update ipyleaflet/velocity.py Co-authored-by: martinRenou --- ipyleaflet/velocity.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ipyleaflet/velocity.py b/ipyleaflet/velocity.py index 901ed0be4..a950fa1c3 100644 --- a/ipyleaflet/velocity.py +++ b/ipyleaflet/velocity.py @@ -101,5 +101,4 @@ def _default_subitems(self): position="topright", transparent_bg=False, ) - self.subitems = (colormap_control,) - return self.subitems + return (colormap_control,) From 2af2de0b598abac928d691d4df7c8b22d8c218c5 Mon Sep 17 00:00:00 2001 From: Florence Haudin <99649086+HaudinFlorence@users.noreply.github.com> Date: Mon, 26 Sep 2022 09:40:54 +0200 Subject: [PATCH 11/13] Update ipyleaflet/velocity.py Co-authored-by: martinRenou --- ipyleaflet/velocity.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/ipyleaflet/velocity.py b/ipyleaflet/velocity.py index a950fa1c3..e8a6771cd 100644 --- a/ipyleaflet/velocity.py +++ b/ipyleaflet/velocity.py @@ -82,14 +82,10 @@ class Velocity(Layer): @default("color_scale") def _default_color_scale(self): - self.color_scale = [] - - for color in self.colormap.colors: - rgb_tuple = tuple(int(x * 256) for x in color[:3]) - rgb_str = f"rgb{rgb_tuple}" - self.color_scale.append(rgb_str) - - return self.color_scale + return [ + f"rgba{tuple(int(x * 255) for x in color)}" + for color in self.colormap.colors + ] @default("subitems") def _default_subitems(self): From c5c83bf41b4f35fa118bbc4812c0ccdc030b5eb7 Mon Sep 17 00:00:00 2001 From: Florence Haudin <99649086+HaudinFlorence@users.noreply.github.com> Date: Mon, 26 Sep 2022 09:41:15 +0200 Subject: [PATCH 12/13] Update js/src/layers/Velocity.js Co-authored-by: martinRenou --- js/src/layers/Velocity.js | 1 - 1 file changed, 1 deletion(-) diff --git a/js/src/layers/Velocity.js b/js/src/layers/Velocity.js index a01f9d547..30663d37c 100644 --- a/js/src/layers/Velocity.js +++ b/js/src/layers/Velocity.js @@ -26,7 +26,6 @@ export class LeafletVelocityModel extends layer.LeafletLayerModel { maxVelocity: 10, velocityScale: 0.005, colorScale: [], - caption:'' }; } } From 127283ea7969006179785a5ea71003033f55099c Mon Sep 17 00:00:00 2001 From: Florence Haudin Date: Mon, 26 Sep 2022 10:23:08 +0200 Subject: [PATCH 13/13] Remove caption in Velocity layer and in colormap Control, since it cant be displayed with the current version of the branca colormap. --- examples/Velocity_with_colormap.ipynb | 4 +--- ipyleaflet/leaflet.py | 4 ---- ipyleaflet/velocity.py | 2 -- 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/examples/Velocity_with_colormap.ipynb b/examples/Velocity_with_colormap.ipynb index e8daf336d..33046d17e 100644 --- a/examples/Velocity_with_colormap.ipynb +++ b/examples/Velocity_with_colormap.ipynb @@ -100,8 +100,7 @@ " velocity_scale=0.01,\n", " max_velocity=20,\n", " colormap = linear.viridis,\n", - " display_options=display_options,\n", - " caption='wind velocity (m/s)',\n", + " display_options=display_options\n", ")" ] }, @@ -112,7 +111,6 @@ "outputs": [], "source": [ "colormap_control = ipyleaflet.ColormapControl(\n", - " caption=wind.caption,\n", " colormap=wind.colormap,\n", " value_min=wind.min_velocity,\n", " value_max=wind.max_velocity,\n", diff --git a/ipyleaflet/leaflet.py b/ipyleaflet/leaflet.py index 6489f4af4..c397ea335 100644 --- a/ipyleaflet/leaflet.py +++ b/ipyleaflet/leaflet.py @@ -2049,8 +2049,6 @@ class ColormapControl(WidgetControl): Attributes ---------- - caption : str, default 'caption' - The caption of the colormap. colormap: branca.colormap.ColorMap instance, default linear.OrRd_06 The colormap used for the effect. value_min : float, default 0.0 @@ -2058,7 +2056,6 @@ class ColormapControl(WidgetControl): value_max : float, default 1.0 The maximal value taken by the data to be represented by the colormap. """ - caption = Unicode('caption') colormap = Instance(ColorMap, default_value=linear.OrRd_06) value_min = CFloat(0.0) value_max = CFloat(1.0) @@ -2068,7 +2065,6 @@ def _default_widget(self): widget = Output(layout={'height': '40px', 'width': '520px', 'margin': '0px -19px 0px 0px'}) with widget: colormap = self.colormap.scale(self.value_min, self.value_max) - colormap.caption = self.caption display(colormap) return widget diff --git a/ipyleaflet/velocity.py b/ipyleaflet/velocity.py index e8a6771cd..f82dce2cb 100644 --- a/ipyleaflet/velocity.py +++ b/ipyleaflet/velocity.py @@ -78,7 +78,6 @@ class Velocity(Layer): velocity_scale = Float(0.005).tag(sync=True, o=True) colormap = Any(linear.OrRd_06) color_scale = List([]).tag(sync=True, o=True) - caption = Unicode("").tag(sync=True, o=True) @default("color_scale") def _default_color_scale(self): @@ -90,7 +89,6 @@ def _default_color_scale(self): @default("subitems") def _default_subitems(self): colormap_control = ColormapControl( - caption=self.caption, colormap=self.colormap, value_min=self.min_velocity, value_max=self.max_velocity,