Coverage for pyguymer3/geo/calc_angle_between_two_locs.py: 100%
8 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
1#!/usr/bin/env python3
3# Define function ...
4def calc_angle_between_two_locs(
5 lon1_deg,
6 lat1_deg,
7 lon2_deg,
8 lat2_deg,
9 /,
10):
11 """Calculate the angle between two coordinates.
13 This function reads in two coordinates (in degrees) on the surface of a
14 sphere and calculates the angle (in degrees) between them, as viewed from
15 the centre of the sphere.
17 Parameters
18 ----------
19 lon1_deg : float
20 the longitude of the first coordinate (in degrees)
21 lat1_deg : float
22 the latitude of the first coordinate (in degrees)
23 lon2_deg : float
24 the longitude of the second coordinate (in degrees)
25 lat2_deg : float
26 the latitude of the second coordinate (in degrees)
28 Returns
29 -------
30 angle : float
31 the angle between the two coordinates (in degrees)
33 Notes
34 -----
35 Copyright 2017 Thomas Guymer [1]_
37 References
38 ----------
39 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
40 """
42 # Import standard modules ...
43 import math
45 # **************************************************************************
47 # Convert to radians ...
48 lon1_rad = math.radians(lon1_deg) # [rad]
49 lat1_rad = math.radians(lat1_deg) # [rad]
50 lon2_rad = math.radians(lon2_deg) # [rad]
51 lat2_rad = math.radians(lat2_deg) # [rad]
53 # Calculate angle in radians ...
54 distance_rad = 2.0 * math.asin(
55 math.hypot(
56 math.sin((lat1_rad - lat2_rad) / 2.0),
57 math.cos(lat1_rad) * math.cos(lat2_rad) * math.sin((lon1_rad - lon2_rad) / 2.0)
58 )
59 ) # [rad]
61 # Return angle ...
62 return math.degrees(distance_rad)