Skip to content

Commit 9c643ca

Browse files
committed
added remove funtion
1 parent bb8679e commit 9c643ca

File tree

5 files changed

+91
-14
lines changed

5 files changed

+91
-14
lines changed

examples/RasterLayer.ipynb

+60-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"metadata": {},
1414
"outputs": [],
1515
"source": [
16-
"from ipyopenlayers import Map, RasterTileLayer,SplitMapControl"
16+
"from ipyopenlayers import Map, RasterTileLayer,SplitMapControl, ZoomSlider"
1717
]
1818
},
1919
{
@@ -38,7 +38,7 @@
3838
{
3939
"data": {
4040
"application/vnd.jupyter.widget-view+json": {
41-
"model_id": "0711a71bef6046faa26be5e90a5eb534",
41+
"model_id": "118567532fa24b9bbfe23392434a10ff",
4242
"version_major": 2,
4343
"version_minor": 0
4444
},
@@ -92,7 +92,42 @@
9292
},
9393
{
9494
"cell_type": "code",
95-
"execution_count": 7,
95+
"execution_count": 9,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"zoom=ZoomSlider()\n",
100+
"m.add_control(zoom)"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 10,
106+
"metadata": {},
107+
"outputs": [
108+
{
109+
"data": {
110+
"application/vnd.jupyter.widget-view+json": {
111+
"model_id": "118567532fa24b9bbfe23392434a10ff",
112+
"version_major": 2,
113+
"version_minor": 0
114+
},
115+
"text/plain": [
116+
"Map(center=[0.0, 0.0], layers=[RasterTileLayer()], zoom=3.1446582428318823)"
117+
]
118+
},
119+
"execution_count": 10,
120+
"metadata": {},
121+
"output_type": "execute_result"
122+
}
123+
],
124+
"source": [
125+
"m.remove(zoom)"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 11,
96131
"metadata": {},
97132
"outputs": [],
98133
"source": [
@@ -101,7 +136,7 @@
101136
},
102137
{
103138
"cell_type": "code",
104-
"execution_count": 8,
139+
"execution_count": 12,
105140
"metadata": {},
106141
"outputs": [],
107142
"source": [
@@ -110,16 +145,32 @@
110145
},
111146
{
112147
"cell_type": "code",
113-
"execution_count": 9,
148+
"execution_count": 14,
114149
"metadata": {},
115-
"outputs": [],
150+
"outputs": [
151+
{
152+
"data": {
153+
"application/vnd.jupyter.widget-view+json": {
154+
"model_id": "118567532fa24b9bbfe23392434a10ff",
155+
"version_major": 2,
156+
"version_minor": 0
157+
},
158+
"text/plain": [
159+
"Map(center=[0.0, 0.0], layers=[RasterTileLayer()], zoom=3.1446582428318823)"
160+
]
161+
},
162+
"execution_count": 14,
163+
"metadata": {},
164+
"output_type": "execute_result"
165+
}
166+
],
116167
"source": [
117-
"m.remove_control(split)"
168+
"m.remove(split)"
118169
]
119170
},
120171
{
121172
"cell_type": "code",
122-
"execution_count": 10,
173+
"execution_count": 12,
123174
"metadata": {},
124175
"outputs": [
125176
{
@@ -128,7 +179,7 @@
128179
"[]"
129180
]
130181
},
131-
"execution_count": 10,
182+
"execution_count": 12,
132183
"metadata": {},
133184
"output_type": "execute_result"
134185
}

ipyopenlayers/openlayers.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,31 @@ def add_control(self, control):
166166

167167
def remove_control(self, control):
168168
self.controls = [x for x in self.controls if x != control]
169-
170169

170+
171+
def remove(self, item):
172+
"""Remove an item from the map : either a layer or a control.
173+
174+
Parameters
175+
----------
176+
item: Layer or Control instance
177+
The layer or control to remove.
178+
"""
179+
if isinstance(item, Layer):
180+
self.layers = tuple(
181+
[layer for layer in self.layers if layer.model_id != item.model_id]
182+
)
183+
184+
elif isinstance(item, BaseControl):
185+
self.controls = tuple(
186+
[
187+
control
188+
for control in self.controls
189+
if control.model_id != item.model_id
190+
]
191+
)
192+
return self
193+
171194
def clear_layers(self):
172195
self.layers = []
173196

src/splitcontrol.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ export default class SplitMapControl extends Control {
6262
const bottomLeft = getRenderPixel(event, [0, mapSize[1]]);
6363
const topRight = getRenderPixel(event, [mapSize[0], 0]);
6464

65-
const width = Math.round((topRight[0] - bottomLeft[0]) * (this._swipe_position / 100));
65+
const width = Math.round(
66+
(topRight[0] - bottomLeft[0]) * (this._swipe_position / 100),
67+
);
6668
const height = topRight[1] - bottomLeft[1];
6769

6870
gl.scissor(bottomLeft[0], bottomLeft[1], width, height);

src/splitmapcontrol.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export class SplitMapControlView extends BaseControlView {
4949
this.map_view = this.options.map_view;
5050
this.map_view.layer_views = this.options.map_view.layerViews;
5151
if (this.map_view && !this.map_view.layerViews) {
52-
console.warn('Layer views is not initialized. Ensure it is properly set.');
52+
console.warn(
53+
'Layer views is not initialized. Ensure it is properly set.',
54+
);
5355
}
5456
}
5557

src/widget.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export * from './vectortilelayer';
3535
export * from './splitmapcontrol';
3636
export * from './splitcontrol';
3737

38-
3938
const DEFAULT_LOCATION = [0.0, 0.0];
4039

4140
export class MapModel extends DOMWidgetModel {
@@ -192,7 +191,7 @@ export class MapView extends DOMWidgetView {
192191
this.map.removeControl(child_view.obj);
193192
child_view.remove();
194193
}
195-
194+
196195
async addLayerModel(child_model: LayerModel) {
197196
const view = await this.create_child_view<LayerView>(child_model, {
198197
map_view: this,

0 commit comments

Comments
 (0)