Skip to content

Commit 7ea8cc5

Browse files
committed
Get intersecting features
1 parent e3ea2f1 commit 7ea8cc5

3 files changed

Lines changed: 80 additions & 38 deletions

File tree

marimo/box-selection.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,27 @@ def _():
1515

1616
@app.cell
1717
def _(ol):
18+
data = "https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson"
19+
# data = "https://github.com/visgl/deck.gl-data/raw/master/examples/line/airports.json"
1820
layer = ol.VectorLayer(
21+
id="xyz",
1922
source=ol.sources.VectorSource(
20-
url="https://openlayers.org/data/vector/ecoregions.json"
21-
)
23+
url=data,
24+
# url="https://openlayers.org/data/vector/ecoregions.json"
25+
crossOrigin="anonymous"
26+
27+
),
28+
style=ol.FlatStyle(
29+
circle_radius=5,
30+
circle_fill_color="red", circle_stroke_color="green")
2231
)
2332
return (layer,)
2433

2534

2635
@app.cell
2736
def _(layer, ol):
2837
m = ol.MapWidget(layers=[layer])
38+
# m.add_layer(layer)
2939
return (m,)
3040

3141

@@ -42,8 +52,8 @@ def _(w):
4252

4353

4454
@app.cell
45-
def _(w):
46-
w.value["view_state"]
55+
def _():
56+
# w.value["view_state"]
4757
return
4858

4959

@@ -53,6 +63,18 @@ def _(m):
5363
return
5464

5565

66+
@app.cell
67+
def _(m):
68+
m.add_select_interaction()
69+
return
70+
71+
72+
@app.cell
73+
def _(w):
74+
w.value["features"]
75+
return
76+
77+
5678
@app.cell
5779
def _():
5880
return

src/openlayers/js/openlayers.anywidget.js

Lines changed: 31 additions & 31 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: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import VectorLayer from "ol/layer/Vector";
99

1010
// Box selection
1111
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';
1215

1316
import { featureToGeoJSON } from "./utils";
14-
import DragBox from 'ol/interaction/DragBox.js';
17+
import VectorSource from "ol/source/Vector";
1518

1619
// TODO: Should be a parameter
1720
const highlightStyle = new Style({
@@ -63,19 +66,36 @@ function addSelectFeaturesToMap(map: Map, model?: AnyModel): void {
6366
}
6467

6568
function addBoxSelectionToMap(map: Map, model?: AnyModel): void {
66-
console.warn("Box selection not implemented yet");
69+
console.warn("Box selection is still experimental.");
70+
71+
let selected = [] as Feature[];
6772

6873
const dragBox = new DragBox({
6974
condition: platformModifierKeyOnly,
7075
});
7176
map.addInteraction(dragBox);
7277
dragBox.on('boxend', function () {
73-
const extent = dragBox.getGeometry().getExtent();
78+
const extent = dragBox.getGeometry().transform('EPSG:3857', 'EPSG:4326').getExtent();
7479
console.log("box extent", extent);
80+
//
81+
for (let layer of map.getLayers().getArray()) {
82+
if (layer instanceof VectorLayer) {
83+
const source: VectorSource = layer.getSource();
84+
console.log("source extend", source.getExtent());
85+
// const features = source.getFeaturesInExtent(extent).filter((feature: Feature) => feature?.getGeometry()?.intersectsExtent(extent));
86+
source.forEachFeatureIntersectingExtent(extent, (feature: Feature) => {
87+
feature.set("layer", layer.get("id"));
88+
selected.push(featureToGeoJSON(feature));
89+
});
90+
console.log("selected features", selected);
91+
}
92+
}
7593
});
7694

95+
7796
dragBox.on('boxstart', function () {
7897
// select.clearSelection();
98+
selected = [];
7999
});
80100
}
81101

0 commit comments

Comments
 (0)