Skip to content

Commit

Permalink
Fix as_leaflet_layer
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Aug 10, 2022
1 parent 59f964d commit 41e46ea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
15 changes: 5 additions & 10 deletions docs/source/layers/layer_like.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ Downstream objects should implement an ``as_leaflet_layer`` method that returns
an ``ipyleaflet`` type capable of being added to the ``Map``.

Here is a simple example of creating a custom data class to hold heatmap data
(coordinates with some numerical value).
(coordinates with some numerical value). We can use that custom data class and
because it has an ``as_leaflet_layer`` interface, we can pass the object
directly to :func:`ipyleaflet.Map.add`.



.. jupyter-execute::

import numpy as np
from ipyleaflet import Map


class MyHeatMap:
Expand All @@ -39,15 +43,6 @@ Here is a simple example of creating a custom data class to hold heatmap data
radius=self.radius,
)

We can now use that custom data class and because it has an
``as_leaflet_layer`` interface, we can pass the object directly to
:func:`ipyleaflet.Map.add`.


.. jupyter-execute::

from ipyleaflet import Map

n = 1000
data = MyHeatMap(
np.random.uniform(-80, 80, (n, 2)),
Expand Down
5 changes: 3 additions & 2 deletions ipyleaflet/leaflet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,8 +1172,6 @@ def add(self, layer):

if isinstance(layer, dict):
layer = basemap_to_tiles(layer)
elif hasattr(layer, 'as_leaflet_layer'):
layer = layer.as_leaflet_layer()
if layer.model_id in self._layer_ids:
raise LayerException('layer already in layergroup: %r' % layer)
self.layers = tuple([layer for layer in self.layers] + [layer])
Expand Down Expand Up @@ -2525,6 +2523,9 @@ def add(self, item):
item: Layer or Control instance
The layer or control to add.
"""
if hasattr(item, 'as_leaflet_layer'):
item = item.as_leaflet_layer()

if isinstance(item, Layer):
if isinstance(item, dict):
item = basemap_to_tiles(item)
Expand Down

0 comments on commit 41e46ea

Please sign in to comment.