Skip to content

Commit d3c87a5

Browse files
committed
Backwards compatiblity and update layer_styles
1 parent aaf6020 commit d3c87a5

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

python/ipyleaflet/ipyleaflet/leaflet.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,6 @@ class VectorTileLayer(Layer):
11251125
attribution = Unicode().tag(sync=True, o=True)
11261126

11271127
layer_styles = Union([Dict(), Unicode()]).tag(sync=True, o=True)
1128-
vector_tile_layer_styles = Union([Dict(), Unicode()], allow_none=True, default_value=None)
11291128
opacity = Float(1.0, min=0.0, max=1.0).tag(sync=True,o=True)
11301129
visible = Bool(True).tag(sync=True, o=True)
11311130
interactive = Bool(False).tag(sync=True, o=True)
@@ -1137,12 +1136,22 @@ class VectorTileLayer(Layer):
11371136
feature_id = Unicode(allow_none=True, default_value=None).tag(sync=True, o=True)
11381137
feature_style = Dict().tag(sync=True)
11391138

1139+
# Backwards compatibility: allow vector_tile_layer_styles as input:
1140+
@property
1141+
def vector_tile_layer_styles(self):
1142+
return self.layer_styles
1143+
1144+
@vector_tile_layer_styles.setter
1145+
def vector_tile_layer_styles(self, value):
1146+
self.layer_styles = value
1147+
11401148
def __init__(self, **kwargs):
11411149
super(VectorTileLayer, self).__init__(**kwargs)
11421150
# Backwards compatibility: allow vector_tile_layer_styles as input:
1143-
vtl_style = getattr(self, "vector_tile_layer_styles")
1144-
if(vtl_style):
1145-
self.layer_styles = vtl_style
1151+
if "vector_tile_layer_styles" in kwargs:
1152+
vtl_style = kwargs["vector_tile_layer_styles"]
1153+
if(vtl_style):
1154+
self.layer_styles = vtl_style
11461155

11471156
def redraw(self):
11481157
"""Force redrawing the tiles.

python/jupyter_leaflet/src/layers/VectorTileLayer.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ export class LeafletVectorTileLayerModel extends LeafletLayerModel {
2929
export class LeafletVectorTileLayerView extends LeafletLayerView {
3030
obj: VectorGrid.Protobuf;
3131

32+
async set_vector_tile_layer_styles(options: any) {
33+
if ('layerStyles' in options) {
34+
let x: any = options['layerStyles'];
35+
options['vectorTileLayerStyles'] = x;
36+
if (typeof x === 'string') {
37+
try {
38+
let blobCode = `const jsStyle=${x}; export { jsStyle };`;
39+
40+
const blob = new Blob([blobCode], { type: 'text/javascript' });
41+
const url = URL.createObjectURL(blob);
42+
const module = await import(/* webpackIgnore: true*/ url);
43+
const jsStyle = module.jsStyle;
44+
45+
options['vectorTileLayerStyles'] = jsStyle;
46+
} catch (error) {
47+
options['vectorTileLayerStyles'] = {};
48+
}
49+
}
50+
}
51+
return options;
52+
}
53+
3254
async create_obj() {
3355
let options = {
3456
...this.get_options(),
@@ -50,24 +72,7 @@ export class LeafletVectorTileLayerView extends LeafletLayerView {
5072
}
5173
}
5274

53-
if ('layerStyles' in options) {
54-
let x: any = options['layerStyles'];
55-
options['vectorTileLayerStyles'] = x;
56-
if (typeof x === 'string') {
57-
try {
58-
let blobCode = `const jsStyle=${x}; export { jsStyle };`;
59-
60-
const blob = new Blob([blobCode], { type: 'text/javascript' });
61-
const url = URL.createObjectURL(blob);
62-
const module = await import(/* webpackIgnore: true*/ url);
63-
const jsStyle = module.jsStyle;
64-
65-
options['vectorTileLayerStyles'] = jsStyle;
66-
} catch (error) {
67-
options['vectorTileLayerStyles'] = {};
68-
}
69-
}
70-
}
75+
options = await this.set_vector_tile_layer_styles(options);
7176

7277
this.obj = L.vectorGrid.protobuf(this.model.get('url'), options);
7378
this.model.on('msg:custom', this.handle_message.bind(this));
@@ -76,6 +81,18 @@ export class LeafletVectorTileLayerView extends LeafletLayerView {
7681
this.obj.setOpacity(0);
7782
}
7883

84+
this.model.on('change:layer_styles', async () => {
85+
let options = {
86+
...this.get_options(),
87+
};
88+
options = await this.set_vector_tile_layer_styles(options);
89+
this.obj.options.vectorTileLayerStyles = options['vectorTileLayerStyles'];
90+
if (this.model.get('visible') == false) {
91+
this.obj.setOpacity(0);
92+
}
93+
this.obj.redraw();
94+
});
95+
7996
this.model.on('change:feature_style', () => {
8097
const feature_style = this.model.get('feature_style');
8198
const reset = feature_style['reset'];

0 commit comments

Comments
 (0)