Skip to content
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

rio-tiler layer #413

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lonboard/_layer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .core import (
BaseArrowLayer,
BaseLayer,
BitmapLayer,
BitmapTileLayer,
ColumnLayer,
HeatmapLayer,
PathLayer,
PointCloudLayer,
PolygonLayer,
ScatterplotLayer,
SolidPolygonLayer,
)
35 changes: 32 additions & 3 deletions lonboard/_layer.py → lonboard/_layer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,42 @@ class BitmapTileLayer(BaseLayer):
```
"""

def __init__(self, **kwargs: BitmapTileLayerKwargs):
super().__init__(**kwargs) # type: ignore
called = 0

def __init__(self, **kwargs: Unpack[BitmapTileLayerKwargs]):
def _handle_anywidget_dispatch(
widget: ipywidgets.Widget, msg: Union[str, list, dict], buffers: List[bytes]
) -> None:
self.called += 1
print(msg)

if not isinstance(msg, dict) or msg.get("kind") != "anywidget-command":
return

print("test")
print(msg)

response = "helloworld from init"
buffers = [b"hello world"]
self.send(
{
"id": msg["id"],
"kind": "anywidget-command-response",
"response": response,
},
buffers,
)

print("before on msg")
self.on_msg(_handle_anywidget_dispatch)
super().__init__(**kwargs)

_layer_type = traitlets.Unicode("bitmap-tile").tag(sync=True)

data = traitlets.Union(
[traitlets.Unicode(), traitlets.List(traitlets.Unicode(), minlen=1)]
[traitlets.Unicode(), traitlets.List(traitlets.Unicode(), minlen=1)],
default_value=None,
allow_none=True,
).tag(sync=True)
"""
Either a URL template or an array of URL templates from which the tile data should
Expand Down
105 changes: 105 additions & 0 deletions lonboard/_layer/rasterio_layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from __future__ import annotations

import traceback
from typing import Unpack

from rio_tiler.io.base import BaseReader

# from rio_tiler.io import Reader
# from rio_tiler.models import ImageData
from lonboard.types.layer import BitmapTileLayerKwargs

from .core import BitmapTileLayer

# # path = "/Users/kyle/Downloads/m_1806551_nw_20_030_20221212_20230329.tif"
# path = "/Users/kyle/github/developmentseed/lonboard/m_4007307_sw_18_060_20220803.tif"
# reader = Reader(path)
# reader.geographic_bounds
# reader.tms
# reader.bounds
# test = reader.tile(2601, 3674, 13)
# dir(test)
# # test.mask.all()
# attrs.evolve(test, data=test.data[:3, :, :])
# ImageData()
# image_data = reader.tile(0, 0, 0)

# with open("tmp.png", "wb") as f:
# f.write(image_data.render())


def handle_anywidget_dispatch(
widget: RioTilerLayer, msg: str | list | dict, buffers: list[bytes]
) -> None:
# widget.called += 1
print(msg)

if not isinstance(msg, dict) or msg.get("kind") != "anywidget-command":
return

try:
content = msg["msg"]
tile_id = content["tile_id"]
tile_x, tile_y, tile_z = tile_id.split("-")
tile_x = int(tile_x)
tile_y = int(tile_y)
tile_z = int(tile_z)

if tile_z < widget.min_zoom:
return widget.send(
{
"id": msg["id"],
"kind": "anywidget-command-response",
"response": {},
},
[],
)

image_data = widget.reader.tile(int(tile_x), int(tile_y), int(tile_z))

# test.evolve
# rio_tiler.
image_buf = image_data.render(add_mask=True)
buffers = [image_buf]

response = "helloworld from init"
widget.send(
{
"id": msg["id"],
"kind": "anywidget-command-response",
"response": response,
},
buffers,
)
except: # noqa: E722
response = traceback.format_exc()
widget.send(
{
"id": msg["id"],
"kind": "anywidget-command-response",
"response": response,
},
buffers,
)


class RioTilerLayer(BitmapTileLayer):
reader: BaseReader

def __init__(
self,
reader: BaseReader,
*,
min_zoom: int = 10,
**kwargs: Unpack[BitmapTileLayerKwargs],
):
self.reader = reader
self.min_zoom = min_zoom

extent = reader.geographic_bounds
kwargs["extent"] = tuple(extent)

self.on_msg(handle_anywidget_dispatch)
super().__init__(**kwargs) # type: ignore

pass
Loading