hyperion.domain.geo¶
hyperion.domain.geo
¶
Pure-domain geographical primitives.
Split out of :mod:hyperion.infrastructure.geo.location (F2 / DDD refactor
Step 3). :class:Location, :class:NamedLocation, :class:SpatialKMeans and
the distance/conversion helpers are pure data + math: they depend only on the
stdlib, haversine (lite core) and lite-core :mod:hyperion.log.
There is no cache reference and no singleton here -- the old
Location._cache class variable (which pulled :class:hyperion.ports.cache.Cache
and, via Cache.from_config(), boto3) is gone. get_distance simply
computes the haversine (or, when approximate, the equirectangular)
distance; callers that want memoisation wrap it themselves
(e.g. :func:functools.lru_cache).
numpy is imported lazily inside :class:SpatialKMeans, so this module
never requires numpy itself; only SpatialKMeans use does (it raises a
clear ImportError without it). haversine does its own optional
import numpy at load iff numpy is installed (a vector fast-path -- not a
declared haversine dependency) and falls back to a pure-math scalar path
when absent, which is the path :meth:Location.get_distance uses. numpy is
therefore safely gated under the [data] extra; the dependency move itself
lands in DDD refactor Step 10.
SpatialKMeans
¶
Bases: Generic[AnyLocation]
K-means clustering for geographical locations.
Initialize the K-means clustering with the given locations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
locations
|
Iterable[Location]
|
The locations to cluster. |
required |
Source code in hyperion/domain/geo.py
fit
¶
Fit the K-means model with k clusters and return the clusters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
The desired number of clusters (must be less than the number of locations). |
required |
max_iters
|
int
|
The maximum number of iterations. Defaults to 100. |
100
|
Returns:
| Type | Description |
|---|---|
dict[Location, list[AnyLocation]]
|
dict[Location, list[Location]]: The clusters with the centroids as keys. |
Source code in hyperion/domain/geo.py
Location
dataclass
¶
A geographical location.
get_distance
¶
Get the distance to another location in meters. If approximate is False (default), will use haversine. Otherwise uses Euclidean to approximate the distance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Location
|
The other location. |
required |
approximate
|
bool
|
Whether to approximate the distance. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The distance in meters. |
Source code in hyperion/domain/geo.py
get_nearest
¶
Get the closest location from the iterable of locations.
If threshold is given and all locations are further than the threshold in meters, an ValueError is raised.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
others
|
Iterable[Location]
|
The other locations. |
required |
threshold
|
float
|
The maximum distance in meters. Defaults to None. |
None
|
approximate
|
bool
|
Whether to approximate the distance. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Location |
AnyLocation
|
The nearest location. |
Source code in hyperion/domain/geo.py
meters_to_degrees
¶
Convert meters to degrees at a given latitude.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meters
|
float
|
The distance in meters. |
required |
at_latitude
|
float
|
The latitude at which the conversion should be done. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
tuple[float, float]: The distance in degrees for latitude and longitude. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |