Skip to content

Commit 42ecb48

Browse files
committed
Refactor drag and drop interaction
1 parent f4edc00 commit 42ecb48

File tree

6 files changed

+136
-115
lines changed

6 files changed

+136
-115
lines changed

examples/standalone/layers/kml_vector_layer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
)
1212

1313
m = ol.Map()
14-
m.add_layer(kml_layer)
14+
# m.add_layer(kml_layer)
1515
m.add_tooltip()
1616
# m.add_call("addDragAndDropVectorLayers")
17-
m.add_drag_and_drop_vector_layers_interaction()
17+
m.add_drag_and_drop_vector_layers_interaction(
18+
formats=[ol.formats.KML(extract_styles=False)],
19+
style=ol.FlatStyle(stroke_color="red", stroke_width=3, circle_radius=5, circle_stroke_color="green"))
1820
m.save("/tmp/ol-example.html")

src/openlayers/js/openlayers.anywidget.js

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

src/openlayers/js/openlayers.standalone.js

Lines changed: 21 additions & 21 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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .models.sources import OSM, SourceT
1313
from .models.view import View
1414
from .styles import FlatStyle
15+
from .models.formats import FormatT, GeoJSON, GPX, TopoJSON, KML
1516

1617

1718
class Map(object):
@@ -152,8 +153,17 @@ def add_select_features(self) -> None:
152153
"""
153154
self.add_call("addSelectFeatures")
154155

155-
def add_drag_and_drop_vector_layers_interaction(self, style: FlatStyle | dict = None) -> None:
156-
return self.add_call("addDragAndDropVectorLayers")
156+
def add_drag_and_drop_vector_layers_interaction(
157+
self,
158+
formats: list[FormatT] = [GeoJSON(), TopoJSON(), GPX(), KML()],
159+
style: FlatStyle | dict = None,
160+
) -> None:
161+
"""Add drag and drop interaction to map"""
162+
formats = [f.model_dump() for f in formats]
163+
if isinstance(style, FlatStyle):
164+
style = style.model_dump()
165+
166+
return self.add_call("addDragAndDropVectorLayers", formats, style)
157167

158168
def set_opacity(self, layer_id: str, opacity: float = 1) -> None:
159169
"""Set the opacity of a layer

srcjs/ipywidget-ts/drag-and-drop.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// See https://openlayers.org/en/latest/examples/drag-and-drop.html
22
// ---
33
import type { Map } from "ol";
4+
import type FeatureFormat from "ol/format/Feature";
45

56
import GPX from 'ol/format/GPX.js';
67
import GeoJSON from 'ol/format/GeoJSON.js';
@@ -10,22 +11,25 @@ import TopoJSON from 'ol/format/TopoJSON.js';
1011
import DragAndDrop from 'ol/interaction/DragAndDrop.js';
1112
import VectorSource from 'ol/source/Vector.js';
1213
import VectorLayer from 'ol/layer/Vector.js';
14+
import { FlatStyle } from "ol/style/flat";
1315

14-
function addDragAndDropToMap(map: Map): void {
16+
const defaultFormats = [
17+
new GPX(),
18+
new GeoJSON(),
19+
new IGC(),
20+
new KML(),
21+
new TopoJSON(),
22+
];
23+
24+
function addDragAndDropToMap(map: Map, formats?: FeatureFormat[], style?: FlatStyle): void {
1525
let dragAndDropInteraction: any;
1626

1727
function setInteraction() {
1828
if (dragAndDropInteraction) {
1929
map.removeInteraction(dragAndDropInteraction);
2030
}
2131
dragAndDropInteraction = new DragAndDrop({
22-
formatConstructors: [
23-
new GPX(),
24-
new GeoJSON(),
25-
new IGC(),
26-
new KML(),
27-
new TopoJSON(),
28-
],
32+
formatConstructors: formats || defaultFormats
2933
});
3034
dragAndDropInteraction.on('addfeatures', function (event: any) {
3135
const vectorSource = new VectorSource({
@@ -34,6 +38,7 @@ function addDragAndDropToMap(map: Map): void {
3438
map.addLayer(
3539
new VectorLayer({
3640
source: vectorSource,
41+
style: style || undefined
3742
}),
3843
);
3944
map.getView().fit(vectorSource.getExtent());

srcjs/ipywidget-ts/map.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import type VectorSource from "ol/source/Vector";
1515
import type VectorLayer from "ol/layer/Vector";
1616
import type WebGLVectorLayer from "ol/layer/WebGLVector";
1717
import type { Coordinate } from "ol/coordinate";
18+
import type FeatureFormat from "ol/format/Feature";
19+
import type { FlatStyle } from "ol/style/flat";
1820
import type { MyMapOptions } from ".";
1921

2022
import type { AnyModel } from "@anywidget/types";
@@ -256,7 +258,9 @@ export default class MapWidget {
256258
addSelectFeaturesToMap(this._map, this._model);
257259
}
258260

259-
addDragAndDropVectorLayers(formats?: any[]): void {
260-
addDragAndDropVectorLayersToMap(this._map);
261+
addDragAndDropVectorLayers(formatsDef?: JSONDef[], style?: FlatStyle): void {
262+
const formats = formatsDef?.map(item => jsonConverter.parse(item));
263+
console.log("drag and drop formats", formats);
264+
addDragAndDropVectorLayersToMap(this._map, formats, style);
261265
}
262266
}

0 commit comments

Comments
 (0)