Skip to content

Commit

Permalink
Add set_feature_style and reset_feature_style
Browse files Browse the repository at this point in the history
  • Loading branch information
lopezvoliver committed Jul 2, 2024
1 parent fd30835 commit c20ba8f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
35 changes: 34 additions & 1 deletion python/ipyleaflet/ipyleaflet/leaflet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,8 @@ class VectorTileLayer(Layer):
Opacity of the layer between 0. (fully transparent) and 1. (fully opaque).
visible: boolean, default True
Whether the layer is visible or not.
renderer_factory: rendererFactory option ('svg' or 'canvas') for L.VectorGrid, default 'svg'
renderer_factory: string, default 'svg'
Engine for rendering VectorTileLayers; either 'canvas' or 'svg'. Use 'svg' for interactive layers.
interactive: boolean, default False
Whether the layer is interactive or not.
get_feature_id: string, default None
Expand Down Expand Up @@ -1142,6 +1143,38 @@ def redraw(self):
"""
self.send({"msg": "redraw"})

def set_feature_style(self, id:Int, layer_style:Dict):
"""Re-symbolize one feature.
Given the unique ID for a vector features, re-symbolizes that feature across all tiles it appears in.
Reverts the effects of a previous set_feature_style call. get_feature_id must be defined for
set_feature_style to work.
Attributes
----------
id: int
The unique identifier for the feature to re-symbolize
layer_styles: dict
Style to apply to the feature
"""
self.send({"msg":
{"setFeatureStyle":
{"id":id, "layerStyle":layer_style}}
})

def reset_feature_style(self, id:Int):
"""Reset feature style
Reverts the style to the layer's deafult.
Attributes
----------
id: int
The unique identifier for the feature to re-symbolize
"""
self.send({"msg": {"resetFeatureStyle":{"id":id}}})



class PMTilesLayer(Layer):
"""PMTilesLayer class, with Layer as parent class.
Expand Down
21 changes: 16 additions & 5 deletions python/jupyter_leaflet/src/layers/VectorTileLayer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { VectorGrid } from 'leaflet';
import { LeafletMouseEvent, VectorGrid } from 'leaflet';
import L from '../leaflet';
import { LeafletLayerModel, LeafletLayerView } from './Layer';

Expand All @@ -21,7 +21,7 @@ export class LeafletVectorTileLayerModel extends LeafletLayerModel {
visible: true,
opacity: 1.0,
renderer_factory: 'svg',
get_feature_id: null
get_feature_id: null,
};
}
}
Expand Down Expand Up @@ -101,9 +101,20 @@ export class LeafletVectorTileLayerView extends LeafletLayerView {
});
}

handle_message(content: { msg: string }) {
if (content.msg == 'redraw') {
this.obj.redraw();
handle_message(content: { msg: any }) {
if (typeof content.msg === 'string') {
if (content.msg == 'redraw') {
this.obj.redraw();
}
} else {
if ('setFeatureStyle' in content.msg) {
let options = content.msg.setFeatureStyle;
this.obj.setFeatureStyle(options.id, options.layerStyle);
}
if ('resetFeatureStyle' in content.msg) {
let options = content.msg.resetFeatureStyle;
this.obj.resetFeatureStyle(options.id);
}
}
}
}

0 comments on commit c20ba8f

Please sign in to comment.