app.coordinates_manager module#
Coordinate management for seeding, retrieval, and central-point validation.
This module provides a focused set of utilities for initializing and validating the coordinate grid used by the weather pipeline. It inserts a single central coordinate and a deterministic ring of surrounding coordinates, and exposes read helpers that return Pydantic schemas. The goal is to keep persistence and validation responsibilities decoupled from input validation and orchestration logic in higher-level modules.
See Also#
app.coordinates_setupInput validation and high-level orchestration.
app.coordinates_utils.calculate_destination_pointGreat-circle destination computation used to derive surrounding points.
app.models.CoordinateSQLAlchemy ORM entity persisted by this module.
app.schemas.CoordinateSchemaPydantic schema returned by query helpers.
Notes#
Primary role: persist a central coordinate and a deterministic set of surrounding points and validate that the stored central coordinate matches the configured central values.
Key dependencies: a live SQLAlchemy Session, the ORM model app.models.Coordinate, and the Pydantic schema app.schemas.CoordinateSchema. Geodesic calculations are delegated to app.coordinates_utils.calculate_destination_point.
Invariants: callers own the Session lifecycle. Exactly one row is marked as the central coordinate (
is_central=True). Generated points are deterministic for the given bearings and distances.
Examples#
>>> # Typical usage within a request/worker context (requires a real DB)
>>> # from app.database import SessionLocal
>>> # session = SessionLocal()
>>> # seed_coordinates_if_needed(session, 59.3293, 18.0686)
>>> # coords = get_coordinates(session)
>>> # len(coords) > 0
True
- app.coordinates_manager.get_coordinates(session: sqlalchemy.orm.Session) List[CoordinateSchema][source]#
Retrieve all coordinates as Pydantic schemas.
- Parameters:
- session
sqlalchemy.orm.Session Open database session to run the query.
- session
- Returns:
list[app.schemas.CoordinateSchema]List of coordinates converted from ORM objects using
model_validate.
- Raises:
sqlalchemy.exc.SQLAlchemyErrorIf an underlying database error occurs during the query.
Examples
>>> # coords = get_coordinates(session) >>> # isinstance(coords, list) True
- app.coordinates_manager.seed_coordinates_if_needed(session: sqlalchemy.orm.Session, central_latitude: float, central_longitude: float) None[source]#
Seed the table or validate the persisted central coordinate.
If the table is empty, inserts the central point and a deterministic set of surrounding points. Otherwise validates that the stored central point is equal (within tolerance) to the configured central coordinate.
- Parameters:
- session
sqlalchemy.orm.Session Database session controlling the transaction.
- central_latitude
float Central latitude, typically provided by configuration.
- central_longitude
float Central longitude, typically provided by configuration.
- session
- Raises:
RuntimeErrorIf the central coordinate is missing or mismatches the provided values.
See also
app.coordinates_manager.get_coordinatesapp.coordinates_manager.set_coordinateapp.coordinates_utils.calculate_destination_point
Examples
>>> # Using a real DB session in application code >>> # seed_coordinates_if_needed(session, 59.3293, 18.0686) >>> # (no return value)
- app.coordinates_manager.set_coordinate(session: sqlalchemy.orm.Session, latitude: float, longitude: float, label: str | None = None, is_central: bool = False) None[source]#
Add a new coordinate to the table.
- Parameters:
- session
sqlalchemy.orm.Session Session used to persist the new coordinate.
- latitude
float Latitude in decimal degrees.
- longitude
float Longitude in decimal degrees.
- label
str|None,optional Optional label describing the coordinate.
- is_centralbool,
optional Whether the coordinate represents the central point (default
False).
- session
- Raises:
sqlalchemy.exc.SQLAlchemyErrorIf a database error occurs while persisting the new coordinate.
Notes
Commits immediately after inserting the object.