Skip to content

Commit 499b029

Browse files
author
Stefan Kuethe
committed
Save changed feature
1 parent 03bbc42 commit 499b029

File tree

4 files changed

+53
-42
lines changed

4 files changed

+53
-42
lines changed

TODOS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# TODOS
2+
3+
## STAC layers
4+
5+
https://openlayers.org/en/latest/examples/stac-item.html

notebooks/Untitled.ipynb renamed to notebooks/modify-interaction.ipynb

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
"metadata": {},
88
"outputs": [],
99
"source": [
10-
"from openlayers.anywidget import MapWidget\n",
11-
"from openlayers.json_defs import (\n",
12-
"WebGLTileLayer, GeoTIFFSource, WebGLVectorLayer,VectorSource, TileLayer)"
10+
"import openlayers as ol"
1311
]
1412
},
1513
{
1614
"cell_type": "code",
17-
"execution_count": 2,
15+
"execution_count": null,
1816
"id": "5f0015f6-9f02-4fcc-9949-a4d9d46515d3",
1917
"metadata": {},
2018
"outputs": [
@@ -30,14 +28,7 @@
3028
}
3129
],
3230
"source": [
33-
"source = GeoTIFFSource(\n",
34-
" sources=[\n",
35-
" {\n",
36-
" \"url\": 'https://s2downloads.eox.at/demo/EOxCloudless/2020/rgbnir/s2cloudless2020-16bits_sinlge-file_z0-4.tif',\n",
37-
" }\n",
38-
" ]\n",
39-
")\n",
40-
"source"
31+
"ol.GeoDataFrame.from_file()"
4132
]
4233
},
4334
{
@@ -322,7 +313,7 @@
322313
],
323314
"metadata": {
324315
"kernelspec": {
325-
"display_name": "Python 3 (ipykernel)",
316+
"display_name": ".venv",
326317
"language": "python",
327318
"name": "python3"
328319
},

src/openlayers/js/openlayers.anywidget.js

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

srcjs/ipywidget-ts/map.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { defaultControls } from "./controls";
1313

1414
// import { DrawControl } from "./custom-controls/draw";
1515

16+
import { featureToGeoJSON } from "./utils";
1617
import { addTooltipToMap } from "./tooltip";
1718
import { addEventListernersToMapWidget } from "./events";
1819
import { addSelectFeaturesToMap } from "./select-features";
@@ -37,7 +38,7 @@ type Metadata = {
3738

3839
// TODO: Rename to something like `FeatureStore`
3940
type Features = {
40-
[key: string]: any[];
41+
[layerId: string]: any[];
4142
}
4243

4344
const jsonConverter = new JSONConverter();
@@ -69,7 +70,7 @@ export default class MapWidget {
6970
_container: HTMLElement;
7071
_map: Map;
7172
_metadata: Metadata = { layers: [], controls: [] };
72-
// _features: Features = {};
73+
_features: Features = {};
7374
_model: AnyModel | undefined;
7475

7576
constructor(mapElement: HTMLElement, mapOptions: MyMapOptions, model?: AnyModel | undefined) {
@@ -263,13 +264,27 @@ export default class MapWidget {
263264
addDragAndDropVectorLayersToMap(this._map, formats, style);
264265
}
265266

267+
// Does not work for `WebGLVectorLayer`
266268
addModifyInteraction(layerId: string): void {
267269
const source = this.getLayer(layerId)?.getSource() as VectorSource;
268270
if (source) {
269271
const snap = new Snap({ source: source });
270272
this._map.addInteraction(snap);
271273
const modify = new Modify({ source: source });
272274
this._map.addInteraction(modify);
275+
276+
// Add event listener
277+
source.on("changefeature", (e) => {
278+
if (e.feature) {
279+
const feature = featureToGeoJSON(e.feature);
280+
console.log("feature changed", feature);
281+
if (this._model) {
282+
this._features[layerId] = [feature];
283+
this._model.set("features", this._features);
284+
this._model.save_changes();
285+
}
286+
}
287+
});
273288
}
274289
}
275290
}

0 commit comments

Comments
 (0)