Skip to content

perf(proj): resolve common EPSG codes without a request, share in-flight ones - #760

Merged
kylebarron merged 3 commits into
mainfrom
kyle/epsg-resolver-builtin
Oct 21, 2026
Merged

kylebarron merged 3 commits into
mainfrom
kyle/epsg-resolver-builtin

Conversation

@kylebarron

@kylebarron kylebarron commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Note

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

Closes #211

The default epsgResolver asked epsg.io for every EPSG code before a layer could load its first tile, including codes proj4 already ships, and it asked once per caller: PROJECTION_REGISTRY only filled after the first request resolved, so the three sources of a MultiCOGLayer or the 112 sources of a MosaicLayer each made their own request for the same code. Measured on the sentinel-2 example: three parallel 210 ms requests for EPSG:32718 before the first tile.

Changes in packages/proj:

  • Codes proj4 knows resolve without a request. proj4.defs("EPSG:<code>") says whether proj4 has a definition (4326, 4269, 3857, 900913 and every UTM zone 326xx/327xx), and new proj4.Proj(code) fills in what the proj string leaves out, such as the ellipsoid axes that the layers read as a to size degree units. No hard-coded table; proj4 becomes a dependency of @developmentseed/proj (it was a dev dependency; every consumer already depends on it).
  • One registry. PROJECTION_REGISTRY now holds Promise<ProjectionDefinition> per EPSG:<code>, so concurrent callers share one in-flight request and there is no second map. A rejected entry is removed, so the next caller retries instead of inheriting the failure.
  • registerProjection(epsg, wktOrProjjson) lets an application bundle its own definitions (WKT or PROJJSON) so the resolver never fetches them. The epsgResolver prop still allows a fully custom resolver, as land-cover uses with the @developmentseed/epsg database.
  • metersPerUnit accepts "degrees", proj4's spelling, next to wkt-parser's "degree".

Tests (packages/proj/tests/registry.test.ts)

  • 4326, 3857, 4269, 32618 and 32718 resolve with fetch stubbed to throw, expose units and a the way the layers read them, and project a point to within 1 mm of proj4's own code.
  • Three concurrent resolutions of 5070 make one request and get the same object; a later call makes none.
  • A 503 rejects both waiters with "Failed to fetch PROJJSON", and the next call retries.
  • A registered projection resolves without a request.

Part of the October 2026 performance research (dev-docs/perf-research-2026-10.md, area G1).

🤖 Written by Claude Code

…ght ones

The default epsgResolver asked epsg.io for every code before a layer could
load its first tile, including 4326, 3857 and 4269, which proj4 already
knows, and it asked once per caller: the three sources of a MultiCOGLayer
or the 112 sources of a mosaic each made their own request for the same
code (measured: three parallel 210 ms requests for EPSG:32718 on the
sentinel-2 example), because PROJECTION_REGISTRY only fills after the
first one resolves.

Ship PROJJSON for 4326, 3857 and 4269 (copied from epsg.io) and resolve
them from that table, and keep one in-flight promise per code so concurrent
callers share a request. A failed request is not cached: the next caller
retries. Fixes #211.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@github-actions github-actions Bot added the perf label Oct 21, 2026
Comment thread packages/proj/src/builtin-projjson.ts Resolved
Comment thread packages/proj/src/builtin-projjson.ts Resolved
Comment thread packages/proj/src/builtin-projjson.ts Resolved
Comment thread packages/proj/src/builtin-projjson.ts Resolved
Comment thread packages/proj/src/registry.ts Resolved
Review follow-up: drop the hard-coded PROJJSON table. proj4 already ships
definitions for 4326, 4269, 3857 and every UTM zone; `proj4.defs` says
whether it knows a code and `new proj4.Proj(code)` fills in the ellipsoid
axes that layers read as `a`. Resolve those without a request.

Merge the in-flight map into PROJECTION_REGISTRY by storing promises, so
there is one registry; a rejected entry is removed so the next caller
retries. Add registerProjection(epsg, wktOrProjjson) for applications that
bundle their own definitions. Accept "degrees" (proj4's spelling) in
metersPerUnit. proj4 becomes a dependency of @developmentseed/proj.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Comment thread packages/proj/src/registry.ts Resolved
Comment thread packages/proj/src/registry.ts Resolved
"vitest": "^5.0.2"
},
"dependencies": {
"proj4": "^2.22.0",

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.

hmm, originally I was thinking the proj library could be independent of a specific projection library (in case users wanted to use a wasm-based proj or similar in the future) but that's something we could revisit down the line

@kylebarron
kylebarron merged commit 89a8053 into main Oct 21, 2026
3 checks passed
@kylebarron
kylebarron deleted the kyle/epsg-resolver-builtin branch October 21, 2026 18:59
kylebarron added a commit that referenced this pull request Oct 22, 2026
> [!NOTE]
> This PR was written by Claude (Claude Code), not by @kylebarron.

Bumps `version` to `0.9.0-beta.3` in the root `package.json` and in
every `packages/*/package.json`, ready for a third beta release. The
publish workflow sees `beta` in the version and publishes to npm under
the `beta` dist-tag, so `latest` stays on 0.8.1.

`CHANGELOG.md` gets a `v0.9.0-beta.3` section listing the 16 PRs merged
since v0.9.0-beta.2. Two of them change the API in ways that break
existing code:

- #769 renames `CachedTags` to `CachedPrimaryImageTags` in
`@developmentseed/geotiff`, and `cachedTags` to `cachedPrimaryImageTags`
on `GeoTIFF` and `Overview`.
- #760 isn't marked breaking, but `PROJECTION_REGISTRY` in
`@developmentseed/proj` now holds `Promise<ProjectionDefinition>`
values, so code that sets entries directly should call
`registerProjection` instead. `@developmentseed/proj` also depends on
proj4 now.

Once this is merged, tag the merge commit `v0.9.0-beta.3` and push the
tag.

🤖 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 22, 2026
> [!NOTE]
> This PR was written by Claude (Claude Code), not by @kylebarron.

The deck.gl-geotiff quick start doesn't build with a default Vite
config. The GeoTIFF decoder worker uses top-level await and dynamic
imports, and Vite bundles workers in `iife` format by default, so `vite
build` fails:

- Vite 8: `Top-level await is currently not supported with the 'iife'
output format`
- Vite 7: `Invalid value "iife" for option "worker.format" - UMD and
IIFE output formats are not supported for code-splitting builds.`

`vite dev` doesn't bundle workers, so this only shows up in production
builds. Every example sets `worker: { format: "es" }`, but no README
mentioned it.

### Changes

- **deck.gl-geotiff README:** a "Using Vite" section after the quick
start, with the config and both error messages, so searching for either
finds it.
- **geotiff README:**
  - The same setting under the worker pool section.
  - Fixes for three snippets that didn't compile:
- `registry` is exported as `DECODER_REGISTRY`. The `pool/worker` doc
comment had the same mistake.
- `GeoTIFF.open` takes `{ headerSource, dataSource }`. The example now
reads the header through the chunked, cached source and tile data
straight from HTTP, as `GeoTIFF.fromUrl` does.
- `data` exists only on pixel-interleaved arrays, so the example checks
`layout` before reading it.
- It also finishes a sentence that was cut off, and says a decoder can
return decoded pixels as well as bytes.
- It links `Decoder` in the API docs instead of `./src/decode.ts`, which
404s on the docs site.
- **proj README:** it said the package doesn't depend on proj4, which
stopped being true in #760.

### Checked

- A Vite app whose `main.ts` is the quick start fails `vite build` on
Vite 8.3.5 and 7.3.7 with the default config. With the README's config,
copied verbatim, it builds on both and emits the worker chunk.
- With `strict` on, every `ts` block in the two READMEs except the Vite
config typechecks against this branch's build. Before, three failed:
`TS2305` (`registry`), `TS2739` (`GeoTIFF.open`) and `TS2339` (`data`).
- The docs site builds, and the three API pages show the new text.

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

Don't make fetch request for globally-known proj4 definitions

1 participant