|
| 1 | +Layer-Like Objects |
| 2 | +================== |
| 3 | + |
| 4 | +The :func:`ipyleaflet.Map.add` method supports |
| 5 | +"layer-like" objects; meaning any object with an ``as_leaflet_layer`` method. |
| 6 | +This interface can be especially useful for downstream developers who want |
| 7 | +their users to more easily be able to add their objects to an |
| 8 | +:class:`ipyleaflet.Map`. |
| 9 | + |
| 10 | +Example |
| 11 | +------- |
| 12 | + |
| 13 | +Downstream objects should implement an ``as_leaflet_layer`` method that returns |
| 14 | +an ``ipyleaflet`` type capable of being added to the ``Map``. |
| 15 | + |
| 16 | +Here is a simple example of creating a custom data class to hold heatmap data |
| 17 | +(coordinates with some numerical value). |
| 18 | + |
| 19 | + |
| 20 | +.. jupyter-execute:: |
| 21 | + |
| 22 | + import numpy as np |
| 23 | + |
| 24 | + |
| 25 | + class MyHeatMap: |
| 26 | + def __init__(self, points, values, radius=20): |
| 27 | + self.points = points |
| 28 | + self.values = values |
| 29 | + self.radius = radius |
| 30 | + |
| 31 | + @property |
| 32 | + def data(self): |
| 33 | + return np.column_stack((self.points, self.values)) |
| 34 | + |
| 35 | + def as_leaflet_layer(self): |
| 36 | + from ipyleaflet import Heatmap |
| 37 | + return Heatmap( |
| 38 | + locations=self.data.tolist(), |
| 39 | + radius=self.radius, |
| 40 | + ) |
| 41 | + |
| 42 | +We can now use that custom data class and because it has an |
| 43 | +``as_leaflet_layer`` interface, we can pass the object directly to |
| 44 | +:func:`ipyleaflet.Map.add`. |
| 45 | + |
| 46 | + |
| 47 | +.. jupyter-execute:: |
| 48 | + |
| 49 | + from ipyleaflet import Map |
| 50 | + |
| 51 | + n = 1000 |
| 52 | + data = MyHeatMap( |
| 53 | + np.random.uniform(-80, 80, (n, 2)), |
| 54 | + np.random.uniform(0, 1000, n), |
| 55 | + ) |
| 56 | + |
| 57 | + m = Map(center=(0, 0), zoom=2) |
| 58 | + m.add(data) |
| 59 | + m |
| 60 | + |
| 61 | + |
| 62 | +External Examples |
| 63 | +----------------- |
| 64 | + |
| 65 | +The following external libraries are working to implement this new interface |
| 66 | + |
| 67 | +- `localtileserver <https://github.com/banesullivan/localtileserver>`_: a dynamic tile server built for visualizing large geospatial images/rasters with ipyleaflet. |
| 68 | +- `xarray-leaflet <https://github.com/xarray-contrib/xarray_leaflet>`_: an xarray extension for tiled map plotting, based on ipyleaflet. |
0 commit comments