Skip to content

refactor(proj): share projectionFunctions and geographicBounds through @developmentseed/proj - #811

Merged
kylebarron merged 4 commits into
mainfrom
kyle/proj-projection-helpers
Oct 28, 2026
Merged

kylebarron merged 4 commits into
mainfrom
kyle/proj-projection-helpers

Conversation

@kylebarron

@kylebarron kylebarron commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Note

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

Stage 4b of the Sources design, part 1 of 2 (dev-docs/specs/2026-10-22-sources-design.md, decision 23). Two projection helpers move into @developmentseed/proj, so that COGSource, MultiCOGSource and the coming ZarrSource share one copy. Behavior is unchanged except for one fix, described below.

  • projectionFunctions(projection) moves from deck.gl-geotiff. It gives the functions that project a CRS to and from WGS84 and Web Mercator, and the CRS's meters per unit. It no longer returns proj4's converter, which only the bounds calculation used.
  • geographicBounds(outline, projectTo4326) is the antimeridian-aware walk from deck.gl-geotiff's getGeographicBounds. It takes any ring of points in a CRS and returns [west, south, east, north]. A ring across the antimeridian is bounded the short way, with east past 180°. COG bounds pass the image's bounding box as the ring. ZarrSource will pass the corners of its finest level.
  • deck.gl-geotiff keeps resolveProjection, which reads a GeoTIFF's crs. getGeographicBounds stays internal, and now takes a projection function.
  • ZarrLayer drops its own copy of the projection functions.

Neither deck.gl-geotiff's source nor deck.gl-zarr's imports proj4 anymore. deck.gl-geotiff's tests still do. deck.gl-zarr's package.json lists proj4 and wkt-parser, which nothing there imports now. I left both package.json files alone; trimming them can be a follow-up.

One fix

geographicBounds keeps latitudes within -90° to 90°.

  • Why: a global grid whose cells are centered on the poles reaches half a cell past them. The ECMWF example's 0.25° grid, with 721 rows from 90°N, comes out at -90.25°, or ±90.125° with node registration.
  • Effect: MapLibre's fitBounds throws on such a latitude. The ZarrSource review found this for that grid.

Limits, now documented

geographicBounds's docs state two limits that the old code already had:

  • The ring isn't closed. The walk stops at the last point and doesn't follow the edge back to the first, so a ring around a pole isn't bounded correctly. A polar stereographic image already got wrong bounds before this PR.
  • Latitudes come from the points alone. An edge that curves past them, such as a box's top edge in a conic projection, can reach past the bounds.

Fixing either would change behavior, so I left them for their own PR.

Tests

  • proj: 9 new tests.
    • projectionFunctions, on UTM 60N and WGS84 parsed from WKT: projects to WGS84 and back, matches proj4 for Web Mercator, gives the meters per unit, and rejects a projection without units.
    • geographicBounds, on five rings:
      • a ring reaching past the poles, whose latitudes are clamped;
      • a UTM 60N ring across the antimeridian, checked against proj4's corner longitudes;
      • a ring stored past -180°;
      • a ring all the way around the world, through a projection that wraps every longitude into [-180°, 180°), so only the walk along the ring can see it;
      • a ring that isn't a box.
    • Replacing the walk with each corner's own longitude fails the antimeridian and whole-world tests.
  • deck.gl-geotiff: the existing fixture tests for getGeographicBounds keep their expected values. They cover the antimeridian, a rotated image and the whole world. Only their helper changed, to pass a projection function. COGSource's and MultiCOGSource's tests run the real projectionFunctions.
  • Same results: the final review ran the old and new code over 2,400 random boxes in 6 CRSs, and every result was identical.
  • In Chrome:
    • cog-basic fits the map to the Fiji image across the antimeridian, as before.
    • dynamical-zarr-ecmwf draws through ZarrLayer with the new projectionFunctions.

🤖 Written by Claude Code

kylebarron and others added 3 commits October 27, 2026 17:27
Two helpers move here from deck.gl-geotiff, so that COGSource,
MultiCOGSource and ZarrSource can share them:

- `projectionFunctions(projection)` gives the functions that project a
  CRS to and from WGS84 and Web Mercator, and its meters per unit.
- `geographicBounds(outline, projectTo4326)` gives the WGS84 bounds of a
  ring of points in a CRS, such as an image's corners. It walks the ring
  so that a ring across the antimeridian is bounded the short way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
COGSource and MultiCOGSource take `projectionFunctions` from proj, and
`getGeographicBounds` passes a GeoTIFF's bounding box to proj's
`geographicBounds`. It now takes a projection function rather than
proj4's converter, which `projectionFunctions` no longer returns.
ZarrLayer drops its own copy of the projection functions.

Neither package's source imports proj4 anymore.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- `geographicBounds` doesn't walk the edge from the last point back to
  the first, so a ring around a pole isn't bounded correctly. Its
  latitudes come from the points alone. The docs no longer claim the
  pole case, and say to add points along curved edges.
- `projectTo3857` clamps a latitude only where proj4 can't project the
  point, not always.

The whole-world test now uses a projection that wraps longitudes, so
only the walk along the ring can see that the ring goes all the way
around. The old one passed without the walk.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
import { epsgResolver as defaultEpsgResolver } from "@developmentseed/proj";
import {
epsgResolver as defaultEpsgResolver,
projectionFunctions,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the name projectionFunctions as a public export from @developmentseed/proj.

Someone just coming to that library would have no idea what it does

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eh, I think this is fine for now

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

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

We discussed projectionsTo4326And3857 and crsToWgs84AndWebMercator, which name the targets. We're keeping projectionFunctions for now, and can rename it later if it confuses people. The JSDoc spells out what it returns: the four functions to and from WGS84 and Web Mercator, and the CRS's meters per unit.

🤖 Written by Claude Code

A global grid whose cells are centered on the poles, such as a 0.25°
grid of 721 rows from 90°N, reaches half a cell past them, so its bounds
came out at -90.125° or -90.25°. MapLibre's `fitBounds` throws on a
latitude outside -90° to 90°. The bounds now clamp it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kylebarron
kylebarron merged commit 7a27a39 into main Oct 28, 2026
4 checks passed
@kylebarron
kylebarron deleted the kyle/proj-projection-helpers branch October 28, 2026 00:27
kylebarron added a commit that referenced this pull request Oct 28, 2026
> [!NOTE]
> This PR was written by Claude (Claude Code), not by @kylebarron.

Stage 4b of the Sources design, part 2 of 2
(`dev-docs/specs/2026-10-22-sources-design.md`, decisions 21 to 23). It
builds on #811, which moved the projection helpers into
`@developmentseed/proj`.

`ZarrSource.open` opens GeoZarr data once, and `ZarrLayer` draws it:

```ts
import { ZarrLayer, ZarrSource } from "@developmentseed/deck.gl-zarr";

const source = await ZarrSource.open(group, { variable: "temperature" });
const { west, south, east, north } = source.geographicBounds;

new ZarrLayer({
  id: "temperature",
  source,
  selection: { time: 0 },
  getTileData,
  renderTile,
});
```

- **`ZarrSource.open(node, options)`** takes a zarrita group or array.
It does four things:
- reads the GeoZarr metadata from the node's attributes, or from
`geoZarrMetadata`;
  - opens the array of each level;
  - resolves the CRS;
  - builds the tile grid and the WGS84 bounds of the finest level.

Its options are `variable`, `geoZarrMetadata`, `epsgResolver` and
`signal`.
- **The source holds:**
  - `arrays`, one per level, finest first;
  - `dimensionNames` and `spatialDims`;
  - `projection`;
  - `tilesetDescriptor` and `geographicBounds`. Bounds are new for Zarr.

It doesn't keep the parsed GeoZarr metadata, since nothing outside
`open` needs it (question 9).
- **ZarrLayer's `source`** takes a `ZarrSource`, or a group or array for
the layer to open with the defaults. `onSourceLoad` gets the source the
layer opened. As in COGLayer, RasterTileLayer handles the rest.
- **`selection` stays on the layer** (decision 22). The layer checks it
against the source whenever either differs from the pair it last
checked. That includes a source opened from input arriving, and a
selection that changed while no source was drawn.
- A selection that doesn't fit goes to `onError` as "Can't draw the
selection in ZarrLayer: ...", and the layer draws nothing until the
selection is fixed. Before, an invalid selection failed the whole load,
as "Metadata loading failed in ZarrLayer: ...".
  - Changing the selection never opens the source again.
- Selections are compared by value, so an inline literal isn't checked
again on every render.
- Tiles already loaded still keep the data of the selection they were
fetched with, until stage 4c. The `selection` docs now say so; the old
docs recommended `updateTriggers`, which doesn't fetch again.
- **Edge tiles** are clipped to each array's own shape, rather than to
the GeoZarr metadata's.
- **Cleanup:** the `window.*` debug lines are gone.
- **Readonly:** `arrays` and `dimensionNames` are readonly, since layers
share a source.

## Migration

`node`, `variable`, `metadata` and `epsgResolver` are removed from
ZarrLayer. `metadata` becomes the `geoZarrMetadata` option of
`ZarrSource.open`.

```ts
// Before
new ZarrLayer({
  node: group,
  variable: "temperature",
  metadata: attrs,
  selection,
  getTileData,
  renderTile,
});

// After: a group or array as the source...
new ZarrLayer({ source: group, selection, getTileData, renderTile });

// ...or, to use the options, an opened source
const source = await ZarrSource.open(group, {
  variable: "temperature",
  geoZarrMetadata: attrs,
});
new ZarrLayer({ source, selection, getTileData, renderTile });
```

TypeScript rejects the removed props. JavaScript ignores them, and
without a `source` the layer draws nothing.

The type parameters are reordered:
- **ZarrLayer:** `ZarrLayer<Store, Dtype, DataT>` becomes
`ZarrLayer<DataT, Dtype, Store>`, and `ZarrLayerProps` changes the same
way. The tile data type comes first, as in deck.gl's `TileLayer<DataT>`
and `COGLayer<DataT>`, and then zarrita's order, as in
`zarr.Array<Dtype, Store>`.
- **ZarrSource:** it follows zarrita: `ZarrSource<Dtype, Store>`.

```ts
// Before
new ZarrLayer<zarr.Readable, "int8", TileData>({ ... });

// After
new ZarrLayer<TileData, "int8">({ ... });
```

## Examples and docs

- **Three examples open a `ZarrSource`:** aef-mosaic,
dynamical-zarr-ecmwf and nldas-icechunk pass `geoZarrMetadata`, in the
effect that already opens their array. In nldas-icechunk, the store
module returns the source.
- **zarr-sentinel2-tci** passes its group as `source`.
- **README:** deck.gl-zarr's README shows both forms.
- **Spec:** it records the answer to question 9, and stage 4b as built.

## Tests

- **`ZarrSource`:** 12 tests against an in-memory GeoZarr store. They
cover:
- levels finest first, `variable`, `geoZarrMetadata`, the dimension
names and the tile grid;
  - the bounds, checked against proj4's corners;
  - node registration moving the bounds by half a pixel;
- a global grid centered on the poles staying within -90° to 90°, using
#811's clamp;
  - the signal reaching every read, and an aborted signal;
  - dimensions without names, and x before y.
- **ZarrLayer:**
  - An opened source draws without opening anything.
  - The load signal reaches `ZarrSource.open`.
  - The newer source wins a race.
  - A selection that doesn't fit:
    - is reported, and draws nothing;
    - draws again once fixed, without opening the source again;
    - isn't reported again for an equal selection;
- is checked, at its newest value, once a source opened from input
arrives;
- is checked when it changed while there was no source and the old
source comes back.
- The tile callback reads the finest level's edge tile at the selection.
The test checks the array, the clipped 176-pixel width and height, and
the slice spec.
  - The 4 `isLoaded` tests and the error-routing test now use `source`.
- **Type tests:**
- `state.source` is exactly `ZarrSource`, and `ZarrSource<"int8">` for
an int8 layer.
  - `onSourceLoad` gets a `ZarrSource`.
  - The removed props are rejected, and so is a string as `source`.
- **Mutation checks:** each of these makes a test fail:
  - not checking the selection when a source arrives;
- comparing the selection with the old props instead of with the pair
last checked;
  - comparing selections by identity;
  - drawing while the selection doesn't fit;
  - mapping levels the wrong way;
  - not clipping edge tiles;
  - leaving the selection out of the slice spec;
  - removing #811's latitude clamp.
- **In Chrome:**
- dynamical-zarr-ecmwf draws as before. Its animation changes the
selection at every step.
  - aef-mosaic draws as before. Switching the year reads no `zarr.json`.
  - nldas-icechunk draws as before.
- zarr-sentinel2-tci reads from a local data server, which I didn't run,
so I only typechecked it.

🤖 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