Source code for fsh_lib.geo

"""Wire representation of a geographic coordinate.

:class:`CoordinateSchema` is the wire form: a plain object
``{"latitude": "40.7", "longitude": "-74.0"}`` whose parts are
:data:`~fsh_lib.numeric.DecimalString` (exact :class:`~decimal.Decimal`
in Python, a precision-safe JSON *string* on the wire, ``string`` in
the generated TypeScript).  It mirrors the role
:data:`~fsh_lib.numeric.DecimalString` plays for a single decimal.

``from_attributes`` lets it be built straight off the database value
(``CoordinateSchema.model_validate(row.coordinates)``)::

    from decimal import Decimal

    from codegen_database.types import Coordinate
    from fsh_lib.geo import CoordinateSchema

    c = Coordinate(latitude=Decimal("40.7"), longitude=Decimal("-74.0"))
    CoordinateSchema.model_validate(c).model_dump(mode="json")
    # -> {"latitude": "40.7", "longitude": "-74.0"}
"""

from __future__ import annotations

from pydantic import BaseModel, ConfigDict

from fsh_lib.numeric import DecimalString  # noqa: TC001 -- runtime annotation


[docs] class CoordinateSchema(BaseModel): """A geographic coordinate as a precision-safe wire object. ``latitude`` / ``longitude`` are :data:`~fsh_lib.numeric.DecimalString` -- exact decimals that cross the wire as strings. ``from_attributes`` allows construction from the ``codegen_database.types.Coordinate`` frozen dataclass (which exposes the same two attribute names). """ model_config = ConfigDict(from_attributes=True) latitude: DecimalString longitude: DecimalString
__all__ = ["CoordinateSchema"]