Skip to content

Commit 9a522e5

Browse files
committed
Add method to select features
1 parent b8adae6 commit 9a522e5

File tree

5 files changed

+115
-48
lines changed

5 files changed

+115
-48
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import openlayers.express as ox
2+
3+
url = "https://openlayers.org/data/vector/us-states.json"
4+
5+
m = ox.GeoJSONLayer(data=url, webgl=False).to_map()
6+
m.add_call("addSelectFeatures")
7+
m.save()

src/openlayers/express.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ def __init__(
4444
data: str | dict,
4545
id: str | None = None,
4646
style: FlatStyle | None = None,
47+
webgl: bool = True,
4748
**kwargs,
4849
):
50+
vector_layer_class = WebGLVectorLayer if webgl else VectorLayer
4951
if isinstance(data, str):
5052
source = VectorSource(url=data)
5153
else:
5254
source = VectorSource(geojson=data)
5355

54-
self._model = WebGLVectorLayer(
56+
self._model = vector_layer_class(
5557
source=source,
5658
style=style
5759
or default_style().model_copy(update=FlatStyle(**kwargs).model_dump2()),

src/openlayers/js/openlayers.standalone.js

Lines changed: 47 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

srcjs/ipywidget-ts/map.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Overlay from "ol/Overlay";
55
import { fromLonLat, transformExtent, useGeographic } from "ol/proj";
66
import { JSONConverter } from "./json";
77
import { addTooltipToMap } from "./tooltip";
8+
import { addSelectFeaturesToMap } from "./select-features";
89

910
// --- Types
1011
import type Layer from "ol/layer/Layer";
@@ -238,4 +239,8 @@ export default class MapWidget {
238239
addTooltip(template: string | null): void {
239240
addTooltipToMap(this._map, template);
240241
}
242+
243+
addSelectFeatures(): void {
244+
addSelectFeaturesToMap(this._map);
245+
}
241246
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Map } from "ol";
2+
import { FeatureLike } from "ol/Feature";
3+
import Feature from "ol/Feature";
4+
import Style, { StyleLike } from "ol/style/Style";
5+
import Fill from 'ol/style/Fill.js';
6+
import Stroke from 'ol/style/Stroke.js';
7+
import VectorLayer from "ol/layer/Vector";
8+
9+
// TODO: Should be a parameter
10+
const highlightStyle = new Style({
11+
fill: new Fill({
12+
color: 'rgba(58, 154, 178,0.7)'
13+
}),
14+
stroke: new Stroke({
15+
// color: 'rgba(241, 27, 0, 0.7)',
16+
color: "rgba(220, 203, 78, 0.7)",
17+
// color: "rgba(111, 178, 193, 0.7)",
18+
width: 2
19+
})
20+
});
21+
22+
function addSelectFeaturesToMap(map: Map): void {
23+
const selected = [] as Feature[];
24+
25+
map.on('singleclick', function (e) {
26+
map.forEachFeatureAtPixel(e.pixel, function (feature, layer) {
27+
const isVectorLayer = layer instanceof VectorLayer;
28+
console.log("isVectorLayer", isVectorLayer);
29+
const f = feature as Feature;
30+
const selIndex = selected.indexOf(f);
31+
if (selIndex < 0) {
32+
console.log("push");
33+
selected.push(f);
34+
35+
// Does not work for WebGLVectorLayer
36+
if (isVectorLayer)
37+
f.setStyle(highlightStyle);
38+
} else {
39+
console.log("delete");
40+
selected.splice(selIndex, 1);
41+
42+
// Does not work for WebGLVectorLayer
43+
if (isVectorLayer)
44+
f.setStyle();
45+
}
46+
});
47+
48+
const output = selected.map(f => { return f.getProperties(); });
49+
console.log(output);
50+
});
51+
}
52+
53+
export { addSelectFeaturesToMap };

0 commit comments

Comments
 (0)