Skip to content

feat(raster-reproject): add an experimental output-pixel error mode - #711

Merged
kylebarron merged 6 commits into
mainfrom
kyle/output-pixel-error
Oct 14, 2026
Merged

kylebarron merged 6 commits into
mainfrom
kyle/output-pixel-error

Conversation

@kylebarron

@kylebarron kylebarron commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

We originally drafted this PR in #708, then I decided I wanted to move forward with it, and this PR is the careful implementation I did, step by step with claude.

Previously, we used input space to compute the error metric for when to stop mesh refinement. We used input space because gdalwarp's linear approximator also uses input space, and we wanted to be as similar to gdalwarp as possible.

But that has some problems. Foremost, it requires you to have a projection function where the inverse projection is defined and valid.

This means that only projections that are 1:1 bijective are supported. This leads to problems with the antimeridian (where +- 180 deg longitude both map to the antimeridian), with mapping lat/lon to polar stereographic (where the lines of +-90 deg latitude map to a single point) and with efficient globe reprojections.

Instead, we're implementing the first step to switching to output pixel error computations. In all of my inspections I can't see any perceptible difference visually, and claude created some statistics arguing that it's more effective for the same number of triangles.

This requires a few more forward projections (we compute the local jacobian / aka local partial derivatives at the center of the input image) in order to find the screen pixel size of one pixel. But it doesn't require any inverse projections, which makes it a much more reliable reprojection approach.

This PR makes the output-pixel error mode experimental and opt-in. A follow-up PR will make it the default. Perhaps we'll remove input-pixel error mode altogether for the next release.


Note

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

RasterReprojector refines its mesh until every test point's error is below maxError. Until now that error was measured in input pixels: the GPU's drawn position is mapped back through the inverse projection and compared with the test point, as gdalwarp's -et does. This adds an experimental _errorSpace: "output" mode that measures in output pixels instead: the distance on the map between where a test point belongs and where the GPU draws it, divided by one input pixel's size on the map. The default stays "input", which builds the same meshes as before.

Output mode only needs the forward projection. Input mode needs to invert the projection, which fails where the projection is N:1 rather than 1:1: at the pole of a polar map, where a whole row of a lat/lon tile meets at one point, and along the antimeridian. A 512 px EPSG:4326 tile reaching 90°N, projected to EPSG:3413, never converges in input mode; output mode meshes it with 3,439 triangles in 20 ms.

Changes

  • @developmentseed/raster-reproject: new RasterReprojector(fns, w, h, { _errorSpace: "output" }).
    • The unit, one output pixel, is measured once per mesh at the center of the seed: √|det J| from half-pixel steps, the side of a square with the pixel's area on the map (code). Unlike the geometric mean of the pixel's two sides, it doesn't overstate sheared pixels.
    • Output mode never calls the inverse functions (code), and each split reuses its test point's projected position for the new vertex.
  • @developmentseed/deck.gl-raster:
    • RasterLayer and RasterTileLayer get an @internal _errorSpace prop, so COGLayer, ZarrLayer and MultiCOGLayer do too. Changing it rebuilds meshes from the tiles already loaded.
    • If a mesh can't be built, RasterLayer now draws nothing and reports the error through raiseError (code). Before, the error escaped updateState and the layer kept drawing its old mesh.
  • Examples: land-cover and custom-projection get a "Mesh error measured" select (input or output pixels) under "Debug overlay". In output mode, custom-projection skips its meshXScale column stretch, which only exists to make input-pixel error track error on the map.
  • tests/compare-error-spaces.test.ts: an on-demand comparison of both modes (COMPARE_ERROR_SPACES=1), skipped in normal test runs.
  • Spec: dev-docs/specs/2026-10-13-output-pixel-error-design.md.

Comparison

Triangles at the default tolerance of 0.125 px. In parentheses: each mesh's worst error in the other mode's unit.

Case Input mode Output mode
NLCD, Albers → Web Mercator 5,288 (0.158 output px) 4,726 (0.145 input px)
NAIP in UTM 18N, LINZ in NZTM same mesh same mesh
MODIS, sinusoidal → Web Mercator 2,382 (0.188) 2,848 (0.139)
EPSG:4326 60–85°N tile → Web Mercator 2,394 (0.536) 5,598 (0.124)
EPSG:4326 67.5–89.9°N tile → EPSG:3413 11,880 (0.177) 3,426 (15.4)
EPSG:4326 30–85°N band → EPSG:3413 33,810 (0.205) 33,004 (0.953)
  • Where input pixels grow on the map, input mode misses its tolerance on screen. That happens at high latitudes in Web Mercator, and with MODIS, whose sinusoidal pixels are sheared. On the 60–85°N tile it's 4× over; output mode adds the triangles that takes.
  • Where they shrink, toward a polar map's pole, input mode spends 3.5× the triangles on detail smaller than an output pixel.
  • Projection calls: per triangle, both modes make about as many; output mode's are all forward.

Follow-ups

🤖 Written by Claude Code

kylebarron and others added 6 commits October 14, 2026 14:38
…projection

A "Mesh error measured: in input pixels / in output pixels" select under
each example's debug overlay sets `_errorSpace`, so the two meshes can be
compared over the same view. Switching rebuilds meshes from the tiles
already loaded; nothing is fetched again.

In output mode, custom-projection skips its column stretch (`meshXScale`),
which only exists to make input-pixel error track error on the map.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`COMPARE_ERROR_SPACES=1 pnpm exec vitest run tests/compare-error-spaces.test.ts --silent=false`
prints a markdown table for the reprojector's four fixtures (scaled to
overviews of at most 2048 px) and three high-latitude cases: triangle
counts, each mesh's worst error in both input and output pixels, projection
calls and time, per tolerance and mode. The regular test run skips it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The design behind `_errorSpace`, updated with what the step-by-step review
decided: "input"/"output" pixels as the terms, a pixel size of √|det J| from
half-pixel steps, drawing nothing (and raising the error) when a mesh can't be
built, and making output mode the only mode in a later PR. The explainer
sections (3 and 4) come in a separate PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the feat label Oct 14, 2026
@kylebarron
kylebarron merged commit a8616fc into main Oct 14, 2026
7 checks passed
@kylebarron
kylebarron deleted the kyle/output-pixel-error branch October 14, 2026 19:50
kylebarron added a commit that referenced this pull request Oct 14, 2026
…ion explainer (#712)

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

The reprojection explainer can now show the reprojector's two error
spaces, input pixels and output pixels (`_errorSpace`, from #711), and a
second scenario where they differ strongly.

- **Scenarios.** The explainer is driven by a scenario: an image, its
projections, a reference line and its text. NLCD (Albers → Web Mercator)
stays the default and looks the same.
- A new Arctic scenario rolls a 30–85°N band of NASA's Blue Marble from
longitude/latitude into a ring on a polar stereographic map (EPSG:3413),
with the Arctic Circle as its reference line.
- A "Scenario" select in the tuning panel switches between them, as does
`?scenario=arctic` in the URL.
- **Error space.** The "Error measured" select, the M key, or
`?metric=output` switch between the two modes. Each mode draws and
labels the gap it measures:
- input mode shows, in the input image, a line from each test point to
where the GPU's drawn position maps back;
  - output mode labels the gap on the map.

Step 3 names where the 2-triangle mesh is worst, because that moves with
the mode: in output pixels, NLCD's worst point is the middle of the top
edge, not the center. The library-default triangle count in the closing
caption is computed per scenario and mode, capped at 25,000 iterations.
- **Tuning panel.** It's now a sidebar beside the slide, so it never
covers the animation. It starts collapsed to a slim rail, and P (for
"panel", formerly H) opens it. On screens under 700 px wide it floats
instead. The keys are drawn as keycaps.
- **Layout fixes.** Error labels stay clear of the panel titles, the
counter and the description; the counter no longer wraps under a narrow
map; and the description sits lower, clear of the counter when the map
fills its panel.
- **README:** the scenarios, the error metric, the keys, and commands
that regenerate the Arctic image from NASA's source.

The Arctic image is NASA Earth Observatory's Blue Marble Next Generation
(July 2004), which is free to use with credit, cropped and resampled to
2048×313 px.

To try it:

```bash
pnpm install
pnpm build
cd examples/explainers/reprojection-explainer
pnpm dev
```

Then open `?scenario=arctic`, `?metric=output`, or both.

Closes #708

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
kylebarron added a commit that referenced this pull request Oct 15, 2026
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_
(#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. #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
#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 (#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.
- #713 (tiles centered on the equator never converge), #715 (geographic
bounds of rotated images are too small), and `MosaicLayer` and
`MultiCOGLayer` source bounds across 180° (#639).
- Tiles that contain a pole have no continuous unwrap.

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

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
kylebarron added a commit that referenced this pull request Oct 16, 2026
> [!NOTE]
> This PR was written by Claude (Claude Code), not by @kylebarron.

Every layer now measures mesh error in output pixels. `RasterLayer`
always builds its mesh with `RasterReprojector`'s output mode (#711),
and so do the layers built on `RasterTileLayer`: `COGLayer`, `ZarrLayer`
and `MultiCOGLayer`. The experimental `_errorSpace` prop is removed from
`RasterLayer` and `RasterTileLayer`. `@developmentseed/raster-reproject`
keeps both modes, and its own default stays `"input"`.

**What changes for users**

- **`maxError` is in output pixels.** The default is still 0.125. It now
bounds how far the mesh may draw a point from where it belongs on the
map, in units of one input pixel's size on the map, normally measured at
the center of the meshed area. Before, it bounded a distance in the
source image, found through the inverse projection.
- **Imagery across the antimeridian draws by default.** Every Web
Mercator tile now gets #716's unwrap, which needs output mode, so an
image that crosses 180° is meshed in one piece and cut at the world's
edge. Before, unless `_errorSpace: "output"` was set, such a tile hit
the iteration cap and was drawn the long way around the world. The globe
is unchanged: it still draws a uniform grid.
- **Meshing no longer calls the inverse projection.**

**Why**

Output mode needs only the forward projection, and #711 found it at
least as accurate on screen in every case it measured. On a polar
EPSG:3031 COG, input mode alone drew the tiles along the 180th meridian
as streaks across the map, overflowed the stack on some of them, and
stalled at the iteration cap on every tile touching 85.05°. Both modes
fail on the tile that contains the pole.

**Costs**

Every tile meshed as `RasterTileLayer` meshes it in Web Mercator, at
`maxError` 0.125:

- **Ordinary tiles**, 3,328 tiles of raster-reproject's NLCD, MODIS,
NAIP, LINZ and CC11 fixtures built into pyramids of 512 px tiles: 1.9%
more triangles (110,989 vs 108,938) and 50% more meshing time (0.215 vs
0.143 ms per tile, medians of 7 runs). About half of the extra time is
output mode's meshing itself; the rest is the unwrap and the cut at the
world's edge.
- **A global EPSG:4326 pyramid** takes 24% more triangles and 79% more
time. Its tiles touching 85° take about 3x the triangles and 4x the
time, because input mode left them 0.54 to 0.79 output pixels off on the
map, against 0.10 to 0.13 now.
- **Images across 180°** go from the iteration cap (10,003 to 20,004
triangles, drawn across the world) to 2 to 40 triangles.

**Examples**

- cog-basic, land-cover and custom-projection lose their "Mesh error
measured" select, and cog-basic no longer switches modes when an
antimeridian source is picked.
- custom-projection drops its `meshXScale` column stretch, which only
made input-pixel error track error on the map. Its polar views now take
25 to 35% fewer triangles and mesh about 5x faster.
- The reprojection explainer still compares both modes through
`RasterReprojector`.

**Tests**

The layer tests now describe one mode. `RasterLayer` builds its mesh
when its inverse functions throw, and meshes an image across the
antimeridian with `_wrapX` alone. `RasterTileLayer` gives every Web
Mercator tile `_wrapX` 512, and globe tiles none. Six `deck.gl-raster`
tests and eight `deck.gl-geotiff` antimeridian fixture tests fail on
main and pass here. The tests of the removed prop are gone.

**Not changed here**

- custom-projection's 89.5° cut at the poles. Output mode converges up
to the pole, so the cut now saves only about 1% of the triangles, but
removing it would change what the example draws.
- Tiles that contain a pole, which neither mode meshes, and #713's
equator stall, which happens in both modes.

🤖 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