File: location_utils.py

package info (click to toggle)
python-aioambient 2024.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 472 kB
  • sloc: python: 755; sh: 41; makefile: 5
file content (49 lines) | stat: -rw-r--r-- 1,634 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Location utility functions."""

import math

# Radius of the Earth in miles
EARTH_RADIUS = 3959.0


class LocationUtils:  # pylint: disable=too-few-public-methods
    """Location utility functions."""

    @staticmethod
    def shift_location(
        latitude: float, longitude: float, latitude_delta: float, longitude_delta: float
    ) -> tuple[float, float]:
        """Calculates a new (latitude, longitude) pair by shifting the location
        given by (`latitude`, `longitude`) by (`latitude_delta`, `longitude_delta`).

        Args:
            latitude: Latitude (in degrees).
            longitude: Longitude (in degrees).
            latitude_delta: Latitude delta (in miles).
            longitude_delta: Longitude delta (in miles).

        Returns:
            New (latitude, longitude) pair.
        """

        # Convert latitude and longitude from degrees to radians
        latitude_rad = math.radians(latitude)
        longitude_rad = math.radians(longitude)

        # Calculate angular distance in radians
        angular_latitude_delta = latitude_delta / EARTH_RADIUS
        angular_longitude_delta = longitude_delta / EARTH_RADIUS

        # Calculate new latitude
        new_latitude_rad = latitude_rad + angular_latitude_delta

        # Calculate new longitude
        new_longitude_rad = longitude_rad + angular_longitude_delta / math.cos(
            latitude_rad
        )

        # Convert new latitude and longitude from radians to degrees
        new_latitude = math.degrees(new_latitude_rad)
        new_longitude = math.degrees(new_longitude_rad)

        return new_latitude, new_longitude