Skip to content

docs: Add an example rendering TiTiler NumPy tiles - #1327

Merged
kylebarron merged 8 commits into
mainfrom
kyle/titiler-numpy-tiles-example
Nov 13, 2026
Merged

kylebarron merged 8 commits into
mainfrom
kyle/titiler-numpy-tiles-example

Conversation

@kylebarron

@kylebarron kylebarron commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Note

This PR was written by Claude (Claude Code), not by @kylebarron.

Adds examples/raster-titiler.ipynb, which renders a Sentinel-2 true-color COG from titiler.xyz's .npy tile endpoint with RasterLayer's generic constructor: an async fetch callback that loads each tile with np.load, and a render callback that uses TiTiler's mask band as the alpha band. It's an example of the "any Python code" rendering that the COG examples describe, with a tile server as the source instead of a GeoTIFF.

The notebook passes morecantile's WebMercatorQuad as is, and extent keeps tile requests inside the scene. That needs deck.gl-raster 0.9 or later (developmentseed/deck.gl-raster#684), which main bundles since #1355. Lonboard 0.17.0 bundles 0.8.1, so the notebook requires lonboard>0.17.0. Don't publish the docs with this example until that release is out.

Also adds the example's screenshot to assets/, an entry in examples/index.md, and the docs nav entry.

Testing

I ran the notebook's code against titiler.xyz in the tests/ui browser harness, on a build of main. All 9 tiles of the scene loaded at zoom 9, no request fell outside extent, and the map matched the screenshot in assets/. The "Debugging the render callback" cells also work.

🤖 Written by Claude Code

Adds examples/raster-numpytiles.ipynb, which renders a Sentinel-2
true-color COG from titiler.xyz's `.npy` tile endpoint with
`RasterLayer`'s generic constructor: an async fetch callback that loads
each tile with `np.load`, and a render callback that uses TiTiler's mask
band as the alpha band. It's an example of the "any Python code"
rendering that the COG examples describe, with a tile server as the
source instead of a GeoTIFF.

The notebook sets the TileMatrixSet's `boundingBox` to the dataset's
bounds, since deck.gl-raster 0.8.1 requires one and only loads tiles
inside it. developmentseed/deck.gl-raster#684 removes the need.

Also adds the example's screenshot to assets/, an entry in
examples/index.md, and the docs nav entry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ds-release-bot ds-release-bot Bot added the docs label Oct 1, 2026
@kylebarron

Copy link
Copy Markdown
Member Author

@vincentsarago I haven't read through this yet, but this is what claude edited and appears to work.

The primary difference is that we inject a bounding box into the TMS. developmentseed/deck.gl-raster#684 tracks the upstream to infer TMS bounds if they're not provided

@vincentsarago vincentsarago left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

@kylebarron

Copy link
Copy Markdown
Member Author

A new HTTP client for each request. In JupyterLab, the tile callbacks run on a different asyncio event loop (in a kernel subshell thread) than the notebook's cells. An async HTTP client created or used in a cell must not be reused inside the fetch callback, so titiler_fetch_tile opens its own httpx2.AsyncClient for each tile.

When my claude limits are back, I want to ask about that

@kylebarron

Copy link
Copy Markdown
Member Author

Also note that the simplest way to render from titiler would be to have titiler return PNG buffers directly. Then render_tile could be a callback that just passes the input along.

@kylebarron
kylebarron marked this pull request as ready for review October 2, 2026 19:41
@kylebarron

Copy link
Copy Markdown
Member Author

A new HTTP client for each request. In JupyterLab, the tile callbacks run on a different asyncio event loop (in a kernel subshell thread) than the notebook's cells. An async HTTP client created or used in a cell must not be reused inside the fetch callback, so titiler_fetch_tile opens its own httpx2.AsyncClient for each tile.

When my claude limits are back, I want to ask about that

other than this question lgtm

@vincentsarago

Copy link
Copy Markdown
Member

@kylebarron here is what Claude told me

🤖

What happens in the kernel:

  1. The frontend sends widget messages on a subshell. Your JupyterLab 4.6.2 creates its kernel connection with commsOverSubshells = CommsOverSubshells.PerCommTarget by default (I checked jlab_core.*.js). Every comm_msg from a widget, including lonboard's tile requests, goes out with a subshellId. That subshell is created once per comm target, not per cell.

  2. ipykernel runs that subshell in its own thread. ipykernel 7.x supports subshells. In kernelbase.py, shell_channel_thread_main sends each message to the subshell named in its header. Each subshell is a separate thread with its own asyncio loop.

  3. lonboard starts the tile task on whichever loop is running. In _raster.py:105, asyncio.create_task(_handle_tile_request(...)) runs inside the comm handler, so the task lands on the subshell's loop. That includes your titiler_fetch_tile. Top-level await in a cell, such as await get_tilejson(...), runs on the main shell's loop instead.

  4. An httpx/httpx2.AsyncClient can't be shared across loops. Its connection pool (httpcore/anyio) is tied to the loop it first ran on. If you create the client in a cell and reuse it in the callback, you'd get errors like "attached to a different loop" or "Event loop is closed", or the request would hang.

No fix is needed. Creating a new client for each tile is correct and safe, and an example notebook doesn't need more than that.

If we really want to fix this, a solution seems to be:

Fix it in lonboard. handle_anywidget_dispatch could schedule _fetch_tile on the main kernel loop with asyncio.run_coroutine_threadsafe, so users never have to think about which loop runs their code. That's a real design change, though. It would bring back the blocking that subshells exist to avoid: tile fetches would wait behind a busy cell. It's worth discussing with Kyle, but not as part of this PR.

kylebarron added a commit that referenced this pull request Nov 13, 2026
> [!NOTE]
> This PR was written by Claude (Claude Code), not by @kylebarron.

Bumps:

- `@deck.gl/*` 9.4.0 →
[9.4.1](https://github.com/visgl/deck.gl/releases/tag/v9.4.1)
-
`@developmentseed/{deck.gl-raster,affine,morecantile,raster-reproject}`
0.8.1 →
[0.9.0](https://github.com/developmentseed/deck.gl-raster/releases/tag/v0.9.0)
→
[0.10.0](https://github.com/developmentseed/deck.gl-raster/releases/tag/v0.10.0)

The deck.gl overrides in `pnpm-workspace.yaml` go to `^9.4.1` too.
Without that, pnpm keeps every overridden deck.gl package at 9.4.0 and
only `@deck.gl/react` moves. deck.gl-raster 0.10 now depends on
`@developmentseed/range-reader`, so I added it to the commented-out
local-checkout overrides.

The breaking changes in deck.gl-raster 0.9 and 0.10 are in
`deck.gl-geotiff`, `deck.gl-zarr` and `geotiff`, which Lonboard doesn't
use. Lonboard only imports `RasterTileLayer`, `TileMatrixSetAdaptor` and
`MinimalTileData`, and needs no code changes.

Upstream fixes relevant to Lonboard:

- deck.gl-raster: tile textures are destroyed when a raster layer is
removed (developmentseed/deck.gl-raster#728), and a WebGL vertex array
no longer leaks per tile (developmentseed/deck.gl-raster#786).
- deck.gl-raster: missing TileMatrixSet bounds are inferred and `extent`
is honored (developmentseed/deck.gl-raster#684). The `TMSBoundingBox`
step in `examples/raster-titiler.ipynb` (#1327) should no longer be
needed. That isn't tested here.
- deck.gl: the MapLibre overlay container is removed with the overlay
(visgl/deck.gl#10899), and `GlobeController` can zoom past level 12
(visgl/deck.gl#10737).

The `beforeId` fix for interleaved MapLibre (visgl/deck.gl#10734) merged
after 9.4.1, so the `idle` workaround tracked in #1293 stays.

Supersedes Dependabot's #1350, which stops at deck.gl-raster 0.9.0.

Closes #1350

## Testing

- `pnpm build`, `pnpm check` and `pnpm test` (12 tests) pass.
- `uv run pytest` passes: 367 passed, 1 skipped, including `tests/ui`.
- `tsc --noEmit` reports the same 9 errors as `main`, and no new ones.
- No test covers `RasterLayer.from_geotiff`, the only path that uses
deck.gl-raster's `RasterTileLayer`. I checked it by hand with a
throwaway UI test. It opens a 2048×2048 EPSG:32618 COG with 3 overviews
and renders it with no basemap. Tiles reach the canvas on this branch
and on `main`, and the browser console output is the same on both.

🤖 Written by [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
kylebarron and others added 3 commits November 13, 2026 12:12
deck.gl-raster 0.9 infers a missing TileMatrixSet bounding box, and
`extent` limits tile requests to the scene. Require a lonboard release
after 0.17.0, which bundles deck.gl-raster 0.8.1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@kylebarron kylebarron left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅

@kylebarron
kylebarron merged commit 8f3641a into main Nov 13, 2026
8 checks passed
@kylebarron
kylebarron deleted the kyle/titiler-numpy-tiles-example branch November 13, 2026 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants