Skip to content

Commit 52f4cb7

Browse files
Improve vector tile layer (#1206)
* Add min/max/native zoom options for VectorTileLayers * Pass vector_tile_layer_styles as string * Fix typescript error * Updated docstring * lint * Update python/jupyter_leaflet/src/layers/VectorTileLayer.ts Co-authored-by: martinRenou <[email protected]> * Update docstring --------- Co-authored-by: martinRenou <[email protected]>
1 parent 1126449 commit 52f4cb7

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

python/ipyleaflet/ipyleaflet/leaflet.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -1096,8 +1096,16 @@ class VectorTileLayer(Layer):
10961096
Url to the vector tile service.
10971097
attribution: string, default ""
10981098
Vector tile service attribution.
1099-
vector_tile_layer_styles: dict, default {}
1099+
vector_tile_layer_styles: dict or string, default {}. If string, it will be parsed as a javascript object (useful for defining styles that depend on properties and/or zoom).
11001100
CSS Styles to apply to the vector data.
1101+
min_zoom: int, default 0
1102+
The minimum zoom level down to which this layer will be displayed (inclusive).
1103+
max_zoom: int, default 18
1104+
The maximum zoom level up to which this layer will be displayed (inclusive).
1105+
min_native_zoom: int, default None
1106+
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.
1107+
max_native_zoom: int, default None
1108+
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.
11011109
"""
11021110

11031111
_view_name = Unicode("LeafletVectorTileLayerView").tag(sync=True)
@@ -1106,7 +1114,12 @@ class VectorTileLayer(Layer):
11061114
url = Unicode().tag(sync=True, o=True)
11071115
attribution = Unicode().tag(sync=True, o=True)
11081116

1109-
vector_tile_layer_styles = Dict().tag(sync=True, o=True)
1117+
vector_tile_layer_styles = Union([Dict(), Unicode()]).tag(sync=True, o=True)
1118+
1119+
min_zoom = Int(0).tag(sync=True, o=True)
1120+
max_zoom = Int(18).tag(sync=True, o=True)
1121+
min_native_zoom = Int(default_value=None, allow_none=True).tag(sync=True, o=True)
1122+
max_native_zoom = Int(default_value=None, allow_none=True).tag(sync=True, o=True)
11101123

11111124
def redraw(self):
11121125
"""Force redrawing the tiles.

python/jupyter_leaflet/src/layers/VectorTileLayer.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,39 @@ export class LeafletVectorTileLayerModel extends LeafletLayerModel {
1313
_model_name: 'LeafletVectorTileLayerModel',
1414
url: '',
1515
vectorTileLayerStyles: {},
16+
min_zoom: 0,
17+
max_zoom: 18,
18+
min_native_zoom: null,
19+
max_native_zoom: null,
1620
};
1721
}
1822
}
1923

2024
export class LeafletVectorTileLayerView extends LeafletLayerView {
2125
obj: VectorGrid.Protobuf;
2226

23-
create_obj() {
24-
const options = {
27+
async create_obj() {
28+
let options = {
2529
...this.get_options(),
26-
rendererFactory: L.canvas.tile,
2730
};
31+
options['rendererFactory'] = L.canvas.tile;
32+
33+
let x: any = this.model.get('vectorTileLayerStyles');
34+
if (typeof x === 'string') {
35+
try {
36+
let blobCode = `const jsStyle=${x}; export { jsStyle };`;
37+
38+
const blob = new Blob([blobCode], { type: 'text/javascript' });
39+
const url = URL.createObjectURL(blob);
40+
const module = await import(/* webpackIgnore: true*/ url);
41+
const jsStyle = module.jsStyle;
42+
43+
options['vectorTileLayerStyles'] = jsStyle;
44+
} catch (error) {
45+
options['vectorTileLayerStyles'] = {};
46+
}
47+
}
48+
2849
this.obj = L.vectorGrid.protobuf(this.model.get('url'), options);
2950
this.model.on('msg:custom', this.handle_message.bind(this));
3051
}

0 commit comments

Comments
 (0)