Skip to content

Commit f4edc00

Browse files
committed
Add drag and drop interaction
1 parent 2726367 commit f4edc00

File tree

6 files changed

+114
-58
lines changed

6 files changed

+114
-58
lines changed

examples/standalone/layers/kml_vector_layer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313
m = ol.Map()
1414
m.add_layer(kml_layer)
1515
m.add_tooltip()
16+
# m.add_call("addDragAndDropVectorLayers")
17+
m.add_drag_and_drop_vector_layers_interaction()
1618
m.save("/tmp/ol-example.html")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "openlayers"
3-
version = "0.1.0-rc.2"
3+
version = "0.1.0-rc.3"
44
description = "Python Bindings for OpenLayersJS"
55
readme = "README.md"
66
authors = [{ name = "Stefan Kuethe", email = "[email protected]" }]

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.

src/openlayers/map.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ def add_select_features(self) -> None:
152152
"""
153153
self.add_call("addSelectFeatures")
154154

155+
def add_drag_and_drop_vector_layers_interaction(self, style: FlatStyle | dict = None) -> None:
156+
return self.add_call("addDragAndDropVectorLayers")
157+
155158
def set_opacity(self, layer_id: str, opacity: float = 1) -> None:
156159
"""Set the opacity of a layer
157160
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// See https://openlayers.org/en/latest/examples/drag-and-drop.html
2+
// ---
3+
import type { Map } from "ol";
4+
5+
import GPX from 'ol/format/GPX.js';
6+
import GeoJSON from 'ol/format/GeoJSON.js';
7+
import IGC from 'ol/format/IGC.js';
8+
import KML from 'ol/format/KML.js';
9+
import TopoJSON from 'ol/format/TopoJSON.js';
10+
import DragAndDrop from 'ol/interaction/DragAndDrop.js';
11+
import VectorSource from 'ol/source/Vector.js';
12+
import VectorLayer from 'ol/layer/Vector.js';
13+
14+
function addDragAndDropToMap(map: Map): void {
15+
let dragAndDropInteraction: any;
16+
17+
function setInteraction() {
18+
if (dragAndDropInteraction) {
19+
map.removeInteraction(dragAndDropInteraction);
20+
}
21+
dragAndDropInteraction = new DragAndDrop({
22+
formatConstructors: [
23+
new GPX(),
24+
new GeoJSON(),
25+
new IGC(),
26+
new KML(),
27+
new TopoJSON(),
28+
],
29+
});
30+
dragAndDropInteraction.on('addfeatures', function (event: any) {
31+
const vectorSource = new VectorSource({
32+
features: event.features,
33+
});
34+
map.addLayer(
35+
new VectorLayer({
36+
source: vectorSource,
37+
}),
38+
);
39+
map.getView().fit(vectorSource.getExtent());
40+
});
41+
map.addInteraction(dragAndDropInteraction);
42+
}
43+
setInteraction();
44+
}
45+
46+
export { addDragAndDropToMap };

srcjs/ipywidget-ts/map.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { fromLonLat, transformExtent, useGeographic } from "ol/proj";
66
import { JSONConverter } from "./json";
77
import { addTooltipToMap } from "./tooltip";
88
import { addSelectFeaturesToMap } from "./select-features";
9+
import { addDragAndDropToMap as addDragAndDropVectorLayersToMap } from "./drag-and-drop";
910

1011
// --- Types
1112
import type Layer from "ol/layer/Layer";
@@ -254,4 +255,8 @@ export default class MapWidget {
254255
addSelectFeatures(): void {
255256
addSelectFeaturesToMap(this._map, this._model);
256257
}
258+
259+
addDragAndDropVectorLayers(formats?: any[]): void {
260+
addDragAndDropVectorLayersToMap(this._map);
261+
}
257262
}

0 commit comments

Comments
 (0)