Python code for calculating the angle between three points (long lat coordinates)

Can someone suggest how to calculate the angle between three points (long lat coordinates)

A : (12.92473, 77.6183)
B : (12.92512, 77.61923)
C : (12.92541, 77.61985)
+4
source share
2 answers

, , ABC (B - ). ( 0,0007 ° 0,002 ° ), . - , , . , . .

. numpy , . , , .

import numpy as np
import math

def latlong_to_3d(latr, lonr):
    """Convert a point given latitude and longitude in radians to
    3-dimensional space, assuming a sphere radius of one."""
    return np.array((
        math.cos(latr) * math.cos(lonr),
        math.cos(latr) * math.sin(lonr),
        math.sin(latr)
    ))

def angle_between_vectors_degrees(u, v):
    """Return the angle between two vectors in any dimension space,
    in degrees."""
    return np.degrees(
        math.acos(np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v))))

# The points in tuple latitude/longitude degrees space
A = (12.92473, 77.6183)
B = (12.92512, 77.61923)
C = (12.92541, 77.61985)

# Convert the points to numpy latitude/longitude radians space
a = np.radians(np.array(A))
b = np.radians(np.array(B))
c = np.radians(np.array(C))

# Vectors in latitude/longitude space
avec = a - b
cvec = c - b

# Adjust vectors for changed longitude scale at given latitude into 2D space
lat = b[0]
avec[1] *= math.cos(lat)
cvec[1] *= math.cos(lat)

# Find the angle between the vectors in 2D space
angle2deg = angle_between_vectors_degrees(avec, cvec)


# The points in 3D space
a3 = latlong_to_3d(*a)
b3 = latlong_to_3d(*b)
c3 = latlong_to_3d(*c)

# Vectors in 3D space
a3vec = a3 - b3
c3vec = c3 - b3

# Find the angle between the vectors in 2D space
angle3deg = angle_between_vectors_degrees(a3vec, c3vec)


# Print the results
print('\nThe angle ABC in 2D space in degrees:', angle2deg)
print('\nThe angle ABC in 3D space in degrees:', angle3deg)

The angle ABC in 2D space in degrees: 177.64369006

The angle ABC in 3D space in degrees: 177.643487338

, ( ), , .

+1

lat/lon, :

Formula:    
θ = atan2( sin Δλ ⋅ cos φ2 , cos φ1sin φ2sin φ1cos φ2cos Δλ )
where   
φ11 is the start point, φ22 the end point (Δλ is the difference in longitude)

JavaScript:
(all angles in radians) 
var y = Math.sin21) * Math.cos2);
var x = Math.cos1)*Math.sin2) -
        Math.sin1)*Math.cos2)*Math.cos21);
var brng = Math.atan2(y, x).toDegrees();
0

Source: https://habr.com/ru/post/1671382/


All Articles