Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Last active January 10, 2026 07:25
Show Gist options
  • Select an option

  • Save mdsumner/fb8373927fe026cd8bf6bf6700179040 to your computer and use it in GitHub Desktop.

Select an option

Save mdsumner/fb8373927fe026cd8bf6bf6700179040 to your computer and use it in GitHub Desktop.

proj4js Wrapper with Authority Code Resolution

A thin wrapper around proj4 that accepts multiple CRS input formats and automatically resolves authority codes via spatialreference.org.

Supported Input Formats

Format Example Handling
Proj4 string +proj=stere +lat_0=-90 ... Native proj4js
WKT1 PROJCS["Antarctic",...] Native proj4js
Authority code EPSG:3031, ESRI:2181 Fetch from spatialreference.org

Authority Code Resolution

When input matches pattern <AUTH>:<CODE> (case-insensitive):

  1. Check if already registered via proj4.defs
  2. If not, fetch: https://spatialreference.org/ref/{auth}/{code}/ogcwkt/
  3. Register with proj4.defs('{AUTH}:{CODE}', fetchedWKT)
  4. Return transformer
// Example resolution
"EPSG:3031"  fetch https://spatialreference.org/ref/epsg/3031/ogcwkt/
"ESRI:2181"  fetch https://spatialreference.org/ref/esri/2181/ogcwkt/
"SR-ORG:7483"  fetch https://spatialreference.org/ref/sr-org/7483/ogcwkt/

// Returns WKT1 like:
// PROJCS["WGS 84 / Antarctic Polar Stereographic",...]

API

// Async wrapper - handles all formats
const proj = await resolveProj('EPSG:3031');
proj.forward([lon, lat]);  // → [x, y]

// Or transform directly
const [x, y] = await transform('EPSG:4326', 'EPSG:3031', [0, -71]);

Implementation Notes

  • Cache fetched definitions in proj4.defs for reuse
  • Return meaningful errors on fetch failure (404, network)
  • Timeout after ~5 seconds
  • Authority names are lowercased for URL: EPSGepsg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment