-
Notifications
You must be signed in to change notification settings - Fork 363
Add support for as_leaflet_layer in Map.add
#1033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c29cf54
Add support for as_leaflet_layer in Map.add
banesullivan 07936b1
Improve docstring
banesullivan e2f70e6
Add documentation page
banesullivan 1b44348
Simplify example
banesullivan b82b4fe
Update docs/source/layers/layer_like.rst
banesullivan f5851cc
Update docs/source/layers/layer_like.rst
banesullivan 59cdf3a
Update docs/source/layers/layer_like.rst
banesullivan c1e9cc5
Minor rewording
davidbrochart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,4 +29,4 @@ Layers | |
| choropleth | ||
| vector_tile.rst | ||
| wkt_layer | ||
|
|
||
| layer_like | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| Layer-Like Objects | ||
| ================== | ||
|
|
||
| :class:`ipyleaflet.Map`'s :func:`ipyleaflet.Map.add` method supports | ||
| "layer-like" objects; meaning any object with an ``as_leaflet_layer`` method. | ||
| This interface can be especially useful for downstream developers who want | ||
| their users to more easily be able to add their objects to an | ||
| :class:`ipyleaflet.Map`. | ||
|
|
||
| Example | ||
| ------- | ||
|
|
||
| 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). | ||
|
|
||
|
|
||
| .. jupyter-execute:: | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
||
| class MyHeatMap: | ||
| def __init__(self, points, values, radius=20): | ||
| self.points = points | ||
| self.values = values | ||
| self.radius = 20 | ||
banesullivan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @property | ||
| def data(self): | ||
| return np.column_stack((self.points, self.values)) | ||
|
|
||
| def as_leaflet_layer(self): | ||
| from ipyleaflet import Heatmap | ||
| return Heatmap( | ||
| locations=self.data.tolist(), | ||
| 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)), | ||
| np.random.uniform(0, 1000, n), | ||
| ) | ||
|
|
||
| m = Map(center=(0, 0), zoom=2) | ||
| m.add(data) | ||
| m | ||
|
|
||
|
|
||
| External Examples | ||
| ----------------- | ||
|
|
||
| The following external libraries are working to implement this new interface | ||
|
|
||
| - `localtileserver <https://github.com/banesullivan/localtileserver>`_: a dynamic tile server built for visualizing large geospatial images/rasters with ipyleaflet. | ||
| - `xarray-leaflet <https://github.com/davidbrochart/xarray_leaflet>`_: an xarray extension for tiled map plotting, based on ipyleaflet. | ||
banesullivan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidbrochart and @martinRenou, please review this explanation and feel free to modify. This is what I came up with on a whim to document the interface.