Skip to content

Commit a2cb24b

Browse files
committed
Add modify to draw features
1 parent 86ad172 commit a2cb24b

File tree

3 files changed

+91
-75
lines changed

3 files changed

+91
-75
lines changed

src/openlayers/js/openlayers.standalone.js

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

srcjs/ipywidget-ts/draw-control.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
/**
2+
* See https://openlayers.org/en/latest/examples/draw-features.html
3+
* and https://openlayers.org/en/latest/examples/draw-and-modify-features.html
4+
* */
15
import type { Map } from "ol";
26
import type { Type as GeomType } from "ol/geom/Geometry";
37

48
import Control from "ol/control/Control";
9+
510
import Draw from "ol/interaction/Draw";
11+
import Modify from 'ol/interaction/Modify.js';
12+
import Snap from 'ol/interaction/Snap.js';
13+
614
import VectorSource from "ol/source/Vector";
715
import VectorLayer from "ol/layer/Vector";
816

17+
import { featureToGeoJSON } from "./utils";
18+
919
type DrawOptions = {
1020
target?: string | HTMLElement | undefined;
1121
cssText?: string;
@@ -39,25 +49,34 @@ function createSelectElement(): HTMLSelectElement {
3949
return select;
4050
}
4151

42-
function x(map: Map, select: HTMLSelectElement): void {
52+
function toggleDrawInteraction(map: Map, select: HTMLSelectElement): void {
4353
let draw: Draw;
54+
let snap: Snap;
55+
56+
const modify = new Modify({ source: source });
57+
map.addInteraction(modify);
4458

4559
function addInteraction() {
4660
const value = select.value;
47-
if (value !== 'None') {
48-
draw = new Draw({
49-
source: source,
50-
type: select.value as GeomType
51-
52-
});
53-
map.addInteraction(draw);
54-
}
61+
if (value === "None")
62+
return;
63+
64+
draw = new Draw({
65+
source: source,
66+
type: value as GeomType
67+
68+
});
69+
map.addInteraction(draw);
70+
snap = new Snap({ source: source });
71+
map.addInteraction(snap);
5572
}
5673

57-
select.onchange = function () {
74+
select.onchange = () => {
5875
map.removeInteraction(draw);
76+
map.removeInteraction(snap);
5977
addInteraction();
6078
};
79+
6180
addInteraction();
6281
}
6382

@@ -72,22 +91,20 @@ class DrawControl extends Control {
7291
target: options.target
7392
});
7493
this.setProperties({ id: "draw", type: "DrawControl" });
75-
this.once("change", (e) => {
76-
console.log("draw map", e.target.getMap());
77-
});
7894
}
7995

8096
y(): void {
81-
console.log("map", this.getMap());
97+
// console.log("map", this.getMap());
8298
const map = this.getMap();
8399
map?.addLayer(vectorLayer);
84100
const select = createSelectElement();
85101

86102
// @ts-expect-error
87-
x(map, select);
103+
toggleDrawInteraction(map, select);
88104
this.element.appendChild(select);
89-
90105
}
106+
107+
getGeoJSONFeatures(): any { }
91108
}
92109

93110
export { DrawControl };

srcjs/ipywidget-ts/select-features.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import Fill from 'ol/style/Fill.js';
77
import Stroke from 'ol/style/Stroke.js';
88
import VectorLayer from "ol/layer/Vector";
99

10-
1110
import { featureToGeoJSON } from "./utils";
1211

1312
// TODO: Should be a parameter
@@ -24,7 +23,7 @@ const highlightStyle = new Style({
2423
});
2524

2625
// TODO: Setting new style only works for 'VectorLayer'
27-
// For 'WebGLVectorLayer' we need to add complete highlight-layer on topt of the current one
26+
// For 'WebGLVectorLayer' we need to add complete highlight-layer on top of the current one
2827
function addSelectFeaturesToMap(map: Map, model?: AnyModel): void {
2928
const selected = [] as Feature[];
3029

0 commit comments

Comments
 (0)