Skip to content

fix: clip AffineTilesetLevel tile corners to array extent - #580

Closed
kylebarron wants to merge 1 commit into
mainfrom
kyle/affine-tile-corners-clip
Closed

kylebarron wants to merge 1 commit into
mainfrom
kyle/affine-tile-corners-clip

Conversation

@kylebarron

@kylebarron kylebarron commented May 29, 2026 •

Copy link
Copy Markdown
Member

Working to solve source-cooperative/cog-viewer#8

Summary

AffineTilesetLevel.projectedTileCorners extrapolates past the array when matrixWidth*tileWidth > arrayWidth (or height equivalent). For projections whose valid domain isn't aligned with the affine — Mollweide, Sinusoidal, Equal Earth — those extrapolated points fall outside the projection's domain, proj4 maps them to the pole, and the Web Mercator clamp collapses every such corner onto a single horizontal line at ±85.05°. The bounding-volume sampler then produces a zero-height OBB, the tileMaxY > minY test in insideBounds fails (0 > 0 is false), and every tile gets culled.

Net effect: a full-world Mollweide COG (e.g. Vizzuality HFP 100) loads, fires onGeoTIFFLoad, then getTileIndices returns [] and nothing renders — not even the debug overlay.

This PR clips the tile's pixel-space rectangle to [0, arrayWidth] × [0, arrayHeight] in projectedTileCorners so every corner stays inside the data extent. Interior tiles are unaffected (their nominal corners are already inside the array). The hfp file now correctly selects tiles, traverses to finer levels, and renders. See screenshot below: debug overlay showing the two selected z=1 tiles spanning the world.

Adds a HFP 100 entry to the cog-basic example dropdown for manual verification — the data renders ~black against the dark basemap on its own (a separate render-pipeline ergonomics issue for unscaled uint16 data), but the debug overlay confirms the traversal is correct.

Test plan

  • pnpm -r test — 384 pass (+2 new edge-clipping tests in affine-tileset-level.test.ts)
  • pnpm -r typecheck — clean across all 23 projects
  • pnpm biome check — clean
  • Headless Chrome + CDP: navigate cog-basic to the HFP Mollweide entry, toggle the debug overlay — tile boundaries appear at x=0 y=0 z=1 and x=1 y=0 z=1 (pre-fix: nothing)
  • Spot-check a non-edge-case example (e.g. NLCD, NAIP) still renders normally

Written by Claude on behalf of @kylebarron

🤖 Generated with Claude Code

Tiles at the right/bottom edge of a level's matrix can nominally extend
past the array when matrixWidth*tileWidth > arrayWidth (likewise for
height). The affine still extrapolates past the data, but for
projections whose valid domain isn't a rectangle aligned with the
affine — Mollweide, Sinusoidal, Equal Earth — those extrapolated
points fall outside the projection's domain and proj4 collapses them
onto the pole. The downstream Web Mercator clamp then sends every such
corner to one horizontal line at ±85.05°, producing a degenerate
bounding volume that the `tileMaxY > minY` check in `insideBounds`
rejects (0 > 0 is false). Every tile gets culled and nothing renders.

Clip the tile's pixel-space rectangle to [0, arrayWidth]×[0, arrayHeight]
before applying the affine so every corner stays inside the data
extent. Interior tiles are unaffected.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@github-actions github-actions Bot added the fix label May 29, 2026
@kylebarron
kylebarron marked this pull request as draft June 2, 2026 15:43
kylebarron added a commit that referenced this pull request Oct 21, 2026
before (black border):

<img width="2800" height="1724" alt="before-fit"
src="https://github.com/user-attachments/assets/e42d808f-47a2-4593-9a76-5946aec0a98b"
/>

after:

<img width="2800" height="1724" alt="after-fit"
src="https://github.com/user-attachments/assets/d129b0e3-8bb5-4649-b14c-a357161a3669"
/>

before:

<img width="2800" height="1724" alt="3-before-20m-corner"
src="https://github.com/user-attachments/assets/dac357ee-9498-4210-977c-a1ca556ea766"
/>

after:

<img width="2800" height="1724" alt="4-after-20m-corner"
src="https://github.com/user-attachments/assets/b54ac9bb-2a70-4029-a86f-dd52a507770f"
/>

before and after for mollweide example (still broken 😄 )

<img width="2800" height="1724" alt="5-before-mollweide"
src="https://github.com/user-attachments/eassets/d7c6519a-6800-4f64-8de2-38209c7338fc"
/>
<img width="2800" height="1724" alt="6-after-mollweide"
src="https://github.com/user-attachments/assets/24ccfad8-7fe8-4604-98a4-c84f70bf82bf"
/>


-----



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

`MultiCOGLayer` now fetches edge tiles with `boundless: false`, as
`COGLayer` does, and meshes only the part of each tile inside the image.
Before, it drew every tile at full size, so the padding past the image's
right and bottom edges showed as a band past the scene. That padding
holds whatever the file's writer stored there, not necessarily nodata.
In Digital Earth Australia's Sentinel-2 files (nodata -999) it is 0,
which a nodata filter can't remove.

Coarser bands are stitched from clipped tiles too, which works because
every step uses the same clipped geometry:

- `AffineTilesetLevel.projectedTileCorners` clips the tiles in the last
column and row to the array (the change from #580).
- `resolveSecondaryTiles` takes the stitched texture's size from the far
clipped corner, so it matches the UV transform it computes from the
clipped extents.
- `assembleTiles` already accepts clipped edge tiles (#753).

Clipping the corners also affects `COGLayer` and `ZarrLayer`, which
already drew only the clipped part: their culling `bbox`, bounding
volumes, Web Mercator pole clamp, and debug outlines now match what they
draw. It also fixes #580's case, a full-world Mollweide COG whose padded
corners fall outside the projection's domain, so every tile was culled.
That file now selects and draws tiles, though its polar tiles still
smear because pixels outside Mollweide's ellipse don't reproject, which
is a separate problem.

`dev-docs/boundless-tiles.md` is rewritten to match.

Closes #580, closes #651

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

1 participant