From 0cd034c1b136d4c8f54548ae96f9c0fbb9049363 Mon Sep 17 00:00:00 2001 From: Oliver Lopez Date: Tue, 25 Jun 2024 16:05:19 -0700 Subject: [PATCH 1/2] Vector tile layer opacity and visible --- python/ipyleaflet/ipyleaflet/leaflet.py | 7 ++++++- .../src/layers/VectorTileLayer.ts | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/python/ipyleaflet/ipyleaflet/leaflet.py b/python/ipyleaflet/ipyleaflet/leaflet.py index f0c6be41..f68039dc 100644 --- a/python/ipyleaflet/ipyleaflet/leaflet.py +++ b/python/ipyleaflet/ipyleaflet/leaflet.py @@ -1106,6 +1106,10 @@ class VectorTileLayer(Layer): Minimum zoom number the tile source has available. If it is specified, the tiles on all zoom levels lower than min_native_zoom will be loaded from min_native_zoom level and auto-scaled. max_native_zoom: int, default None Maximum zoom number the tile source has available. If it is specified, the tiles on all zoom levels higher than max_native_zoom will be loaded from max_native_zoom level and auto-scaled. + opacity: float, default 1. + Opacity of the layer between 0. (fully transparent) and 1. (fully opaque). + visible: boolean, default True + Whether the layer is visible or not. """ _view_name = Unicode("LeafletVectorTileLayerView").tag(sync=True) @@ -1115,7 +1119,8 @@ class VectorTileLayer(Layer): attribution = Unicode().tag(sync=True, o=True) vector_tile_layer_styles = Union([Dict(), Unicode()]).tag(sync=True, o=True) - + opacity = Float(1.0, min=0.0, max=1.0).tag(sync=True) + visible = Bool(True).tag(sync=True) min_zoom = Int(0).tag(sync=True, o=True) max_zoom = Int(18).tag(sync=True, o=True) min_native_zoom = Int(default_value=None, allow_none=True).tag(sync=True, o=True) diff --git a/python/jupyter_leaflet/src/layers/VectorTileLayer.ts b/python/jupyter_leaflet/src/layers/VectorTileLayer.ts index f255e7c0..83f41d03 100644 --- a/python/jupyter_leaflet/src/layers/VectorTileLayer.ts +++ b/python/jupyter_leaflet/src/layers/VectorTileLayer.ts @@ -17,6 +17,9 @@ export class LeafletVectorTileLayerModel extends LeafletLayerModel { max_zoom: 18, min_native_zoom: null, max_native_zoom: null, + interactive: true, + visible: true, + opacity: 1.0, }; } } @@ -29,8 +32,8 @@ export class LeafletVectorTileLayerView extends LeafletLayerView { ...this.get_options(), }; options['rendererFactory'] = L.canvas.tile; - - let x: any = this.model.get('vectorTileLayerStyles'); + + let x: any = options['vectorTileLayerStyles']; if (typeof x === 'string') { try { let blobCode = `const jsStyle=${x}; export { jsStyle };`; @@ -55,6 +58,18 @@ export class LeafletVectorTileLayerView extends LeafletLayerView { this.listenTo(this.model, 'change:url', () => { this.obj.setUrl(this.model.get('url')); }); + this.listenTo(this.model, 'change:opacity', () => { + if (this.model.get('visible')) { + this.obj.setOpacity(this.model.get('opacity')); + } + }); + this.listenTo(this.model, 'change:visible', () => { + if (this.model.get('visible')) { + this.obj.setOpacity(this.model.get('opacity')); + } else { + this.obj.setOpacity(0); + } + }); } handle_message(content: { msg: string }) { From 30ac4f2af6c3d82e8c7b53f9b323a21d46eda531 Mon Sep 17 00:00:00 2001 From: Oliver Lopez Date: Tue, 25 Jun 2024 16:23:32 -0700 Subject: [PATCH 2/2] lint --- python/jupyter_leaflet/src/layers/VectorTileLayer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/jupyter_leaflet/src/layers/VectorTileLayer.ts b/python/jupyter_leaflet/src/layers/VectorTileLayer.ts index 83f41d03..6866d045 100644 --- a/python/jupyter_leaflet/src/layers/VectorTileLayer.ts +++ b/python/jupyter_leaflet/src/layers/VectorTileLayer.ts @@ -32,7 +32,7 @@ export class LeafletVectorTileLayerView extends LeafletLayerView { ...this.get_options(), }; options['rendererFactory'] = L.canvas.tile; - + let x: any = options['vectorTileLayerStyles']; if (typeof x === 'string') { try {