Skip to content

Commit 4e5ec95

Browse files
committed
Add method to set source
1 parent 66115e1 commit 4e5ec95

File tree

6 files changed

+179
-78
lines changed

6 files changed

+179
-78
lines changed

notebooks/geopandas/icon_layer.ipynb

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"outputs": [],
99
"source": [
1010
"import openlayers as ol\n",
11-
"from openlayers.geopandas import GeoDataFrame"
11+
"from openlayers.geopandas import GeoDataFrame, gdf_to_geojson"
1212
]
1313
},
1414
{
@@ -212,32 +212,32 @@
212212
},
213213
{
214214
"cell_type": "code",
215-
"execution_count": 7,
215+
"execution_count": 5,
216216
"id": "fa678da4-ead3-489c-9296-5907060d9077",
217217
"metadata": {},
218218
"outputs": [],
219219
"source": [
220-
"m = gdf.ol.icon(icon_src=\"https://upload.wikimedia.org/wikipedia/commons/7/7c/201408_cat.png\", icon_scale=0.1).explore()"
220+
"m = gdf[0:10].ol.icon(icon_src=\"https://upload.wikimedia.org/wikipedia/commons/7/7c/201408_cat.png\", icon_scale=0.1).explore()"
221221
]
222222
},
223223
{
224224
"cell_type": "code",
225-
"execution_count": 9,
225+
"execution_count": 6,
226226
"id": "f25f4d6d-bf6a-4c0c-9f99-99382b8e9dfb",
227227
"metadata": {},
228228
"outputs": [
229229
{
230230
"data": {
231231
"application/vnd.jupyter.widget-view+json": {
232-
"model_id": "4b717fb6022b4d05b03bfda16129f179",
232+
"model_id": "158bb835125f444080430a06ea50d3fc",
233233
"version_major": 2,
234234
"version_minor": 1
235235
},
236236
"text/plain": [
237237
"MapWidget(calls=[{'method': 'addLayer', 'args': ({'id': 'geopandas', 'source': {'@@geojson': {'type': 'Feature…"
238238
]
239239
},
240-
"execution_count": 9,
240+
"execution_count": 6,
241241
"metadata": {},
242242
"output_type": "execute_result"
243243
}
@@ -248,18 +248,94 @@
248248
},
249249
{
250250
"cell_type": "code",
251-
"execution_count": 1,
251+
"execution_count": 12,
252252
"id": "74dea72b-2158-4347-984f-6d62c130c01e",
253253
"metadata": {},
254+
"outputs": [
255+
{
256+
"data": {
257+
"text/plain": [
258+
"{'layers': [{'id': 'osm', 'type': 'TileLayer'},\n",
259+
" {'id': 'geopandas', 'type': 'WebGLVectorLayer'}],\n",
260+
" 'controls': []}"
261+
]
262+
},
263+
"execution_count": 12,
264+
"metadata": {},
265+
"output_type": "execute_result"
266+
}
267+
],
268+
"source": [
269+
"m.metadata"
270+
]
271+
},
272+
{
273+
"cell_type": "code",
274+
"execution_count": 12,
275+
"id": "beca60c5-3925-45f1-a7d5-1a409deb6272",
276+
"metadata": {},
277+
"outputs": [],
278+
"source": [
279+
"# m.add_call(\"setVectorSource\", \"geopandas\", ol.VectorSource(geojson=gdf_to_geojson(gdf[20:100])).model_dump())"
280+
]
281+
},
282+
{
283+
"cell_type": "code",
284+
"execution_count": 8,
285+
"id": "dcc1f821-ecf7-4bf0-8dab-7b6e141824e5",
286+
"metadata": {},
287+
"outputs": [],
288+
"source": [
289+
"new_src = ol.VectorSource(geojson=gdf_to_geojson(gdf[20:100]))"
290+
]
291+
},
292+
{
293+
"cell_type": "code",
294+
"execution_count": 9,
295+
"id": "41c064ca-b232-49a5-a597-ce69746446da",
296+
"metadata": {},
254297
"outputs": [],
255298
"source": [
256-
"# gdf[0:10]"
299+
"m.set_source(layer_id=\"geopandas\", source=new_src)"
300+
]
301+
},
302+
{
303+
"cell_type": "code",
304+
"execution_count": 10,
305+
"id": "24a5bdfa-a5f0-4493-999a-89f5876a57d8",
306+
"metadata": {},
307+
"outputs": [],
308+
"source": [
309+
"m.remove_layer(\"osm\")"
310+
]
311+
},
312+
{
313+
"cell_type": "code",
314+
"execution_count": 13,
315+
"id": "31fdcb7d-b5b0-4755-adb2-d192e7e89114",
316+
"metadata": {},
317+
"outputs": [
318+
{
319+
"data": {
320+
"text/plain": [
321+
"{'layers': [{'id': 'osm', 'type': 'TileLayer'},\n",
322+
" {'id': 'geopandas', 'type': 'WebGLVectorLayer'}],\n",
323+
" 'controls': []}"
324+
]
325+
},
326+
"execution_count": 13,
327+
"metadata": {},
328+
"output_type": "execute_result"
329+
}
330+
],
331+
"source": [
332+
"m.metadata"
257333
]
258334
},
259335
{
260336
"cell_type": "code",
261337
"execution_count": null,
262-
"id": "beca60c5-3925-45f1-a7d5-1a409deb6272",
338+
"id": "50db0b70-4efc-4f68-a6ec-e3bab5dde7be",
263339
"metadata": {},
264340
"outputs": [],
265341
"source": []

src/openlayers/js/openlayers.anywidget.js

Lines changed: 34 additions & 34 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: 34 additions & 34 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: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .models.controls import ControlT
1010
from .models.layers import LayerT, TileLayer
1111
from .models.map_options import MapOptions
12-
from .models.sources import OSM
12+
from .models.sources import OSM, SourceT
1313
from .models.view import View
1414
from .styles import FlatStyle
1515

@@ -182,6 +182,18 @@ def set_style(self, layer_id: str, style: FlatStyle | dict) -> None:
182182

183183
self.add_layer_call(layer_id, "setStyle", style)
184184

185+
def set_source(self, layer_id: str, source: SourceT | dict) -> None:
186+
"""Set / Update the source of a layer
187+
188+
Args:
189+
layer_id {str}: The ID of the layer
190+
source (SourceT | dict): The source of the layer
191+
"""
192+
if isinstance(source, SourceT):
193+
source = source.model_dump()
194+
195+
self.add_call("setSource", layer_id, source)
196+
185197
def to_html(self, **kwargs) -> str:
186198
"""Render map to HTML"""
187199
data = self.options | dict(calls=self.calls)

srcjs/ipywidget-ts/anywidget.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ function render({ model, el }: { model: AnyModel; el: HTMLElement }): void {
6969
try {
7070
// @ts-expect-error
7171
mapWidget[msg.method](...msg.args);
72+
73+
// TODO: Move to 'map.ts'
7274
updateModelMetadata();
7375
} catch (error) {
7476
console.log("error in anywidget msg call", error);

srcjs/ipywidget-ts/map.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ export default class MapWidget {
197197
layer[call.method](...call.args)
198198
}
199199

200+
setSource(layerId: string, sourceDef: JSONDef): void {
201+
const layer = this.getLayer(layerId);
202+
if (layer) {
203+
const source = jsonConverter.parse(sourceDef);
204+
layer.setSource(source);
205+
const features = sourceDef[GEOJSON_IDENTIFIER];
206+
if (features)
207+
source.addFeatures(new GeoJSON().readFeatures(features));
208+
}
209+
}
210+
200211
// --- Control methods
201212
getControl(controlId: string): Control | undefined {
202213
for (let control of this._map.getControls().getArray()) {

0 commit comments

Comments
 (0)