Skip to content

fix(deck.gl-raster): pick parent tile by centre in RasterTileset2D.getParentIndex - #841

Merged
kylebarron merged 3 commits into
developmentseed:mainfrom
alukach:fix/parent-index-rounded-overviews
Nov 5, 2026
Merged

kylebarron merged 3 commits into
developmentseed:mainfrom
alukach:fix/parent-index-rounded-overviews

Conversation

@alukach

@alukach alukach commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Note

After merge, Claude (Claude Code) updated the Fix and Testing sections for @kylebarron to match the merged code, which includes the clamp from 80c1721. The rest is the original description.

Problem

While a tile loads, best-available refinement draws its parent (the coarser tile covering the same area) as a placeholder. For some COGs, getParentIndex returns the parent's neighbour instead. That tile is drawn somewhere else, so the loading tile's area goes blank until it arrives.

before.mp4
after.mp4
Before After

This happens when a COG's overview sizes were rounded down. Each overview halves the level above, but an odd size can't be halved exactly. For example, the Umbra Port of Rotterdam COG in cog-basic goes 22859 → 11429 px. Every level still covers the same extent, so the decimation between levels is slightly above 2 (here 22859 / 11429 ≈ 2.00009).

getParentIndex maps a child to a parent with floor(x / decimation + TILE_EDGE_TOLERANCE). With decimation slightly above 2, an even x lands just under a whole number, and the floor picks the tile before the right one:

decimation = 2857 / 1428 = 2.0007
child x = 2  →  floor(2 / 2.0007 + 1e-6) = floor(0.99965) = 0   // expected 1

The 1e-6 tolerance from #750 (for #738) absorbs floating-point noise like 2.000000000000004, but not an error of ~10⁻⁴. A COG is affected when twice an overview's size is less than the size of the level above. Umbra's 2 × 11429 = 22858 < 22859 is one example.

Fix

Map the child tile's centre instead of its left/top edge:

return {
  x: Math.min(
    Math.floor((index.x + 0.5) / decimationX),
    parentOverview.matrixWidth - 1,
  ),
  y: Math.min(
    Math.floor((index.y + 0.5) / decimationY),
    parentOverview.matrixHeight - 1,
  ),
  z: index.z - 1,
};

Tile x spans [x, x + 1) in child-tile units, so x + 0.5 is its centre. Dividing by decimation converts that to parent-tile units, and floor gives the parent containing the centre, which is the one covering most of the child. When decimation is a whole number, including 1:1 (Sentinel-2's last overview), the result is unchanged. The centre is half a tile from any edge, so getParentIndex no longer needs TILE_EDGE_TOLERANCE.

The clamp covers a level whose last tile holds only a sliver of data. With 512 px tiles, for example, the last tile of a 2049 px level holds one column of data, but its centre lies past the edge of the 1024 px level, so without the clamp getParentIndex would return a column that doesn't exist.

Testing

New cases in tests/raster-tileset/get-parent-index.test.ts, using the overview widths of the Umbra COG from cog-basic (357 → 22859 px, 512 px tiles), fail on main:

AssertionError: expected { x: +0, y: +0, z: 2 } to deeply equal { x: 1, y: 1, z: 2 }

The tests check every tile against the parent holding most of its data, for the Umbra widths and for a 2049 px COG whose last tile holds one column of data. With this change, all 259 tests in packages/deck.gl-raster pass, including the #750 tests.

To see it in cog-basic: pick Umbra Port of Rotterdam (rotated COG) and tick Debug overlay. Then zoom quickly from the four z=1 tiles into the right-hand side of the image (I recommend setting network throttle to "Fast 4G" in devtools). On main, part of the screen drops to bare basemap while tiles load. With this change, the coarse tile stays visible until the fine tiles arrive.

🤖 Generated with Claude Code

🤖 Written by Claude Code

…tParentIndex

COGs whose overviews were sized by halving and rounding down (e.g.
1543 -> 771 px) have a decimation like 2.0013 between levels, well beyond
TILE_EDGE_TOLERANCE. Flooring the child's top-left corner then picks the
left/top neighbour, so best-available refinement draws the placeholder
over the wrong tile and leaves a hole while the child loads.

Map the child's centre instead. It is half a tile from any edge, so it
covers both this and the rounded scale denominators from developmentseed#738 without a
tolerance, and is unchanged for whole-number decimation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the fix label Nov 5, 2026
@alukach
alukach requested a review from kylebarron November 5, 2026 00:18
alukach and others added 2 commits November 4, 2026 17:20
Base the rounded-overview fixture on a COG that is already in
examples/cog-basic instead of an external file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
When a level's last tile holds only a sliver of data, such as the one
column of a 2049px level over a 1024px one, the tile's center lies past
the parent level's edge, so getParentIndex returned a parent that doesn't
exist. Clamp it to the parent level's last column and row, which hold
that data.

Test getParentIndex against the parent holding most of each tile's data,
instead of restating the center formula, and cover the sliver case.

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

Copy link
Copy Markdown
Member

Note

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

I pushed 80c1721 on top of this PR:

  • Clamp to the parent level. When a level's last tile holds only a sliver of data, its center can lie past the parent level's edge. Example: with 512 px tiles, the last tile of a 2049 px level (one column of data) mapped to column 2 of the 1024 px level, which has only 2 columns. getParentIndex now clamps to the parent's last column and row, which hold that data. Main didn't have this problem; it came with the center formula and affects about 0.1% of level pairs (51 tiles in 3 of naip-mosaic's 924 COGs).
  • Test oracle. The "every tile" test now checks each tile against the parent holding most of its data, instead of repeating the center formula. It runs on the Umbra widths and on the 2049 px case.
  • Comment: shortened, and links this PR.

This affects most COGs, not only Umbra. GDAL's COG driver rounds overview sizes down: gdal_translate -of COG gives 1543 -> 771 -> 385, while gdaladdo gives 772 -> 386. Across the 924 NAIP COGs in naip-mosaic, main gives 53% of tiles the wrong parent. The most visible case: when a level has an odd number of tile columns, main gives its last column the wrong parent, and nothing else shows the right one. So a strip along a COG's right or bottom edge goes blank while its tiles load.

Before (left, main) and after (right, this PR). Each run zooms in one level while tile requests are held for 3 s, with the control panel hidden:

naip-mosaic-side-by-side.mp4

naip-mosaic: on main, the last tile column of a NAIP COG shows the bare basemap; with this PR, its parent stays as the placeholder.

cog-basic-umbra-side-by-side.mp4

cog-basic, Umbra COG with the debug overlay, zooming into the image's corner (5714 -> 11429 px level): on main, the corner tile x=11 y=11 z=4 isn't used as the placeholder for the last column and row.

The views before the zoom and after the tiles load are pixel-identical between main and this PR. Only the placeholders shown while loading change.

🤖 Written by Claude Code

@kylebarron kylebarron 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.

Thanks! This is a bug I've seen for a while but hadn't taken the time to track down

@kylebarron
kylebarron merged commit b021874 into developmentseed:main Nov 5, 2026
5 checks passed
@alukach
alukach deleted the fix/parent-index-rounded-overviews branch November 5, 2026 16:02
kylebarron added a commit that referenced this pull request Nov 5, 2026
> [!NOTE]
> This PR was written by Claude (Claude Code), not by @kylebarron.

Bumps `version` to `0.10.0-beta.3` in the root `package.json` and in
every `packages/*/package.json`. The publish workflow publishes it under
the `beta` dist-tag, so `latest` stays on 0.9.0.

`CHANGELOG.md` gets a `v0.10.0-beta.3` section with GitHub's generated
notes for the 17 PRs merged since v0.10.0-beta.2.

## Breaking change

- #843: `MosaicLayer` no longer takes `maxCacheByteSize`. To bound an
item's memory by bytes, set `maxCacheByteSize` on the layer that
`renderSource` returns.

## Behavior changes

- #839: a `MosaicLayer` item out of view keeps its opened source in the
cache but drops its layers, which frees their textures.
- #785: `parseGeoZarrMetadata` takes Y from the first entry of
`spatial:dimensions` and X from the second, whatever their names, as
v0.1 of the spatial convention says. Lists that aren't two entries long
are rejected.
- #841: `RasterTileset2D.getParentIndex` picks the parent tile that
contains the child's centre.

## New APIs

- #835: `parseVRT` in `@developmentseed/deck.gl-geotiff`, and `fromGDAL`
in `@developmentseed/affine`.
- #789: `renderSource` gets the props that `MosaicLayer` passes to its
sublayers.

## Not included

#838 (`ZarrSource` opens from a store with a `path`) and #837
(`DEFAULT_RANGE_READER` moves to `@developmentseed/deck.gl-raster`) are
still open. Both are breaking.

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

Co-authored-by: Claude Opus 5.5 <[email protected]>
alukach added a commit to source-cooperative/cog-viewer that referenced this pull request Nov 5, 2026
* chore: upgrade deck.gl-raster to 0.10.0-beta.3 and drop patches

0.10.0-beta.3 ships the getParentIndex fix (developmentseed/deck.gl-raster#841)
and Cylindrical Equal Area support (#791), so the deck.gl-raster, geotiff and
proj patches are no longer needed. Drop the test that guarded our patch;
upstream owns that logic now.

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

* fix: migrate to the 0.10 COGLayer source API

0.10 dropped COGLayer/MultiCOGLayer's geotiff, sources, epsgResolver,
onGeoTIFFLoad and onError props in favour of an opened `source`. We still
passed the old props, so the layer never opened anything: no tiles, no
fit-to-bounds, no error.

Open the COGSource / MultiCOGSource ourselves in the load effect (keeping
robustEpsgResolver and loadGeoTIFF's CORS workaround), check and fit the
bounds there, and hand the layer the opened source. A source with invalid
bounds is never set, so the extentValid gate is no longer needed.

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

* fix: address review of the 0.10 source migration

- Fit to bounds in an effect gated on the map's onLoad; maplibre-gl loads
  asynchronously, so mapRef could still be null when the source opened.
- Restore the layers' onError so layer failures reach the error toast.
- Run validateCog on every file of a multi-COG, like the single-file path.
- Derive geotiff from source and share one auto-stats effect, so a stats
  failure in multi-COG mode logs a warning instead of an error toast.

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

---------

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.

2 participants