Skip to content

feat: add zRange to RasterTileLayer for draping COGs over terrain - #720

Merged
kylebarron merged 6 commits into
mainfrom
kyle/cog-terrain-example
Oct 16, 2026
Merged

kylebarron merged 6 commits into
mainfrom
kyle/cog-terrain-example

Conversation

@kylebarron

@kylebarron kylebarron commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Note

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

Adds an experimental example that drapes COGs over 3D terrain with deck.gl's _TerrainExtension. It also adds a zRange prop to RasterTileLayer, and so to COGLayer and ZarrLayer, so that tiles are selected for the terrain's heights.

Draping

Draping itself needs no library change:

  • A TerrainLayer with operation: "terrain+draw" is the terrain source.
  • A COGLayer with extensions: [new _TerrainExtension()] and terrainDrawMode: "drape" is rendered onto it.
  • For each terrain tile, deck.gl draws every COG tile whose WGS84 bounds overlap it into a texture, then drapes that texture over the terrain mesh. So the COG's projection and tile grid don't have to match the terrain's Web Mercator tiles.

terrainDrawMode: "drape" has to be set explicitly. Left unset, the extension picks "offset" for our mesh layer, because it inherits SimpleMeshLayer's instancePositions. Offset mode lifts only the reprojection mesh's vertices onto the terrain.

The example

examples/experimental/cog-terrain drapes three kinds of COG over AWS Terrain Tiles, none of them in the terrain's grid:

  • Nine 30 cm NAIP COGs around Pikes Peak, in UTM zone 13N. This is the default, at 1.5x vertical exaggeration, in true color or as false-color infrared. NAIP's fourth band is near-infrared, so these tiles use their own getTileData and renderTile; COGLayer's default pipeline would draw that band as alpha.
  • swisstopo's 1:1 million map, in EPSG:2056.
  • A 1962 USGS topographic quad, in a NAD27 polyconic projection.

It also:

  • measures zRange as terrain tiles load, from each tile mesh's header.boundingBox in TerrainLayer's onTileLoad. TerrainLayer sets its own zRange from the same field;
  • uses TerrainController, so the camera rises and falls with the ground as you move;
  • limits the terrain to the COGs' WGS84 bounds: TerrainLayer's extent loads only tiles that overlap them, and a ClipExtension stops it drawing outside them;
  • scales the Terrarium decoder from a vertical exaggeration slider. It also scales the camera's height and rescales the measured zRange, since TerrainController doesn't lift the camera when the terrain rises;
  • starts the NAIP view where TerrainController's first pick stays under maxZoom, so the camera doesn't jump back once the terrain loads.

zRange

RasterTileLayer culled tiles as if the ground were at sea level. Over the Grand Canyon, about 1.9 km up, that left tile-shaped holes near the camera.

zRange (meters) is now passed to the inner TileLayer, whose traversal already builds bounding volumes over that height range. It is compared by value, so an inline array doesn't re-render the layer.

The traversal caches each tile's bounding volume, keyed on the elevation in common-space units. Those units change with the viewport's latitude, so with a zRange, every north-south pan missed the cache and reprojected every visited tile. Cache entries now keep the tile's reprojected reference points, so a new elevation only refits the box.

Not handled

  • COGLayer picks each tile's resolution for the zoom at the center of the view, however far away the tile is. Tilted views therefore load tiles at the center's resolution out to the horizon, so the example caps the NAIP view at zoom 14 and pitch 60.
  • Removing a COGLayer doesn't destroy its tile textures, because deck.gl's Tileset2D.finalize doesn't call onTileUnload. This also affects COGLayer's default pipeline, so it's left for a follow-up in RasterTileset2D.
  • When a dataset's coarsest level has more than 100 tiles, root tiles are still culled against the view at sea level before zRange applies.
  • zRange is ignored in GlobeView, whose bounding volumes don't model elevation yet.
  • In deck.gl 9.4, picking with a terrain+draw layer that isn't pickable logs "Picked non-existent layer". The example's terrain is pickable, which TerrainController needs anyway.
  • The example's README lists the limits of draping itself. For instance, the terrain tiles cap the draped resolution, and only the first view is draped.

Closes #57

🤖 Written by Claude Code

kylebarron and others added 2 commits October 15, 2026 21:01
RasterTileLayer culled tiles as if the ground were at sea level, so a COG
draped over high terrain was missing tiles near the camera. It now accepts
zRange (meters) and passes it to its inner TileLayer, compared by value.

The traversal's bounding-volume cache compared elevation in common-space
units, which change with the viewport's latitude, so with a zRange every
north-south pan reprojected every visited tile. Cache entries now keep the
tile's reprojected reference points, and a new elevation only refits the box.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
An experimental example that drapes a COG over AWS Terrain Tiles with
deck.gl's _TerrainExtension: swisstopo's 1:1 million map (EPSG:2056) and a
1962 USGS topographic quad in a NAD27 polyconic projection, neither of which
shares the terrain's Web Mercator tile grid. Adds a card for it on the docs
examples page.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the feat label Oct 16, 2026
Comment on lines -558 to -567
const refPointPositions: [number, number, number][] = [];
for (const p of commonSpacePositions) {
refPointPositions.push([p[0], p[1], minZ]);

if (minZ !== maxZ) {
// Also sample at maximum elevation to capture the full 3D volume
refPointPositions.push([p[0], p[1], maxZ]);
}
}

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 looks like the same code that we refactored below into fitBoundingVolume... what changed?

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.

Nothing in the logic. The loop moved into fitBoundingVolume unchanged, so both this full computation and the new cache-refit path in getBoundingVolume can call it.

🤖 Written by Claude Code

const entry = {
...cacheHit,
zRange,
boundingVolume: fitBoundingVolume(

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.

Oh we also fit the bounding volume here

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. On a cache hit where only zRange differs, this refits the box from the cached reference points instead of reprojecting them. In common-space units zRange changes with the viewport's latitude, so before this, every north-south pan reprojected every visited tile.

🤖 Written by Claude Code

Comment on lines +149 to +161
/**
* Lowest and highest elevation, in meters, of the surface the raster is
* drawn on, such as the terrain under a COG draped with deck.gl's
* `TerrainExtension`.
*
* Tiles are selected for the volume between these heights instead of for
* the ground at sea level, so tiles the terrain lifts into view still
* load. This only changes which tiles load; it doesn't move the raster.
* Ignored in `GlobeView`.
*
* @default null
*/
zRange?: [number, number] | null;

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.

again I'm a little surprised that the user would need to / be able to reliably know this. Shouldn't this be passed through from the other tile layer somehow?

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.

deck.gl layers don't share state, so there's no built-in way to pass it through. TileLayer's own zRange is also a user prop: TerrainLayer computes it from its loaded tiles and passes it to its inner TileLayer. A draped COGLayer has no handle on the terrain layer.

So the prop stays, matching TileLayer's, and the JSDoc now says how to get it from a TerrainLayer: each tile mesh's header.boundingBox, in onTileLoad. The example does exactly that (67e7d1e), so nobody needs to know the elevations ahead of time.

Doing it automatically would mean RasterTileLayer finding terrain layers through deck.gl's layer manager and reading TerrainLayer's internal state. That's possible as a follow-up, but brittle.

🤖 Written by Claude Code

kylebarron and others added 2 commits October 16, 2026 12:12
Reworks the cog-terrain example after review:

- Adds nine 30 cm NAIP COGs around Pikes Peak, in true color and as
  false-color infrared, and makes them the default at 1.5x vertical
  exaggeration. NAIP's fourth band is near-infrared, so its tiles use their
  own getTileData and renderTile instead of COGLayer's default pipeline,
  which would draw that band as alpha. They use linear filtering and a
  64 MB tile cache per COG.
- Measures zRange from the terrain tiles as they load (TerrainLayer's
  onTileLoad and each mesh's header.boundingBox) instead of hard-coding it
  per dataset.
- Adds a vertical exaggeration slider that scales the Terrarium decoder.
- Uses TerrainController, so the camera follows the ground, with a new view
  per scene.
- Limits the terrain to the COGs' WGS84 bounds: TerrainLayer's extent skips
  tiles outside them, and a ClipExtension stops it drawing there.
- Caps the NAIP view at zoom 14 and pitch 60, because COGLayer picks every
  tile's resolution for the zoom at the center of the view.

Also documents how to get RasterTileLayer's zRange from a TerrainLayer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replaces the two placeholder screenshots with a false-color infrared view
of the NAIP imagery draped over Pikes Peak.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment on lines +159 to +163
* A deck.gl `TerrainLayer`'s tiles report their elevations: in its
* `onTileLoad`, each tile's mesh has a `header.boundingBox`, which is
* also how `TerrainLayer` sets its own `zRange`. The `cog-terrain`
* example widens this range as terrain tiles load.
*

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.

So do we need this at all or can we remove it? Seems like we should be able to automatically expand the zRange on the fly without adding a public prop to RasterTileLayer...

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.

The deck.gl TileLayer accepts a zRange prop, and so I think it's ok to also accept a zRange prop here.

@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

…xample

Address the third static review of the cog-terrain example:

- Memoize the Terrarium decoder and the clip box. TerrainLayer compares the
  decoder by reference in its tiles' getTileData update triggers, so every
  zRange widening reloaded every terrain tile, and switching between the two
  NAIP options reloaded the terrain too.
- Rescale the measured zRange when the exaggeration changes, instead of
  selecting tiles for sea level until the terrain reloads.
- Scale the camera's height with the exaggeration and give the view a new
  TerrainController. The controller moves only during interactions, so at 3x
  the camera ended up inside the terrain.
- Start the NAIP view where TerrainController's first pick stays under
  maxZoom, so the view no longer jumps back once the terrain loads.
- Don't create a NAIP texture for a tile cancelled while it decodes.
- Correct the README and comments that the review found wrong.

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, just waiting for the merge conflict to be resolved

main removed RasterTileLayer's `_errorSpace` prop (#721), so the conflict in
raster-tile-layer.test.ts keeps this branch's zRange tests and drops the
`_errorSpace` test that main deleted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kylebarron
kylebarron enabled auto-merge (squash) October 16, 2026 17:59
@kylebarron
kylebarron disabled auto-merge October 16, 2026 18:01
@kylebarron
kylebarron merged commit 8f56d8e into main Oct 16, 2026
3 checks passed
@kylebarron
kylebarron deleted the kyle/cog-terrain-example branch October 16, 2026 18:01
kylebarron added a commit that referenced this pull request Oct 16, 2026
…changes (#731)

> [!NOTE]
> This PR was written by Claude (Claude Code), not by @kylebarron.

Follow-ups to #720's terrain example, from a final review of its camera
changes:

- **Scene changes reset the exaggeration** to the default 1.5x. Every
start view is set for it. At 3x, NAIP's start put the camera inside the
terrain in short windows, and jumped back about 1.6 zoom levels in 750
px ones.
- **Lowering the exaggeration leaves the camera where it is.** Lowered
with the terrain, the camera ended up nearer the ground, and the new
`TerrainController`'s first pick could zoom past `maxZoom` and jump
back. Raising it still raises the camera with the terrain.
- **The camera math uses the window's size.** The view states the
controller emits keep a stale size after a resize, until you next move
the map.
- **Wording:**
- The NAIP start view's thresholds are 630 px, and 1760x1100 at the
default exaggeration.
- The pitch limit of 70 raises deck.gl's default of 60 rather than
capping it.
  - The `naip.ts` comment on cancelled tiles no longer depends on #728.

### Not fixed: the first pick after an exaggeration change

The review found that the new `TerrainController`'s first pick can land
on the wrong ground, and suggested a new `TerrainLayer` per
exaggeration. I tried that, and also waiting for the terrain to load
before handing the camera to a `TerrainController`. Neither changed the
result.

- Raising NAIP from 1.5x to 2x put the target at 6,005 m on terrain
beyond the summit. The slope in view is at 8,360 m.
- This happened even with every terrain tile loaded.
- The pick reports the correct terrain tile, but a point farther along
the view ray than the visible surface.
- Picks from a view whose target is on the ground come back correct, so
this looks like a deck.gl picking issue.

The README now describes it: the target eases to the ground as you move,
so zooming straight in after raising the exaggeration can take the
camera into the slope.

Checked in headed Chrome at 1200x750:

- 1.5x to 3x one step at a time, and back down to 1x. The zoom never
passed 12.2, and the terrain stayed in view.
- Scene switches reset the slider, and NAIP's start view lands where it
does on first load (zoom 13.58).
- No console errors.

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

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

Rendering RasterLayer over terrain with terrain+draw operation?

1 participant