Skip to content

Commit 3559d62

Browse files
committed
Add box selected features to model
1 parent 7ea8cc5 commit 3559d62

3 files changed

Lines changed: 96 additions & 71 deletions

File tree

marimo/box-selection.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def _():
1717
def _(ol):
1818
data = "https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson"
1919
# data = "https://github.com/visgl/deck.gl-data/raw/master/examples/line/airports.json"
20-
layer = ol.VectorLayer(
20+
layer = ol.WebGLVectorLayer(
2121
id="xyz",
2222
source=ol.sources.VectorSource(
2323
url=data,
@@ -34,7 +34,7 @@ def _(ol):
3434

3535
@app.cell
3636
def _(layer, ol):
37-
m = ol.MapWidget(layers=[layer])
37+
m = ol.MapWidget(layers=[ol.BasemapLayer(), layer])
3838
# m.add_layer(layer)
3939
return (m,)
4040

@@ -45,6 +45,11 @@ def _(m, mo):
4545
return (w,)
4646

4747

48+
@app.cell
49+
def _():
50+
return
51+
52+
4853
@app.cell
4954
def _(w):
5055
w

src/openlayers/js/openlayers.anywidget.js

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

srcjs/ipywidget-ts/select-features.ts

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,38 @@ import type { Map } from "ol";
33
import type Feature from "ol/Feature";
44

55
import Style from "ol/style/Style";
6-
import Fill from 'ol/style/Fill.js';
7-
import Stroke from 'ol/style/Stroke.js';
6+
import Fill from 'ol/style/Fill';
7+
import Stroke from 'ol/style/Stroke';
8+
import Circle from "ol/style/Circle";
89
import VectorLayer from "ol/layer/Vector";
10+
import WebGLVectorLayer from "ol/layer/WebGLVector";
11+
import VectorSource from "ol/source/Vector";
912

1013
// Box selection
11-
import { platformModifierKeyOnly } from 'ol/events/condition.js';
12-
import { getWidth } from 'ol/extent.js';
13-
import DragBox from 'ol/interaction/DragBox.js';
14-
import { getTransformFromProjections, transform } from 'ol/proj';
14+
import { platformModifierKeyOnly } from 'ol/events/condition';
15+
// import { getWidth } from 'ol/extent.js';
16+
import DragBox from 'ol/interaction/DragBox';
17+
// import { getTransformFromProjections, transform } from 'ol/proj';
1518

1619
import { featureToGeoJSON } from "./utils";
17-
import VectorSource from "ol/source/Vector";
20+
21+
const fill = new Fill({
22+
color: 'rgba(58, 154, 178,0.7)'
23+
});
24+
const stroke = new Stroke({
25+
color: "rgba(220, 203, 78, 0.7)",
26+
width: 2
27+
});
1828

1929
// TODO: Should be a parameter
2030
const highlightStyle = new Style({
21-
fill: new Fill({
22-
color: 'rgba(58, 154, 178,0.7)'
31+
image: new Circle({
32+
fill: fill,
33+
stroke: stroke,
34+
radius: 5,
2335
}),
24-
stroke: new Stroke({
25-
// color: 'rgba(241, 27, 0, 0.7)',
26-
color: "rgba(220, 203, 78, 0.7)",
27-
// color: "rgba(111, 178, 193, 0.7)",
28-
width: 2
29-
})
36+
fill: fill,
37+
stroke: stroke
3038
});
3139

3240
// TODO: Setting new style only works for 'VectorLayer'
@@ -79,22 +87,34 @@ function addBoxSelectionToMap(map: Map, model?: AnyModel): void {
7987
console.log("box extent", extent);
8088
//
8189
for (let layer of map.getLayers().getArray()) {
82-
if (layer instanceof VectorLayer) {
90+
if (layer instanceof VectorLayer || layer instanceof WebGLVectorLayer) {
8391
const source: VectorSource = layer.getSource();
8492
console.log("source extend", source.getExtent());
8593
// const features = source.getFeaturesInExtent(extent).filter((feature: Feature) => feature?.getGeometry()?.intersectsExtent(extent));
8694
source.forEachFeatureIntersectingExtent(extent, (feature: Feature) => {
8795
feature.set("layer", layer.get("id"));
88-
selected.push(featureToGeoJSON(feature));
96+
selected.push(feature);
97+
if (layer instanceof VectorLayer) {
98+
feature.setStyle(highlightStyle);
99+
}
89100
});
90-
console.log("selected features", selected);
101+
const output = selected.map(f => featureToGeoJSON(f));
102+
console.log("selected features", output);
103+
if (model) {
104+
model.set("features", { selected: output });
105+
model.save_changes();
106+
}
91107
}
92108
}
93109
});
94110

95111

96112
dragBox.on('boxstart', function () {
97-
// select.clearSelection();
113+
selected.forEach((f: Feature) => { f.setStyle(); });
114+
if (model) {
115+
model.set("features", {});
116+
model.save_changes();
117+
}
98118
selected = [];
99119
});
100120
}

0 commit comments

Comments
 (0)