Skip to content

fix(deck.gl-raster): tolerate float error in RasterTileset2D.getParentIndex - #750

Merged
kylebarron merged 3 commits into
developmentseed:mainfrom
orshih6:contribot/issue-738
Oct 21, 2026
Merged

kylebarron merged 3 commits into
developmentseed:mainfrom
orshih6:contribot/issue-738

Conversation

@orshih6

@orshih6 orshih6 commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

getParentIndex divides a child tile's column and row by the footprint ratio between two levels, then floors the result. With TileMatrixSetAdaptor, each footprint comes from scaleDenominator * 0.00028. When a TileMatrixSet rounds its scale denominators, as the OGC example WebMercatorQuad does, that ratio can land just above 2. Between z7 and z8 it's 2.0000000000000044. Then Math.floor(2 / 2.0000000000000044) is 0, so an even column or row maps to the parent before its real one. For example, 9/2/1 mapped to 8/0/0 instead of 8/1/0.

The fix adds a tolerance of 1e-6 to the quotient before flooring. This only changes the result when the quotient is within 1e-6 below a whole number, which in practice means the ratio is a whole number with float error in it. Non-integer ratios, such as COG overviews that don't halve exactly, give the same parents as before.

I added tests/raster-tileset/get-parent-index.test.ts. It builds a tileset from the OGC example's z6 to z9 scale denominators. It checks that 9/2/1 maps to 8/1/0, and that every column and row at those levels maps to floor(i / 2). Both tests failed before the change. They pass now, along with the existing tests/raster-tileset and tileset-refinement tests. Biome check is clean.

🤖 Generated with Claude Code

Fixes #738


This change was prepared with an AI coding agent (Claude) and reviewed by me before submitting. I'm happy to make any changes you'd like.

@github-actions github-actions Bot added the fix label Oct 21, 2026
@kylebarron

Copy link
Copy Markdown
Member

I'm pretty sure this is covered by #689...

@kylebarron

Copy link
Copy Markdown
Member

Actually no, it isn't covered by #689; that fixed a couple precision issues but not in getParentIndex

kylebarron and others added 2 commits October 20, 2026 22:26
getParentIndex used its own 1e-6 epsilon, the same value as the traversal's
TILE_EDGE_TOLERANCE in tile-span.ts. Export that constant and use it, so the
two tolerances can't drift apart.

The test now loads the OGC example WebMercatorQuad.json, as the other
raster-tileset tests do, instead of copying four of its scale denominators.
It checks the first and last 8 tiles of all 25 levels, which covers every
affected level pair (z4, z7, z8, z9, z14, z17 and z21) and the far edge of
each level, where the float error is largest. Checking every tile of every
level takes ~765 ms; this takes ~3 ms.

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

@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!

@kylebarron
kylebarron enabled auto-merge (squash) October 21, 2026 02:30
@kylebarron
kylebarron disabled auto-merge October 21, 2026 02:30
@kylebarron
kylebarron merged commit e529da4 into developmentseed:main Oct 21, 2026
3 checks passed
kylebarron added a commit that referenced this pull request Nov 5, 2026
…tParentIndex (#841)

### 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.

| <video
src="https://github.com/user-attachments/assets/1b4028d8-e5ca-40e5-85ca-90940fe5da47"
controls muted playsinline></video> | <video
src="https://github.com/user-attachments/assets/8e96e6e1-2eb5-4d87-bd14-81d97593edc4"
controls muted playsinline></video> |
|---|---|
| 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:

```ts
return {
  x: Math.floor((index.x + 0.5) / decimationX),
  y: Math.floor((index.y + 0.5) / decimationY),
  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`.

### 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 }
```

With this change, all 257 tests in `packages/deck.gl-raster` pass,
including the #750 tests.

**To see it in
[`cog-basic`](https://developmentseed.org/deck.gl-raster/examples/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](https://claude.com/claude-code)

---------

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

RasterTileset2D.getParentIndex returns a neighboring tile when TileMatrixSet scale denominators are rounded

2 participants