Source code for geodetic_engine.geodesy.crs

"""Coordinate reference systems, and the axis and unit contract they declare.

Two different orderings are in play throughout this package, and conflating
them is the single easiest way to ship wrong coordinates:

* The **declared axis order** is what the EPSG dataset says the CRS's axes are,
  in the order EPSG defines them. ``EPSG:4326`` declares ``(Lat, Lon)``. This
  module reports that order faithfully and never reinterprets it.
* The **coordinate value order** is the order this package accepts and returns
  numbers in, which is always ``xy`` (longitude then latitude, easting then
  northing). See :mod:`geodetic_engine.geodesy.transformation`.

So for ``EPSG:4326``, :attr:`CoordinateReferenceSystem.axis_abbreviations` is
``("Lat", "Lon")`` while the coordinate values are ordered ``(lon, lat)``. That
is deliberate: the metadata describes the CRS, not this package's calling
convention.
"""

from __future__ import annotations

import logging
import re
from dataclasses import dataclass
from functools import lru_cache
from pathlib import Path
from typing import Any

from pyproj import CRS
from pyproj.exceptions import CRSError

from geodetic_engine._referencing.axes import value_axis_order
from geodetic_engine._referencing.database import (
    DatabaseIdentity,
    bound_definition,
    bound_definition_by_name,
    database_identity,
    skip_reason,
)
from geodetic_engine._referencing.osdu_id import (
    CRS_ENTITY,
    OsduId,
    parse_osdu_id,
    resolve_osdu_id,
    unresolved_reason,
)
from geodetic_engine.geodesy.errors import (
    UnembeddableOperationError,
    UnresolvableCRSError,
)
from geodetic_engine.persistablereference import (
    CatalogPersistableReference,
    CrsReference,
    PersistableReferenceError,
    looks_like_reference,
    parse_persistable_reference,
    persistable_reference_from_catalog,
)

logger = logging.getLogger(__name__)

# A dynamic reference frame is one whose coordinates move with time, and PROJ
# records that by giving the datum a reference epoch. An ensemble or a static
# datum has no such key.
_DYNAMIC_MARKER = "frame_reference_epoch"

# Strict authority and code tokens for from_authority(), checked before PROJ
# sees them because pyproj.CRS.from_authority concatenates them into a string
# for proj_create, which would otherwise parse trailing text as a PROJ string.
_AUTHORITY = re.compile(r"[A-Za-z][\w.-]*\Z", re.ASCII)
_CODE = re.compile(r"[\w.-]+\Z", re.ASCII)


[docs] @dataclass(frozen=True, slots=True) class AxisSpec: """One axis of a CRS, exactly as the EPSG dataset declares it. Attributes: name: Full axis name, for example ``"Geodetic latitude"``. abbrev: Axis abbreviation, for example ``"Lat"``. direction: Positive direction, for example ``"north"``. unit_name: Name of the axis unit, for example ``"degree"``. unit_code: Authority code of the unit, for example ``"9122"``. unit_conversion_factor: Multiplier from this unit to the SI unit of the same quantity (radians for angles, metres for lengths). """ name: str abbrev: str direction: str unit_name: str unit_code: str unit_conversion_factor: float
[docs] class CoordinateReferenceSystem: """A resolved CRS together with the axis roles and units it declares. Wraps :class:`pyproj.CRS` so that a caller never has to read PROJ's source to find out how many axes a CRS has, what they mean, or what units they are in. Construction is cached, since resolving a CRS is expensive relative to transforming a point. Example: >>> crs = CoordinateReferenceSystem.from_user_input("EPSG:4326") >>> crs.axis_abbreviations ('Lat', 'Lon') >>> crs.axis_units ('degree', 'degree') >>> crs.dimension 2 The declared order above is EPSG's. Coordinate *values* for this CRS are still ordered ``(lon, lat)`` everywhere in this package. """ __slots__ = ("_axes", "_crs", "_definition")
[docs] def __init__(self, crs: CRS, definition: str) -> None: """Wrap an already-resolved :class:`pyproj.CRS`. Prefer :meth:`from_user_input`, which caches. This constructor exists for the case where a :class:`pyproj.CRS` is already in hand. Args: crs: The resolved CRS. definition: The input it was resolved from, kept for error messages and for :func:`repr`. """ self._crs = crs self._definition = definition self._axes = tuple( AxisSpec( name=axis.name, abbrev=axis.abbrev, direction=axis.direction, unit_name=axis.unit_name, unit_code=axis.unit_code, unit_conversion_factor=axis.unit_conversion_factor, ) for axis in crs.axis_info )
[docs] @classmethod def from_user_input(cls, value: Any) -> CoordinateReferenceSystem: """Resolve a CRS from an EPSG code, WKT, PROJ string or CRS object. Args: value: An authority code such as ``"EPSG:4326"`` or ``4326``, a WKT string, a PROJ string, a PROJJSON string, an OSDU ``persistableReference`` payload, an OSDU record id such as ``"opendes:reference-data--CoordinateReferenceSystem:Projected:EPSG::23031"`` (see :meth:`from_osdu_id`), or an existing :class:`pyproj.CRS` or :class:`CoordinateReferenceSystem`. Returns: The resolved CRS. Raises: UnresolvableCRSError: If PROJ cannot construct a CRS from the input. Example: >>> CoordinateReferenceSystem.from_user_input(4326).authority_code 'EPSG:4326' """ if isinstance(value, cls): return value if isinstance(value, CRS): return cls(_rebound(value), value.srs) return _cached(_normalize(value), database_identity())
[docs] @classmethod def from_persistable_reference(cls, payload: str) -> CoordinateReferenceSystem: """Resolve a CRS from an OSDU ``persistableReference`` payload. The payload carries its own definition, so the CRS is built from the ESRI WKT it states rather than from the authority code beside it. A payload that binds a transformation resolves to a bound CRS, which settles the datum shift that would otherwise be a choice. See :mod:`geodetic_engine.persistablereference`. :meth:`from_user_input` accepts these too, so a payload can be passed anywhere a CRS can. This is for when a caller wants the input read as a reference and nothing else. Args: payload: The payload, plain or URL-encoded JSON. Returns: The resolved CRS. Raises: UnresolvableCRSError: If the payload is not a readable persistableReference, states something other than a CRS, or states a definition this package will not translate. """ return _cached_reference(_normalize(payload), database_identity())
[docs] @classmethod def from_osdu_id(cls, value: str) -> CoordinateReferenceSystem: """Resolve a CRS from the id of the OSDU record that publishes it. The data partition and any version separator are dropped, and what remains is looked up exactly in the identifiers the active proj.db records. Those are written by :mod:`geodetic_engine.osdudb` from an OSDU catalogue, so the database has to have been built with ``geodetic-osdudb``. A bound CRS's id resolves to the bound CRS, transformation and all. The id is never decoded from its spelling and never matched approximately, so an id that differs from a recorded one by a single character is refused rather than resolved to a near miss. See :mod:`geodetic_engine.geodesy.osdu_id`. :meth:`from_user_input` recognises full ids too. This also accepts a bare ``data.ID`` such as ``"Projected:EPSG::23031"``, since it can only mean a CRS here. Args: value: The record id, with or without its partition, or the record's ``data.ID``. Returns: The resolved CRS. Raises: UnresolvableCRSError: If ``value`` is not an OSDU CRS record id, or the active proj.db records no such id. Example: >>> CoordinateReferenceSystem.from_osdu_id( ... "opendes:reference-data--CoordinateReferenceSystem:" ... "BoundProjected:EPSG::23031_EPSG::1133" ... ).crs.is_bound # doctest: +SKIP True """ osdu = ( parse_osdu_id(value, entity=CRS_ENTITY) if isinstance(value, str) else None ) if osdu is None: raise UnresolvableCRSError(f"{value!r} is not an OSDU record id") definition = value.strip() if parse_osdu_id(value) else osdu.identifier return _cached(definition, database_identity())
[docs] @classmethod def from_authority( cls, auth_name: str, code: str | int ) -> CoordinateReferenceSystem: """Resolve a CRS strictly by authority name and code. Unlike :meth:`from_user_input`, this never accepts WKT, PROJ strings, OSDU ids or persistableReferences: both tokens are validated before PROJ sees them so trailing text cannot turn an authority lookup into a PROJ string. Args: auth_name: Authority name, for example ``"EPSG"`` or ``"OGC"``. code: Code within that authority, for example ``4326`` or ``"CRS84"``. Returns: The resolved CRS. Raises: UnresolvableCRSError: If either token is malformed or PROJ cannot resolve the authority and code as a CRS. Example: >>> CoordinateReferenceSystem.from_authority("EPSG", 4326).authority_code 'EPSG:4326' """ if not isinstance(auth_name, str): raise UnresolvableCRSError( f"cannot resolve a CRS from authority {type(auth_name).__name__}; " "give an authority name string" ) authority = auth_name.strip() if not _AUTHORITY.match(authority): raise UnresolvableCRSError(f"{auth_name!r} is not a CRS authority") if isinstance(code, bool): raise UnresolvableCRSError(f"{code!r} is not a CRS code") if isinstance(code, int): try: code_text = str(code) except ValueError: raise UnresolvableCRSError( "cannot resolve a CRS from an integer too long to be a CRS code" ) from None elif isinstance(code, str): code_text = code.strip() else: raise UnresolvableCRSError( f"cannot resolve a CRS from code {type(code).__name__}; " "give a string or integer code" ) if not _CODE.match(code_text): raise UnresolvableCRSError(f"{code!r} is not a CRS code") return _cached_authority(authority, code_text, database_identity())
@property def crs(self) -> CRS: """The underlying :class:`pyproj.CRS`.""" return self._crs @property def definition(self) -> str: """The input this CRS was resolved from.""" return self._definition @property def name(self) -> str: """Human readable CRS name, for example ``"WGS 84"``.""" return self._crs.name @property def axes(self) -> tuple[AxisSpec, ...]: """The CRS's axes in EPSG-declared order, not in coordinate value order.""" return self._axes @property def axis_abbreviations(self) -> tuple[str, ...]: """Axis abbreviations in EPSG-declared order, for example ``("Lat", "Lon")``.""" return tuple(axis.abbrev for axis in self._axes) @property def axis_units(self) -> tuple[str, ...]: """Axis unit names in EPSG-declared order, ``("degree", "degree")`` for 4326.""" return tuple(axis.unit_name for axis in self._axes) @property def dimension(self) -> int: """Number of axes the CRS declares.""" return len(self._axes) @property def is_geographic(self) -> bool: """Whether this CRS expresses position as latitude and longitude. True through a bound CRS wrapping a geographic one, and through a compound CRS whose horizontal part is geographic. """ return bool(self._crs.is_geographic) @property def authority_code(self) -> str | None: """``"AUTH:CODE"`` if the CRS is identified in an authority, else None.""" authority = self._crs.to_authority() return None if authority is None else f"{authority[0]}:{authority[1]}"
[docs] def persistable_reference_from_catalog( self, *, database: Path | None = None ) -> CatalogPersistableReference | None: """The persistableReference an OSDU catalogue publishes for this CRS. Read from a proj.db built by :mod:`geodetic_engine.osdudb`, which records it verbatim against the id of the record that publishes it, for EPSG CRSs the base proj.db defines as much as for those the build adds. The CRS is looked up by the OSDU id or authority code it was resolved from, so a bound CRS named by its id gets the bound CRS's reference rather than that of the CRS it wraps. A CRS defined by WKT or PROJJSON is looked up by its authority code only when PROJ is certain of it. This is the catalogue's own string, to write into an OSDU record that names this CRS. To generate one from the definition instead, use :func:`~geodetic_engine.persistablereference.to_persistable_reference`. Args: database: The proj.db to read; the first on PROJ's data path by default. Returns: The reference, or None when none is recorded: see :func:`~geodetic_engine.persistablereference.missing_reference_reason`. Raises: AmbiguousCatalogReferenceError: If records publishing different references name this CRS's authority code. Example: >>> crs = CoordinateReferenceSystem.from_osdu_id( ... "BoundProjected:EPSG::23031_EPSG::1612" ... ) # doctest: +SKIP >>> crs.persistable_reference_from_catalog().kind # doctest: +SKIP <Kind.EARLY_BOUND_CRS: 'EBC'> """ return persistable_reference_from_catalog(self, database=database)
@property def is_dynamic(self) -> bool: """Whether any datum in this CRS is a dynamic reference frame. Coordinates in a dynamic frame require a coordinate epoch to be meaningful, so this drives the epoch requirement in :mod:`geodetic_engine.geodesy.transformation`. """ return _has_dynamic_frame(self._crs.to_json_dict()) @property def value_axis_order(self) -> tuple[int, ...]: """Indices of the declared axes, in coordinate value order. Element ``i`` is the position, among :attr:`axes`, of the axis whose value comes ``i``-th in the ``xy`` ordering this package uses. This is the bridge between what the EPSG dataset declares and the order values are actually in, and it is the thing a caller needs in order to label a coordinate correctly. Axes are identified by direction, falling back to the abbreviation for polar CRSs whose axes share a direction: ``EPSG:32661`` declares both of its axes pointing south, and only the ``N`` and ``E`` abbreviations distinguish them. Returns: A permutation of ``range(self.dimension)``. Example: >>> crs = CoordinateReferenceSystem.from_user_input("EPSG:4326") >>> crs.axis_abbreviations ('Lat', 'Lon') >>> crs.value_axis_order (1, 0) So the first coordinate value is the axis at declared index 1, longitude. """ return value_axis_order(self._axes) @property def value_axis_abbreviations(self) -> tuple[str, ...]: """Axis abbreviations in coordinate value order, ``("Lon", "Lat")`` for 4326.""" return tuple(self._axes[index].abbrev for index in self.value_axis_order) def __eq__(self, other: object) -> bool: if not isinstance(other, CoordinateReferenceSystem): return NotImplemented return bool(self._crs == other._crs) def __repr__(self) -> str: return f"CoordinateReferenceSystem({self.authority_code or self.name!r})"
def _normalize(value: Any) -> str: """Render user input as the cache key string PROJ will be given.""" if isinstance(value, int): try: return f"EPSG:{value}" except ValueError: # More digits than sys.get_int_max_str_digits(): no code is that long. raise UnresolvableCRSError( "cannot resolve a CRS from an integer too long to be an EPSG code" ) from None if isinstance(value, str): return value.strip() raise UnresolvableCRSError( f"cannot resolve a CRS from {type(value).__name__}; " "give an authority code, WKT, a PROJ string or a pyproj.CRS" ) @lru_cache(maxsize=256) def _cached(definition: str, identity: DatabaseIdentity) -> CoordinateReferenceSystem: """Resolve and cache a CRS by its textual definition.""" if (osdu := parse_osdu_id(definition)) is not None: # Resolved here, never by PROJ: PROJ would match an unknown id against # names approximately, and a near miss is a different object. Checked # before payloads, which a {{NAMESPACE}} placeholder would resemble. return CoordinateReferenceSystem(_osdu_crs(osdu, definition), definition) if looks_like_reference(definition): return _cached_reference(definition, identity) try: crs = CRS.from_user_input(definition) except CRSError as error: # A CRS the build deliberately left out is not an unknown name, and # PROJ cannot tell the difference. detail = skip_reason(definition) or str(error) raise UnresolvableCRSError( f"could not resolve {definition!r} as a CRS: {detail}" ) from error return CoordinateReferenceSystem(_rebound(crs), definition) @lru_cache(maxsize=256) def _cached_reference( definition: str, identity: DatabaseIdentity ) -> CoordinateReferenceSystem: """Resolve only persistableReferences, shared by both public constructors.""" return CoordinateReferenceSystem(_reference(definition), definition) @lru_cache(maxsize=256) def _cached_authority( auth_name: str, code: str, identity: DatabaseIdentity ) -> CoordinateReferenceSystem: """Resolve and cache a CRS strictly by validated authority name and code.""" definition = f"{auth_name}:{code}" try: crs = CRS.from_authority(auth_name, code) except CRSError as error: detail = skip_reason(definition) or str(error) raise UnresolvableCRSError( f"could not resolve {definition!r} as a CRS: {detail}" ) from error return CoordinateReferenceSystem(_rebound(crs, known=(auth_name, code)), definition) def _osdu_crs(osdu: OsduId, definition: str) -> CRS: """Resolve an OSDU record id through the identifiers proj.db records. Raises: UnresolvableCRSError: If the id names a transformation rather than a CRS, or names nothing in the active database, or names a CRS PROJ cannot construct. """ if not osdu.is_crs: raise UnresolvableCRSError( f"{definition!r} is the OSDU id of a {osdu.described}, not of a CRS" ) found = resolve_osdu_id(osdu) if found is None: raise UnresolvableCRSError( f"could not resolve {definition!r} as a CRS: {unresolved_reason(osdu)}" ) if not found.is_crs: raise UnresolvableCRSError( f"{definition!r} is recorded against {found.table_name} " f"{found.authority_code}, which is not a CRS" ) try: crs = CRS.from_authority(found.auth_name, found.code) except CRSError as error: raise UnresolvableCRSError( f"{definition!r} names {found.authority_code}, which PROJ could not " f"construct: {error}" ) from error return _rebound(crs, known=(found.auth_name, found.code)) def _reference(definition: str) -> CRS: """Read an OSDU persistableReference payload as a CRS. Raises: UnresolvableCRSError: If the payload cannot be read, or states something other than a CRS. Every way this fails is a statement about the input rather than about PROJ, so all of them are reported as one failure to resolve. """ try: reference = parse_persistable_reference(definition) if not isinstance(reference, CrsReference): stated = reference.kind.name.lower().replace("_", " ") raise UnresolvableCRSError( f"the persistableReference given states a {stated}, not a CRS" ) return reference.to_crs() except (PersistableReferenceError, UnembeddableOperationError) as error: raise UnresolvableCRSError( f"could not resolve a persistableReference as a CRS: {error}" ) from error def _rebound(crs: CRS, *, known: tuple[str, str] | None = None) -> CRS: """Restore the bound CRS the database defines, which PROJ unwraps. PROJ discards the ``BOUNDCRS`` wrapper when it builds a CRS from a code, keeping the binding for operation selection but not on the object. Reading the stored definition back gives an object that can say which operation it carries. See :mod:`geodetic_engine.geodesy.database`. The code is tried first, then the name. A bound CRS over another authority's base comes back identifying itself as that base, so its own code cannot be recovered from it, and the name is the only thing that survives the unwrapping. A caller that looked the CRS up by code already knows it, and passes it as ``known``. """ if crs.is_bound: return crs # An exact identification only: a fuzzy match could rebind a CRS onto a # different authority's bound definition. authority = known or crs.to_authority(min_confidence=100) definition = bound_definition(*authority) if authority else None described = f"{authority[0]}:{authority[1]}" if authority else crs.name if definition is None and crs.name: definition = bound_definition_by_name(crs.name) if definition is None: return crs try: rebound = CRS.from_wkt(definition) except CRSError as error: logger.warning( "%s is stored as a bound CRS that could not be read back: %s", described, error, ) return crs return rebound if rebound.is_bound else crs def _has_dynamic_frame(definition: object) -> bool: """Search a PROJJSON tree for a datum carrying a frame reference epoch.""" if isinstance(definition, dict): if _DYNAMIC_MARKER in definition: return True return any(_has_dynamic_frame(value) for value in definition.values()) if isinstance(definition, list): return any(_has_dynamic_frame(item) for item in definition) return False