Spatial Reference
@math.gl/geospatial/crs provides a format-neutral descriptor for reporting how a dataset's coordinates are
referenced. It preserves discovery state and representation without performing a transformation.
For concepts, examples, and integration guidance, see the Coordinate Reference Systems developer guide.
SpatialReferenceโ
type SpatialReference = Readonly<{
crs: CRSReference;
vertical?: CRSReference;
coordinateEpoch?: number;
coordinateFrame: 'geographic' | 'geocentric' | 'projected' | 'local' | 'unknown';
coordinateOrder: readonly string[];
units?: readonly string[];
}>;
crs is the primary horizontal or compound CRS. vertical is available when a source declares a
separate vertical CRS. coordinateOrder describes the source array layout and is independent of
the authoritative axis order in the CRS definition.
CRSReferenceโ
CRSReference is a discriminated union of KnownCRSReference and UnresolvedCRSReference.
A known reference has an explicit or specification-defaulted definition:
type KnownCRSReference = Readonly<{
state: 'explicit' | 'default';
definition: ReadonlyCRSDefinition;
representation: SpatialReferenceRepresentation;
provenance: SpatialReferenceProvenance;
alternatives?: readonly SpatialReferenceAlternative[];
}>;
An unresolved reference distinguishes explicitly unknown from absent metadata:
type UnresolvedCRSReference = Readonly<{
state: 'unknown' | 'absent';
provenance: SpatialReferenceProvenance;
}>;
createSpatialReference()โ
Creates an immutable descriptor, supplying normalized defaults for omitted fields.
createSpatialReference(options?: CreateSpatialReferenceOptions): SpatialReference
PROJJSON definitions and descriptor-owned arrays are cloned and recursively frozen. A non-finite coordinate epoch throws. The function does not parse or resolve CRS definitions.
const spatialReference = createSpatialReference({
crs: {
state: 'default',
definition: 'OGC:CRS84',
representation: 'identifier',
provenance: 'format-default'
},
coordinateFrame: 'geographic',
coordinateOrder: ['x', 'y'],
units: ['degree', 'degree']
});
inferCRSRepresentation()โ
Classifies a CRSDefinition using conservative syntax checks.
inferCRSRepresentation(definition: ReadonlyCRSDefinition): SpatialReferenceRepresentation
It recognizes PROJJSON objects, common WKT roots, PROJ strings with a proj parameter,
authority-like identifiers, OGC URNs, and URLs. Other strings are returned as opaque. It does not
validate syntax, resolve an identifier, or establish semantic equivalence.
normalizeCRSIdentifier()โ
Changes known spellings of one authority CRS identifier to the form AUTHORITY:CODE.
normalizeCRSIdentifier(identifier: CRSIdentifier): CRSIdentifier
Use this function to compare identifiers that have different spellings. For example, a tile matrix
set can give http://www.opengis.net/def/crs/EPSG/0/32619, and a view can give EPSG:32619.
| Input | Result |
|---|---|
epsg:32619 | EPSG:32619 |
urn:ogc:def:crs:EPSG::32619 | EPSG:32619 |
http://www.opengis.net/def/crs/EPSG/0/32619 | EPSG:32619 |
CRS:84, urn:ogc:def:crs:OGC:1.3:CRS84 | OGC:CRS84 |
urn:ogc:def:crs:EPSG:6.6:4326 | unchanged |
WGS84, WKT, PROJ strings | unchanged |
Rules:
- The known authority prefixes EPSG, OGC, ESRI and IAU accept case-insensitive input aliases. Known OGC codes CRS84, CRS83, CRS27 and CRS84h use their registered spelling; other authority and code spellings are preserved. This convenience does not imply that registry identifiers are generally case-insensitive.
- The function changes the WMS 1.3 codes
CRS:84,CRS:83andCRS:27toOGC:CRS84,OGC:CRS83andOGC:CRS27. - The function removes a URN or URI version only in these cases:
- The version is empty.
- The version is
0. - The version is
1.3and the code is OGCCRS84,CRS83orCRS27. The OGC register publishes these three codes only at version 1.3.
- The function returns all other versioned identifiers unchanged. Thus a versioned identifier does not compare equal to an unversioned identifier.
- The function returns unknown strings unchanged.
The function does not make different registry entries equal. EPSG:4326 and OGC:CRS84 stay
different, because their axis orders are different. EPSG:900913 stays different from
EPSG:3857. The function does not resolve definitions. It does not make datums equal, and it does
not change axis order.
For example, http://www.opengis.net/def/crs/OGC/0/CRS84h becomes OGC:CRS84h,
while OGC:CustomCase keeps its code spelling.
Keep the original identifier of the source in the metadata. Use the result only to compare identifiers.
getCRSAuthorityAxisOrder()โ
Gives the axis order that the CRS authority defines for the first two coordinates.
type CRSAuthorityAxisOrder = 'xy' | 'yx';
getCRSAuthorityAxisOrder(definition: ReadonlyCRSDefinition): CRSAuthorityAxisOrder | undefined
xy: the first axis is easting or longitude.yx: the first axis is northing or latitude.undefined: the function does not know the order.
OGC services and formats use the authority axis order on the wire. Examples are WMS 1.3
BBOX, WMTS tile matrix origins, WFS 2.0 and GML. Use this function to build a request or to
read such a value. Keep application coordinates in your own order.
import {getCRSAuthorityAxisOrder} from '@math.gl/geospatial/crs';
getCRSAuthorityAxisOrder('EPSG:4326'); // 'yx' (latitude, longitude)
getCRSAuthorityAxisOrder('urn:ogc:def:crs:OGC:1.3:CRS84'); // 'xy' (longitude, latitude)
getCRSAuthorityAxisOrder('http://www.opengis.net/def/crs/EPSG/0/3035'); // 'yx' (northing first)
getCRSAuthorityAxisOrder('EPSG:2056'); // undefined (not in the table)
The function reads the order from the definition:
| Input | Source of the order |
|---|---|
| PROJJSON | direction of the first two entries of coordinate_system.axis. A BoundCRS uses source_crs. A CompoundCRS uses its first component. |
| WKT1, WKT2 | Direction of the first two AXIS nodes of the root CRS, sorted by ORDER when every axis has one. The function ignores the axes of a nested base CRS. A BOUNDCRS uses SOURCECRS. A compound CRS uses its first component. |
| Identifier | A small built-in table. See below. |
| PROJ string | None. The function returns undefined. |
The identifier table contains these entries. URN and URI forms are normalized with
normalizeCRSIdentifier(). A versioned EPSG URN or URI, such as
urn:ogc:def:crs:EPSG:6.3:4326, uses the entry of its code. The source of each EPSG entry is
the CRS record at https://epsg.org/crs_<code>/ and the coordinate system record that it names.
| Result | Identifiers | EPSG coordinate system |
|---|---|---|
xy | OGC:CRS84, OGC:CRS83, OGC:CRS27, OGC:CRS84h (also CRS:84, CRS:83, CRS:27) | โ (OGC register, longitude first) |
xy | EPSG:3857 | 4499: easting, northing |
xy | EPSG:32601โEPSG:32660, EPSG:32701โEPSG:32760 (WGS 84 / UTM) | 4400: easting, northing |
yx | EPSG:4148, 4167, 4171, 4230, 4258, 4267, 4269, 4277, 4283, 4314, 4326, 4490, 4612, 4617, 4619, 4674, 6318, 6668, 7844 | 6422: latitude, longitude |
yx | EPSG:2193, 3006, 3034 | 4500: northing, easting |
yx | EPSG:2180 | 4531: northing, easting |
yx | EPSG:3035 | 4532: northing, easting |
yx | EPSG:31466โEPSG:31469 | 4530: northing, easting |
The function returns undefined in these cases:
- The identifier is not in the table. The function does not look up a registry.
- The input is a PROJ string, opaque text or malformed WKT.
- The definition has no axes. For example, many WKT1 strings have no
AXISnodes. - The first two axes are not one east/west axis and one north/south axis. Examples are geocentric axes and polar axes that have a meridian.
This order is not the same as CRSMetadata.axis. CRSMetadata.axis gives
the effective array order of a resolved CRS in the projection engine. The engine uses longitude
or easting first for EPSG:4326 and OGC:CRS84 unless you set enforceAxis. The authority
axis order describes the definition, and the engine does not change it.
Representation valuesโ
SpatialReferenceRepresentation is one of:
identifierwktproj-stringprojjsonopaque
For a defaulted reference, representation describes the normalized definition selected by the
adapter. The default state and format-default provenance record that the source did not
serialize that value.
State valuesโ
SpatialReferenceState is one of:
explicitdefaultunknownabsent
Only explicit and default references contain a definition. Narrow on state before accessing
definition, representation, or alternatives.
ReadonlyCRSDefinition is a serialized string or a deeply readonly PROJJSON object. Mutable
PROJJSON inputs remain assignable to this type, while descriptors returned by
createSpatialReference() prevent both top-level and nested mutation at compile time and runtime.
Provenance valuesโ
SpatialReferenceProvenance is one of:
metadataformat-defaultcaller-overridelegacy-assumptionunknown
Coordinate-frame valuesโ
SpatialReferenceCoordinateFrame is one of:
geographicgeocentricprojectedlocalunknown
The frame describes stored coordinates. It is intentionally broader than PROJJSON CRS types so a loader can report local coordinate systems that do not have a conventional CRS definition.
Schema and field metadataโ
SPATIAL_REFERENCE_METADATA_KEY is vis.gl:spatial_reference. Its value is a JSON-serialized
SpatialReference describing current coordinate values, not just their original source CRS.
The convention works with Apache Arrow schema and field metadata without importing Arrow.
Format-specific metadata can separately preserve original definitions and source provenance.
- Schema metadata supplies a default for coordinate fields identified by the consumer. It does not label every numeric field as a coordinate.
- Field metadata overrides the entire schema descriptor. An explicit
unknownorabsentfield reference blocks inheritance. A missing key allows inheritance. - Coordinate order describes stored components, regardless of the CRS's authoritative axis order.
- Reprojection must update the descriptor. Selection and renaming can preserve it. Arbitrary coordinate calculations must not inherit a reference unless their coordinate meaning is known.
- GeoArrow geometry fields use their standard extension metadata as the authority. Adapters must handle that metadata before these helpers and reject conflicting declarations; these helpers do not inspect GeoArrow extensions or determine CRS equivalence.
import {
createSpatialReference,
setSpatialReferenceMetadata,
resolveSpatialReferenceMetadata
} from '@math.gl/geospatial/crs';
const schemaMetadata = setSpatialReferenceMetadata(undefined, createSpatialReference({
crs: {
state: 'explicit',
definition: 'EPSG:4326',
representation: 'identifier',
provenance: 'metadata'
},
coordinateFrame: 'geographic',
coordinateOrder: ['longitude', 'latitude'],
units: ['degree', 'degree']
}));
// Pass this map to an Arrow Schema. Resolve metadata for a selected coordinate field:
const reference = resolveSpatialReferenceMetadata(schemaMetadata, positionField.metadata);
setSpatialReferenceMetadata(metadata, spatialReference)โ
Returns a new Map<string, string> with the serialized descriptor, preserving unrelated entries.
Accepts ReadonlyMap<string, string> | undefined and a SpatialReference. Does not modify the
input map or descriptor. Replaces any existing value at SPATIAL_REFERENCE_METADATA_KEY.
getSpatialReferenceMetadata(metadata)โ
Accepts ReadonlyMap<string, string> | undefined. Returns a deeply immutable SpatialReference,
or undefined if the key is missing. Malformed JSON and invalid descriptor envelopes throw.
resolveSpatialReferenceMetadata(schemaMetadata, fieldMetadata?)โ
Accepts metadata maps and returns the field declaration when present, otherwise the schema
reference. Returns undefined when neither declares a reference. Invalid field metadata throws
rather than falling back to the schema.
The helpers validate states, representations, component arrays, and finite coordinate epochs. They do not validate WKT, PROJ strings, or the full PROJJSON schema, resolve identifiers, infer missing CRSs, or load a projection engine. In streaming data, producers must keep declarations consistent with each batch's coordinate values; these helpers do not reconcile batches.