Skip to content

Commit 1cb79be

Browse files
author
Stefan Kuethe
committed
Refactor adding layers
1 parent 2e40c00 commit 1cb79be

File tree

3 files changed

+95
-52
lines changed

3 files changed

+95
-52
lines changed

notebooks/layers/Untitled.ipynb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
{
5353
"data": {
5454
"application/vnd.jupyter.widget-view+json": {
55-
"model_id": "b5de21d0df90434b9cb84b729fcfcce9",
55+
"model_id": "7769c318a0154c1b9303dd2994ce4035",
5656
"version_major": 2,
5757
"version_minor": 1
5858
},
@@ -93,17 +93,18 @@
9393
},
9494
{
9595
"cell_type": "code",
96-
"execution_count": 11,
96+
"execution_count": 7,
9797
"id": "ea76d744",
9898
"metadata": {},
9999
"outputs": [],
100100
"source": [
101-
"m.add_layer(BasemapLayer.carto(\"light_all\"))"
101+
"# m.add_layer(BasemapLayer.carto(\"light_all\"))\n",
102+
"m.remove_layer(\"carto-light-all\")"
102103
]
103104
},
104105
{
105106
"cell_type": "code",
106-
"execution_count": 13,
107+
"execution_count": 6,
107108
"id": "1aa4155d",
108109
"metadata": {},
109110
"outputs": [],
@@ -114,13 +115,15 @@
114115
},
115116
{
116117
"cell_type": "code",
117-
"execution_count": 15,
118+
"execution_count": 8,
118119
"id": "2e7cee1d",
119120
"metadata": {},
120121
"outputs": [],
121122
"source": [
122123
"# m.add_layer(countries)\n",
123-
"m.add_tooltip()\n",
124+
"\n",
125+
"m.remove_layer(countries.id)\n",
126+
"# m.add_tooltip()\n",
124127
"#import geopandas as gpd\n",
125128
"#from openlayers.geopandas import OLGeoDataFrame"
126129
]

src/openlayers/js/openlayers.anywidget.js

Lines changed: 25 additions & 25 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: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,51 @@ import type { Coordinate } from "ol/coordinate";
3535
// ...
3636
type LayerStore = {
3737
[key: string]: Layer;
38-
}
38+
};
3939

4040
//
4141
type ControlStore = {
4242
[key: string]: Control;
43-
}
43+
};
44+
45+
//
46+
type Metadata = {
47+
layers: any[]; //{ [key: string]: any };
48+
controls: any[]; //{ [key: string]: any };
49+
};
50+
51+
const TYPE_IDENTIFIER = "@@type";
52+
const GEOJSON_IDENTIFIER = "@@geojson";
4453

4554
const jsonConverter = new JSONConverter();
4655

47-
/*
48-
const defaultLayers = [
49-
new TileLayer({
50-
source: new OSM()
51-
})
52-
];
53-
*/
56+
function parseLayerDef(layerDef: JSONDef): Layer {
57+
const layer = jsonConverter.parse(layerDef);
58+
layer.set("id", layerDef.id);
59+
addGeojsonFeatures(layer, layerDef[GEOJSON_IDENTIFIER]);
60+
return layer;
61+
}
62+
63+
function addGeojsonFeatures(layer: Layer, features: any): void {
64+
if (features === undefined)
65+
return;
66+
67+
const source = layer.getSource() as VectorSource;
68+
source.addFeatures(new GeoJSON().readFeatures(features));
69+
console.log("geojson features added", features);
70+
}
71+
5472

5573
export default class MapWidget {
5674
_container: HTMLElement;
5775
_map: Map;
76+
_metadata: Metadata = { layers: [], controls: [] };
5877
_layerStore: LayerStore = {};
5978
_controlStore: ControlStore = {};
6079

6180
constructor(mapElement: HTMLElement, mapOptions: MyMapOptions) {
6281
let baseLayers: Layer[] = [] // defaultLayers;
82+
/*
6383
if (mapOptions.layers !== undefined) {
6484
for (let layerJSONDef of mapOptions.layers) {
6585
@@ -73,7 +93,7 @@ export default class MapWidget {
7393
this._layerStore[layerJSONDef.id] = layer;
7494
}
7595
}
76-
96+
*/
7797

7898
let baseControls: Control[] = [];
7999
// TODO: Use 'addControls' after map was created instead
@@ -103,6 +123,11 @@ export default class MapWidget {
103123
controls: defaultControls().extend(baseControls),
104124
layers: baseLayers,
105125
});
126+
127+
// Add layers
128+
for (let layerDef of mapOptions.layers || []) {
129+
this.addLayer(layerDef);
130+
}
106131
}
107132

108133
// TODO: Obsolete
@@ -127,13 +152,6 @@ export default class MapWidget {
127152
return this._layerStore[layerId];
128153
}
129154

130-
getLayer(layerId: string): Layer | undefined {
131-
for (let layer of this._map.getLayers().getArray()) {
132-
if (layer.get("id") === layerId)
133-
return layer as Layer;
134-
}
135-
}
136-
137155
// TODO: Obsolete
138156
getControlOld(controlId: string): Control {
139157
return this._controlStore[controlId];
@@ -166,7 +184,8 @@ export default class MapWidget {
166184
console.log("geojsonObject added to VectorSource", geoJSONObject);
167185
}
168186

169-
addLayer(layerJSONDef: JSONDef): void {
187+
/*
188+
addLayerOld(layerJSONDef: JSONDef): void {
170189
const layer = jsonConverter.parse(layerJSONDef);
171190
layer.set("id", layerJSONDef.id);
172191
@@ -177,14 +196,35 @@ export default class MapWidget {
177196
this._layerStore[layerJSONDef.id] = layer;
178197
console.log("layerStore", this._layerStore);
179198
}
199+
*/
200+
201+
getLayer(layerId: string): Layer | undefined {
202+
for (let layer of this._map.getLayers().getArray()) {
203+
if (layer.get("id") === layerId)
204+
return layer as Layer;
205+
}
206+
}
207+
208+
addLayer(layerDef: JSONDef): void {
209+
const layer = parseLayerDef(layerDef);
210+
this._map.addLayer(layer);
211+
this._metadata.layers.push({
212+
id: layer.get("id"),
213+
type: layerDef[TYPE_IDENTIFIER],
214+
extent: layer.getExtent()
215+
});
216+
console.log("layer", layer.get("id"), "added", this._metadata);
217+
}
180218

181219
removeLayer(layerId: string): void {
182220
const layer = this.getLayer(layerId);
183-
if (layer === undefined) return;
221+
if (layer === undefined)
222+
return;
184223

185224
this._map.removeLayer(layer);
186-
delete this._layerStore[layerId];
187-
console.log("layer", layerId, "removed", this._layerStore);
225+
// delete this._layerStore[layerId];
226+
this._metadata.layers = this._metadata.layers.filter(item => item["id"] != layerId);
227+
console.log("layer", layerId, "removed", this._metadata);
188228
}
189229

190230
addControl(controlJSONDef: JSONDef): void {

0 commit comments

Comments
 (0)