Skip to content

feat: Add antimeridian crossing support to RasterReprojector - #269

Closed
espg wants to merge 2 commits into
developmentseed:mainfrom
englacial:feat/antimeridian
Closed

espg wants to merge 2 commits into
developmentseed:mainfrom
englacial:feat/antimeridian

Conversation

@espg

@espg espg commented Feb 26, 2026 •

Copy link
Copy Markdown

Ported from #266. Needed for default globe discussed on source-cooperative/source.coop#230.

Summary

  • Detect antimeridian (±180°) crossing from initial corner vertices in RasterReprojector — when a tile's WGS84 longitudes span >180°, normalize them to a continuous range (e.g., [170, 190] instead of [170, -170]) so mesh triangles don't wrap the wrong way around the globe
  • Apply longitude offset to subsequent vertices during mesh refinement
  • Add 5 new unit tests for antimeridian handling

Closes #171

Test plan

  • All unit tests pass (pnpm test in raster-reproject)
  • Run cog-basic example — verify no regression in standard rendering

espg added 2 commits February 26, 2026 13:15
Detect when a tile's output positions span the ±180° meridian and
normalize longitudes to a continuous range (e.g., [170, 190] instead
of [170, -170]). This prevents mesh triangles from spanning 340° of
longitude and ensures correct GPU-side interpolation for tiles near
the date line.
@dcherian

dcherian commented Mar 3, 2026

Copy link
Copy Markdown

I'm curious as to how this works.

In xpublish-tiles, I found that fixing the discontinuity in source CRS space was pointless since proj4 would just wrap to the extents of the output CRS upon transforming. So I had to do the unwrapping after transforming. My solution: use phase unwrapping on the coordinates since we know the coordinate space width.

@kylebarron

Copy link
Copy Markdown
Member

@espg do you think you'd be able to add some visual screenshots/screencasts to your PRs to more visually explain the internals of the changes?

kylebarron added a commit that referenced this pull request May 27, 2026
…hes to ±85.051° (#574)

* docs(specs): add antimeridian crossing-tile (cut-in-two) design

Design for rendering imagery crossing ±180° in Web Mercator (issues #171,
#366). Splits a crossing tile at the antimeridian into west/east pieces so
each reprojects as a normal non-crossing tile — avoiding the proj4-rewrap
unwrap that prior attempts (#353/#374/#269) stumbled on. Generalizes the
RasterReprojector to accept a delaunator-shaped initial-triangulation seed
(subsuming #351 uvBounds / pole clamp), splits in _renderSubLayers into two
single-mesh RasterLayers, and uses a two-box bounding volume composing with
the merged world-copy traversal (#518).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* docs(specs): refine antimeridian spec — add test fixture, handle slanted cuts

- Use the vendored geotiff-test-data antimeridian.tif fixture (42x42 EPSG:4326,
  crosses -180 at column 24) as the primary deterministic crossing test.
- Handle slanted (rotated-geotransform) cuts, not just vertical: any straight
  cut yields convex pieces delaunator triangulates exactly; error only on
  curved/concave cuts. createInitialConditions is therefore part of the MVP
  crossing path.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* feat(raster-reproject): add createInitialConditions (delaunator-backed seed)

Add the InitialTriangulation type and a tree-shakeable createInitialConditions
helper that builds a Delaunay seed from a UV point set. delaunator is confined
to its own module (initial-conditions.ts); delatin.ts does not import it and the
package is sideEffects:false, so it tree-shakes out for consumers that don't use
it. Foundation for antimeridian cut-in-two and the sub-domain capability in #351.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* refactor(raster-reproject): document delaunator seed pattern, don't ship a wrapper

Per review: a one-line delaunator wrapper isn't worth a runtime dependency.
Expose only the InitialTriangulation type and show the delaunator one-liner in
its docstring. delaunator moves to devDependencies — used by tests to validate
winding compatibility, not shipped to consumers.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* docs(specs): drop createInitialConditions wrapper from antimeridian design

raster-reproject exposes only the InitialTriangulation type + documents the
delaunator one-liner; delaunator is a dev/test dep (winding validation), not
shipped. Runtime seed-building for crossing tiles is the deck.gl-raster
builder's job (follow-up plan). Mark stage 1 done.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* feat(raster-reproject): seed RasterReprojector from an initial triangulation

Generalize the constructor to accept an optional initialTriangulation seed
(delaunator's data shape), defaulting to a hardcoded unit-square seed so
behavior is unchanged and the package needs no runtime triangulation dep.
Refinement only ever splits existing triangles, so a sub-domain seed confines
the mesh to that region. Tests build seeds via delaunator (the documented
pattern) to validate winding compatibility + sub-domain confinement.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* feat(raster-reproject): add rectangleSeed helper for sub-rectangle seeds

Build an axis-aligned 2-triangle rectangle seed for a UV sub-rectangle (no
delaunator, runtime-safe). UNIT_SQUARE_SEED is now rectangleSeed(0,0,1,1).
Used to clamp a mesh to a UV band — e.g. the valid Web Mercator latitude band
(#182 / #351) — and reused by the antimeridian vertical-cut case.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* feat(deck.gl-raster): add webMercatorClampSeed (clamp mesh to ±85.051°)

Pure helper that returns a rectangleSeed clamping a north-up geographic tile's
reprojection mesh to the Web Mercator latitude band, or undefined when no clamp
is needed/possible (rotated/projected tiles, fully-polar tiles). Avoids the
degenerate near-pole triangles from #182 / #351. Unit-tested with synthetic
corner latitudes.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* feat(deck.gl-raster): clamp Web Mercator meshes to valid latitude band

Wire the reprojector seed through the render path: RasterLayer gains an
initialTriangulation prop (passed to RasterReprojector, regenerated on change);
getTileMetadata computes a per-tile _webMercatorReprojectorSeed via
webMercatorClampSeed; _renderSubLayers passes it in the Web Mercator branch
only (globe shows the poles, full mesh). Fixes the degenerate near-pole
triangles for EPSG:4326 imagery reaching ±90° (#182 / #351).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* refactor(deck.gl-raster): name clamp seed as initialTriangulation

Rename webMercatorClampSeed → webMercatorInitialTriangulation and the tile
metadata field _webMercatorReprojectorSeed → _webMercatorInitialTriangulation,
for consistency with the InitialTriangulation type and the
RasterLayer.initialTriangulation prop (drops the ad-hoc 'seed'/'ReprojectorSeed'
terms).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* refactor(deck.gl-raster): rename clamp fn to createInitialWebMercatorTriangulation

Verb-prefixed name for the builder (was webMercatorInitialTriangulation); the
tile metadata field stays _webMercatorInitialTriangulation.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* refactor(raster-reproject): rename rectangleSeed -> triangulateRectangle

Active verb name for the helper that triangulates a UV rectangle into an
InitialTriangulation; internal UNIT_SQUARE_SEED -> UNIT_SQUARE_TRIANGULATION;
test file renamed to match. Updates the deck.gl-raster clamp caller too.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* refactor: apply triangulateRectangle rename across source + caller

Completes the rename (a657eb8 only moved the test file): rectangleSeed ->
triangulateRectangle in delatin.ts + index export + the renamed test, and the
deck.gl-raster web-mercator-clamp caller. UNIT_SQUARE_SEED -> UNIT_SQUARE_TRIANGULATION.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
kylebarron added a commit to willjnz/deck.gl-raster that referenced this pull request Oct 21, 2026
…tseed#716)

Antimeridian handling has been a requested feature for a long time. It
took a little while but I'm happy with where we're ending up,
architecturally.

For one, we no longer split the mesh in two, like one of my previous
attempts did.

And our switch to using an error metric in _output space_ rather than in
_input space_
(developmentseed#711) makes it a
lot simpler to compute the reprojection mesh.

Here's an example of rendering across the antimeridian of a Sentinel 2
TCI image in Fiji:

<img width="701" height="697" alt="image"
src="https://github.com/user-attachments/assets/6d363374-659f-4fb0-b6ff-ff5f606cbbef"
/>


<img width="605" height="655" alt="image"
src="https://github.com/user-attachments/assets/febc8475-8d7e-44ea-8d1a-b430f78681f0"
/>
<img width="911" height="958" alt="image"
src="https://github.com/user-attachments/assets/805af185-9879-4f23-bcdf-fc8b37347e35"
/>



----


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

An image that crosses the antimeridian now renders in Web Mercator as
one mesh, straight across 180°, when mesh error is measured in output
pixels (`_errorSpace: "output"`). Before, its mesh never converged and
was drawn the long way around the world.

**Why it broke.** proj4 returns x inside one copy of the world. So a
tile that crosses 180° has its west corners near +180° and its east
corners near -180°, and its triangles span the whole world. The GPU then
draws points near the seam in the middle of the world, and refinement
piles up along the seam until it hits the iteration cap. The same wrap
made a crossing tile's bounding box span the world, so the tile was
selected everywhere. It also sent the tile's debug outline the long way
around, and made `getGeographicBounds` span the world, so the examples'
fly-to zoomed out to the far side of it.

An image that stops just short of 180° can cross it too, when its edge
tiles are fetched as full blocks, as `MultiCOGLayer` does: the blocks'
padding runs past 180° though the image doesn't. developmentseed#366's Digital Earth
Pacific COG, in EPSG:3832, ends at 179.97°E, and its 512 px edge blocks
reach 180.07°E.

**The approach.** The forward projection is made continuous across the
wrap before the mesh is built, and the finished mesh is cut at the
world's edge. The reprojector stays planar: it knows nothing about world
copies, Web Mercator or longitudes. `RasterLayer` is told only that its
output x wraps with some period, and `RasterTileLayer`, which knows it
draws in Web Mercator's common space, passes the period. This replaces
developmentseed#714, which unwrapped inside the reprojector and gave the same meshes.

**What changes.**

- **`@developmentseed/proj`** gets `nearestCopy`, which moves a value by
whole periods next to a reference, and `unwrapAlong`, which unwraps a
value at the end of a path by walking along it.
- **`@developmentseed/deck.gl-raster`**
- `RasterLayer` gets an `@internal` `_wrapX` prop: the period of the
output's x axis. With it, `RasterLayer` lifts the image's forward
projection. It unwraps the image's corners by walking to each from the
image's center, then moves each projected x to the copy nearest the
bilinear blend of the corners at that pixel. The unchanged reprojector
meshes the result, and `RasterLayer` cuts the mesh at the world's edge,
moving each part back into the world (Sutherland-Hodgman, with UVs
interpolated along cut edges). `_wrapX` requires `_errorSpace:
"output"`, which never calls the inverse projection; that is what made
this hard in input mode (developmentseed#673). The debug overlay outlines the mesh as
drawn.
- `RasterTileLayer` sets the period, 512, in its Web Mercator branch,
and passes it as `_wrapX` in output mode. Input mode and the globe are
unchanged.
- Tile selection: a tile's reference points are unwrapped from its
center, so a crossing tile's box hugs it. The part past the world's edge
is also tested one world over, so either part alone in view selects the
tile, with or without `repeat`, and it is no longer selected from across
the world. A box half a world wide or more covers the whole world, which
handles tiles around a pole. This also fixes a bug where a polar grid's
root tile was culled near the antimeridian and no tile was selected.
Tile selection changes in input mode too.
- The debug tile outline goes the short way across the antimeridian, and
its label sits in the world copy nearest 0°.
- **`@developmentseed/deck.gl-geotiff`**: `getGeographicBounds` bounds
an image across the antimeridian the short way. West is in [-180°,
180°), and east may run past 180°, as MapLibre's `fitBounds` accepts.
- **`cog-basic`**, in development, serves the five antimeridian fixtures
from `fixtures/geotiff-test-data` and lists them as sources. Picking one
switches to output pixels and draws the antimeridian.
- **Spec:**
[`dev-docs/specs/2026-10-14-antimeridian-unwrap-design.md`](https://github.com/developmentseed/deck.gl-raster/blob/1fde6b45c33c6193796b9e6500e422b665321828/dev-docs/specs/2026-10-14-antimeridian-unwrap-design.md)
replaces the 2026-05-27 cut-in-two spec, now marked superseded.

**Tests.** The main requirement is that a crossing image is never drawn
the long way around. The tests check that the unwrapped mesh is under
half a world wide and crosses exactly one world edge, that no drawn
triangle is wider than half a world, and that the drawn mesh lies inside
the world, with a part at each edge. They run on synthetic images, and
on every tile of every level of the five antimeridian fixtures, through
the tileset and `RasterLayer` that `COGLayer` builds. The fixtures are
EPSG:4326, rotated, UTM 60N (where the seam is curved), EPSG:3832, and
180°W to 180°E, and each is selected with the view on the seam and on
either side of it, with and without `repeat`. A crossing image meshes
exactly like the same image moved off the seam, and an image that
doesn't cross is unchanged. An EPSG:3832 edge block whose padding
crosses 180° meshes the short way, a polar grid (NSIDC's EPSG:3413
extent) is selected at every longitude, and an EPSG:3031 tile with the
pole at a corner is drawn as it would be without wrapping.

**Not changed here:**

- Input mode, the default, still can't draw a crossing image. A
follow-up PR makes output pixels the only mode in the layers.
- `MultiCOGLayer` meshes the full block of an edge tile, including the
zero-filled padding past the image. In output mode, padding that crosses
180° is drawn just east of -180°, unless the render pipeline hides
zeros.
- The Web Mercator clamp seed takes the nominal tile's latitudes, not
the data's, and the debug outline draws the nominal tile. These existing
bugs show on the tiny fixtures.
- Views in other projections: `RasterTileLayer` draws every view but the
globe in Web Mercator's common space.
- developmentseed#713 (tiles centered on the equator never converge), developmentseed#715 (geographic
bounds of rotated images are too small), and `MosaicLayer` and
`MultiCOGLayer` source bounds across 180° (developmentseed#639).
- Tiles that contain a pole have no continuous unwrap.

Closes developmentseed#171
Closes developmentseed#366
Closes developmentseed#269
Closes developmentseed#374
Closes developmentseed#576
Closes developmentseed#673
Closes developmentseed#714

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

Support images spanning the antimeridian

3 participants