Skip to content

Commit

Permalink
Migrated distance_between() to leads.data. (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
ATATC committed Jan 5, 2025
1 parent d58c494 commit 702ee80
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
14 changes: 13 additions & 1 deletion leads/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from time import time as _time
from typing import override as _override, Any as _Any

from numpy import radians as _radians, degrees as _degrees, cos as _cos
from numpy import radians as _radians, degrees as _degrees, cos as _cos, sqrt as _sqrt


class Serializable(object):
Expand Down Expand Up @@ -161,6 +161,18 @@ def meters2dlon(meters: float, lat: float) -> float:
return _degrees(meters / 6378137 / _cos(_radians(lat)))


def distance_between(lat_0: float, lon_0: float, lat: float, lon: float) -> float:
"""
Calculate the distance between two locations on the Earth.
:param lat_0: the latitude of the first location
:param lon_0: the longitude of the first location
:param lat: the latitude of the second location
:param lon: the longitude of the second location
:return:
"""
return _sqrt(dlon2meters(lon - lon_0, .5 * (lat_0 + lat)) ** 2 + dlat2meters(lat - lat_0) ** 2)


def format_duration(duration: float) -> str:
"""
Wrap the duration into a formatted string.
Expand Down
4 changes: 3 additions & 1 deletion leads/data_persistence/analyzer/inference.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from abc import ABCMeta as _ABCMeta, abstractmethod as _abstractmethod
from typing import Any as _Any, override as _override, Generator as _Generator, Literal as _Literal

from leads.data import distance_between
from leads.data_persistence.analyzer.utils import time_invalid, speed_invalid, acceleration_invalid, \
mileage_invalid, latitude_invalid, longitude_invalid, distance_between
mileage_invalid, latitude_invalid, longitude_invalid
from leads.data_persistence.core import CSVDataset, DEFAULT_HEADER, VISUAL_HEADER_ONLY


Expand Down Expand Up @@ -239,6 +240,7 @@ class VisualDataRealignmentByLatency(Inference):
Offset the delay introduced by camera recording and video encoding so that the sensor data and the picture of the
same frame match.
"""

def __init__(self, *channels: _Literal["front", "left", "right", "rear"]) -> None:
super().__init__((0, 1), VISUAL_HEADER_ONLY)
self._channels: tuple[_Literal["front", "left", "right", "rear"], ...] = channels if channels else (
Expand Down
15 changes: 0 additions & 15 deletions leads/data_persistence/analyzer/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from typing import Any as _Any

from leads.data import dlat2meters, dlon2meters
from .._computational import sqrt as _sqrt


def time_invalid(o: _Any) -> bool:
return not isinstance(o, int)
Expand Down Expand Up @@ -30,15 +27,3 @@ def longitude_invalid(o: _Any) -> bool:

def latency_invalid(o: _Any) -> bool:
return not isinstance(o, int | float)


def distance_between(lat_0: float, lon_0: float, lat: float, lon: float) -> float:
"""
Calculate the distance between two locations on the Earth.
:param lat_0: the latitude of the first location
:param lon_0: the longitude of the first location
:param lat: the latitude of the second location
:param lon: the longitude of the second location
:return:
"""
return _sqrt(dlon2meters(lon - lon_0, .5 * (lat_0 + lat)) ** 2 + dlat2meters(lat - lat_0) ** 2)

0 comments on commit 702ee80

Please sign in to comment.