Skip to content

Commit

Permalink
added remove funtion
Browse files Browse the repository at this point in the history
  • Loading branch information
Nour-Cheour10 committed Aug 7, 2024
1 parent bb8679e commit 9c643ca
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 14 deletions.
69 changes: 60 additions & 9 deletions examples/RasterLayer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"metadata": {},
"outputs": [],
"source": [
"from ipyopenlayers import Map, RasterTileLayer,SplitMapControl"
"from ipyopenlayers import Map, RasterTileLayer,SplitMapControl, ZoomSlider"
]
},
{
Expand All @@ -38,7 +38,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0711a71bef6046faa26be5e90a5eb534",
"model_id": "118567532fa24b9bbfe23392434a10ff",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -92,7 +92,42 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"zoom=ZoomSlider()\n",
"m.add_control(zoom)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "118567532fa24b9bbfe23392434a10ff",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map(center=[0.0, 0.0], layers=[RasterTileLayer()], zoom=3.1446582428318823)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m.remove(zoom)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -101,7 +136,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -110,16 +145,32 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "118567532fa24b9bbfe23392434a10ff",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map(center=[0.0, 0.0], layers=[RasterTileLayer()], zoom=3.1446582428318823)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m.remove_control(split)"
"m.remove(split)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"outputs": [
{
Expand All @@ -128,7 +179,7 @@
"[]"
]
},
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
25 changes: 24 additions & 1 deletion ipyopenlayers/openlayers.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,31 @@ def add_control(self, control):

def remove_control(self, control):
self.controls = [x for x in self.controls if x != control]



def remove(self, item):
"""Remove an item from the map : either a layer or a control.
Parameters
----------
item: Layer or Control instance
The layer or control to remove.
"""
if isinstance(item, Layer):
self.layers = tuple(
[layer for layer in self.layers if layer.model_id != item.model_id]
)

elif isinstance(item, BaseControl):
self.controls = tuple(
[
control
for control in self.controls
if control.model_id != item.model_id
]
)
return self

def clear_layers(self):
self.layers = []

Expand Down
4 changes: 3 additions & 1 deletion src/splitcontrol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export default class SplitMapControl extends Control {
const bottomLeft = getRenderPixel(event, [0, mapSize[1]]);
const topRight = getRenderPixel(event, [mapSize[0], 0]);

const width = Math.round((topRight[0] - bottomLeft[0]) * (this._swipe_position / 100));
const width = Math.round(
(topRight[0] - bottomLeft[0]) * (this._swipe_position / 100),
);
const height = topRight[1] - bottomLeft[1];

gl.scissor(bottomLeft[0], bottomLeft[1], width, height);
Expand Down
4 changes: 3 additions & 1 deletion src/splitmapcontrol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export class SplitMapControlView extends BaseControlView {
this.map_view = this.options.map_view;
this.map_view.layer_views = this.options.map_view.layerViews;
if (this.map_view && !this.map_view.layerViews) {
console.warn('Layer views is not initialized. Ensure it is properly set.');
console.warn(
'Layer views is not initialized. Ensure it is properly set.',
);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export * from './vectortilelayer';
export * from './splitmapcontrol';
export * from './splitcontrol';


const DEFAULT_LOCATION = [0.0, 0.0];

export class MapModel extends DOMWidgetModel {
Expand Down Expand Up @@ -192,7 +191,7 @@ export class MapView extends DOMWidgetView {
this.map.removeControl(child_view.obj);
child_view.remove();
}

async addLayerModel(child_model: LayerModel) {
const view = await this.create_child_view<LayerView>(child_model, {
map_view: this,
Expand Down

0 comments on commit 9c643ca

Please sign in to comment.