Skip to content

refactor(deck.gl-raster): handle every layer's source in RasterTileLayer - #805

Merged
kylebarron merged 10 commits into
mainfrom
kyle/raster-tile-layer-source
Oct 27, 2026
Merged

kylebarron merged 10 commits into
mainfrom
kyle/raster-tile-layer-source

Conversation

@kylebarron

@kylebarron kylebarron commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Note

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

Stage 4a of the Sources design, dev-docs/specs/2026-10-22-sources-design.md. RasterTileLayer now handles the source prop for every layer that takes one (question 5). COGLayer and MultiCOGLayer drop their copies of that code, and ZarrLayer won't need a third copy in 4b.

A layer that takes a source still declares its source and onSourceLoad props, which document its inputs (question 6). It also says how to open them:

// The source types come from COGLayerProps' `source: COGSource | COGInput`
class COGLayer extends RasterTileLayer<DataT, COGLayerProps<DataT>> {
  protected override _openSource(input: COGInput, signal: AbortSignal) {
    return COGSource.open(input, { signal });
  }
}

RasterTileLayer does the rest:

  • Types: two type parameters, SourceT and InputT, default to the two parts of the layer's source prop, here COGSource and COGInput. They type source, onSourceLoad, _openSource, _sourceState and state.source, so the base reads the props without casts, and a layer passes no extra type arguments.
  • An opened source is drawn at once, and any open in flight is aborted.
  • Other input is opened in loadMetadata through _openSource. A result that arrives after source changed is dropped. Otherwise onSourceLoad gets it, and the layer draws it.
  • undefined and null draw nothing.
  • State: the source is kept in state.source, and the tile grid comes from it.
  • _sourceState(source) returns more state to keep with a source. COGLayer infers its default pipeline there. If it throws, the layer reports the error through onError and draws nothing.

Question 5's proposal had a second hook per layer, isOpenedSource. instanceof RasterSource replaces it, since every source extends RasterSource (decision 10). The hooks start with an underscore, like every other protected method in the repo.

Behavior changes

Apart from these three changes, COGLayer and MultiCOGLayer behave as before.

  • An opened COG that can't be drawn no longer leaves the old one on the map.
    • Since 2a, giving COGLayer an opened COG that its default pipeline can't draw, such as a float32 COG, threw from updateState.
    • deck.gl then skips renderLayers. The layer kept drawing the previous COG, and fetching its tiles, until another prop changed.
    • Now the layer reports the error through onError, as "Can't draw the source in COGLayer: ...", and draws nothing.
  • source: null draws nothing, like undefined. Before, COGLayer and MultiCOGLayer reported an error for it.
  • clearState() is gone from COGLayer and MultiCOGLayer. It was public, but undocumented.

Spec

The spec records the stage 4 decisions, 18 to 23:

  • Stage 4 lands as three PRs: 4a, 4b and 4c.
  • This PR's design.
  • Each layer declares its own source and onSourceLoad.
  • ZarrLayer takes one source. node, variable, metadata and epsgResolver move to ZarrSource.open options.
  • selection stays on the layer.
  • What ZarrSource holds.

The stage 4 entry now lists 4a, 4b and 4c. The 4c plan includes a texture leak to fix first: deck.gl's Tileset2D.reloadAll drops tiles without calling onTileUnload. Two new questions:

  • Question 9: how to name ZarrSource's parsed metadata, next to the raw metadata option.
  • Question 10: a ServerSource for tile servers, so that every layer takes a source. That one is for later.

Tests

  • RasterTileLayer: 14 tests with a test layer.
    • An opened source draws without opening anything.
    • Other input opens with the load signal, and onSourceLoad gets the source.
    • An open that source replaced is aborted, and its late result is dropped.
    • A removed or null source draws nothing.
    • State from _sourceState is stored, and its errors reach onError.
    • A layer without _openSource rejects input.
    • One test runs a real deck.gl LayerManager with a stub device. It checks that the old source's sublayer goes away when _sourceState throws. Tests that call renderLayers themselves can't see that.
  • COGLayer: the two tests for a COG that the default pipeline can't draw now expect onError instead of a throw. Its other tests pass unchanged.
  • MultiCOGLayer: new tests check that an empty record replacing an opened source draws nothing, and so does a null source.
  • Type tests:
    • COGLayer's state holds a COGSource, and MultiCOGLayer's a MultiCOGSource.
    • COGLayer's source takes nothing else, such as a number.
    • Defaulting InputT to any or never, or SourceT to RasterSource, fails them or the build.
  • Mutation checks: each of these makes tests fail:
    • not aborting the open in flight when source changes;
    • not clearing the old source before _sourceState;
    • dropping MultiCOGLayer's empty-record check;
    • throwing _sourceState's errors again;
    • opening a null source.
  • In Chrome: each example drew the new source with no errors, and looked as before:
    • cog-basic: URL input, with the source switched while the previous one was still opening.
    • sentinel-2: MultiCOGLayer, with the composite and the scene switched mid-open.
    • vermont-cog-comparison: opened sources, switched away and back.

🤖 Written by Claude Code

kylebarron and others added 4 commits October 26, 2026 18:48
…eLayer

COGLayer and MultiCOGLayer each handle their `source` prop with the same
code, and ZarrLayer would add a third copy. RasterTileLayer now does it
for any subclass that declares `source` and `onSourceLoad`:

- It draws an opened `RasterSource` at once, and aborts any open in
  flight.
- It opens other input in `loadMetadata`, through the subclass's
  `_openSource(input, signal)`, then calls `onSourceLoad`.
- It keeps the source in `state.source`, and takes the tile grid from it.

`_sourceState(source)` lets a subclass store more state with a source,
such as a default render pipeline. If it throws, the layer draws
nothing rather than the old source.

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

COGLayer and MultiCOGLayer drop their copies of the `source` handling,
`clearState()` included, and implement RasterTileLayer's hooks:

- COGLayer opens input with `COGSource.open`, and infers its default
  pipeline in `_sourceState`.
- MultiCOGLayer opens a record with `MultiCOGSource.open`. Its
  `shouldLoadMetadata` still skips an empty record, which draws nothing.

Their behavior doesn't change, and their existing tests pass as they
were. A new test pins that an empty record replacing an opened source
draws nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The spec records decisions 18 to 23:
- Stage 4 lands as three PRs.
- RasterTileLayer handles `source` (question 5, as built here).
- Each layer declares its own `source` and `onSourceLoad` (question 6).
- ZarrLayer takes one `source`, and its old props become
  `ZarrSource.open` options.
- `selection` stays on ZarrLayer.
- What `ZarrSource` holds, and where its projection helpers move.

Stage 4's bullet becomes 4a, 4b and 4c, with 4c's plan. That plan
includes unloading what deck.gl's `reloadAll` drops without calling
`onTileUnload`. A new question 9 asks how to name ZarrSource's parsed
metadata next to the raw `metadata` option.

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

When `_sourceState` threw for an opened `source`, the error escaped
`updateState`. deck.gl then skips `renderLayers`, so the layer kept its
old inner TileLayer: the map showed the previous source, and kept
fetching its tiles, until another prop changed. COGLayer has done this
since 2a, for a COG its default pipeline can't draw. RasterTileLayer
now reports the error through `onError` instead, with the prefix
"Can't draw the source in <layer>", on both the opened and the
shorthand path.

`source: null` now means no source, like `undefined`. Before, the base
opened it, and MultiCOGLayer threw from `updateState` and kept drawing
its old source.

A new test drives a real deck.gl LayerManager, since tests that call
`renderLayers` directly can't see the stale sublayer.

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.

let's try to remove these casts with better typing

* {@link COGSource.open}.
*/
protected override _openSource(
input: COGInput,

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.

this input type can be derived from a type on the class probably?

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.

Note

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

Mostly, yes. In 97b827f the input type is a type argument of the class: COGLayer extends RasterTileLayer<DataT, COGLayerProps<DataT>, COGSource, COGInput>. The base declares _openSource(input: InputT, signal): Promise<SourceT>, so TypeScript checks this override against COGInput and COGSource.

The annotation itself has to stay, because TypeScript doesn't infer an overriding method's parameter types from the base class. Without it, tsc 7.0.2 reports TS7006: input implicitly has an any type.

🤖 Written by Claude Code

* tiles over.
*
* Subclasses may supply this via state by overriding the protected
* Layers that take a `source` take the descriptor from it instead. Other

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.

In the future this will be irrelevant because all layers will take a source, right?

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.

Note

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

For subclasses, yes. ZarrLayer is the last one that overrides _tilesetDescriptor(), and it moves onto source in 4b, so I'll drop the "Other subclasses" sentence there.

Direct use is the exception. The titiler-cog example passes a tilesetDescriptor built from a tile server's TileMatrixSet, with no RasterSource behind it. RasterTileLayer could take an opened RasterSource too: it already draws one, and the new SourceT parameter would type it. But a tile server would then need a small RasterSource of its own, so I'd decide that separately.

🤖 Written by Claude Code

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.

But a tile server would then need a small RasterSource of its own, so I'd decide that separately.

I think a ServerSource could make sense... But something to think about later

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.

Note

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

Agreed. I added it to the spec as question 10, marked for later (1f9c559).

🤖 Written by Claude Code

*/
type SourceProps = {
/** An opened {@link RasterSource}, or input for the layer to open. */
source?: unknown;

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.

I'd like for the type of this to be better. Can we add a generic onto SourceProps?

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.

Note

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

Done in 97b827f. SourceProps<SourceT, InputT> types source as SourceT | InputT | null, and onSourceLoad as (source: SourceT) => void.

  • RasterTileLayer takes SourceT and InputT as type parameters. They default to never for layers without a source.
  • It adds SourceProps<SourceT, InputT> to its props, so this.props.source is typed in the base.
  • _openSource, _sourceState and state.source use the same types. COGLayer and MultiCOGLayer no longer redeclare source in their state.
  • Each layer still declares its own source and onSourceLoad, to document its inputs. TypeScript intersects those declarations with these.

The three older as unknown as RasterTileLayerProps<DataT> casts stay. They read tilesetDescriptor, getTileData and renderTile, which subclasses redeclare with other signatures, such as COGLayer's getTileData(image, options); that's why those props live in ExtraProps. Removing those casts would mean retyping those props. I can open an issue for that if you want.

🤖 Written by Claude Code


override updateState(params: UpdateParameters<this>): void {
super.updateState(params);
const { source } = params.props as SourceProps;

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.

similarly I don't like this cast.

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.

Note

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

Removed in 97b827f: params.props.source is typed now.

  • In the base, a private type guard, _isOpened(source): source is SourceT, replaces instanceof RasterSource. instanceof alone can't tell TypeScript that a RasterSource is this layer's SourceT.
  • loadMetadata now returns early unless source is input, which narrows it to InputT for _openSource.

🤖 Written by Claude Code

kylebarron and others added 2 commits October 27, 2026 16:23
…t casts

RasterTileLayer takes two more type parameters, `SourceT` and `InputT`.
They are the opened source a subclass draws and the input it opens, such
as COGLayer's `COGSource` and `COGInput`. `SourceProps<SourceT, InputT>`
adds `source` and `onSourceLoad` to the layer's props with those types,
so the base reads them without casts. `_openSource`, `_sourceState` and
`state.source` take the same types.

A private `_isOpened` type guard tells an opened source from input.
`loadMetadata` returns early for anything that isn't input, which also
narrows `source` to `InputT`.

COGLayer and MultiCOGLayer pass their source and input types, and drop
the `source` they declared in state. The test layers type their props
the same way, which also removes the test's casts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Decision 19 gains the two type parameters that type a layer's `source`.
Decision 20 now says RasterTileLayer adds `source` and `onSourceLoad`
to every layer's props, while each layer still declares its own, to
document its inputs. The Layers section's COGLayer sample shows the new
type arguments.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment on lines +118 to +121
DataT,
COGLayerProps<DataT>,
COGSource,
COGInput

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.

We're getting to have a lot of type parameters now:

  • DataT: type of data transferred from getTileData to renderTile
  • COGLayerProps: input props into the class
  • COGSource: type of source (e.g. COG, MultiCOG, Zarr)
  • COGInput: type of cog input...

It feels like a lot of type parameters?

Can we combine these at all and simplify them? Like COGLayerProps and COGInput seem perhaps overlapping?

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.

Note

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

Yes, they overlap: COGLayerProps already says source: COGSource | COGInput.

  • What changed: in 853357d, SourceT and InputT default to those two parts of the source prop. COGLayer and MultiCOGLayer pass two type arguments again, RasterTileLayer<DataT, COGLayerProps<DataT>>, as before this PR. A layer without a source gets never.
  • Why they're still type parameters: with defaults derived from the props, the class body keeps working with plain types.
  • Type tests (9048397): COGLayer's state holds a COGSource, and its source rejects a number. Defaulting InputT to any or never makes them fail.

The other two parameters stay:

  • DataT is the tile data apps can choose (COGLayer<DataT>).
  • The props type is what deck.gl's CompositeLayer types this.props with. COGLayerProps<DataT> repeats DataT because its getTileData and renderTile use it.

🤖 Written by Claude Code

* tiles over.
*
* Subclasses may supply this via state by overriding the protected
* Layers that take a `source` take the descriptor from it instead. Other

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.

But a tile server would then need a small RasterSource of its own, so I'd decide that separately.

I think a ServerSource could make sense... But something to think about later

kylebarron and others added 4 commits October 27, 2026 16:43
…ce prop

A layer's props already say what its `source` takes, such as COGLayer's
`source: COGSource | COGInput`. `SourceT` and `InputT` now default to
those two parts of the `source` prop in `ExtraProps`, so COGLayer and
MultiCOGLayer pass two type arguments again: `RasterTileLayer<DataT,
COGLayerProps<DataT>>`. A layer without a `source` gets `never`, as
before.

Type tests pin the derived types: COGLayer's state holds a `COGSource`,
MultiCOGLayer's a `MultiCOGSource`, and a COGLayer subclass can't
override `_openSource` with an input COGLayer's `source` doesn't take.
The test layer in deck.gl-raster declares its `source` prop instead of
passing type arguments.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The override test only checked COGLayer's own annotation of
`_openSource`, so a derived input type of `any` passed it. A source of
the wrong type is what such a type would let through.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Decision 19 says `SourceT` and `InputT` default to the two parts of a
layer's `source` prop, and the Layers sample goes back to two type
arguments. A new question 10 records the `ServerSource` idea for tile
servers, for later.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`SourceOf` and `InputOf` hid two one-line definitions behind names. The
defaults of `SourceT` and `InputT` now use `Extract` and `Exclude`
directly, with a comment showing what each gives for COGLayer, and
`SourcePropOf`'s doc explains its `keyof` check. The types don't
change.

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.

lgtm

@kylebarron
kylebarron merged commit 6c9da23 into main Oct 27, 2026
3 checks passed
@kylebarron
kylebarron deleted the kyle/raster-tile-layer-source branch October 27, 2026 21:07
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.

1 participant