简体   繁体   English

Python代码计算三点之间的角度(lat长坐标)

[英]Python code to calculate angle between three points (lat long coordinates)

Can anybody suggest how to calculate angle between three points (lat long coordinates) 任何人都可以建议如何计算三点之间的角度(纬度长坐标)

A : (12.92473, 77.6183)
B : (12.92512, 77.61923)
C : (12.92541, 77.61985)

I see two main ways to solve your problem, assuming you want angle ABC (B is the vertex of the angle). 假设您需要角度ABC(B是角度的顶点),我会看到两种解决问题的主要方法。 Since your three points are close to each other (less than 0.0007° latitude and 0.002° longitude apart), we can approximate the earth as a plane and use two-dimensional vector calculations. 由于您的三个点彼此接近(纬度小于0.0007°,经度相差0.002°),我们可以将地球近似为一个平面并使用二维矢量计算。 A degree of longitude and of latitude are not the same distance when we are away from the equator, but we can adjust for that. 当我们离开赤道时,经度和纬度不是相同的距离,但我们可以对此进行调整。 Another solution is to treat your points as in three-dimensional space and use three-dimensional vector calculations. 另一种解决方案是将您的点视为三维空间并使用三维矢量计算。 Here we just need to convert the given spherical coordinates to 3D Cartesian coordinates. 这里我们只需要将给定的球面坐标转换为3D笛卡尔坐标。

Here is my code for your problem. 这是我的问题代码。 I use the numpy module here for convenience, but this can be done pretty easily without it. 为方便起见,我在这里使用了numpy模块,但如果没有它,这可以很容易地完成。 This code is fairly wordy so you can see better just what is being done. 这段代码非常冗长,因此您可以更好地了解正在完成的工作。

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)

This gives the results 这给出了结果

The angle ABC in 2D space in degrees: 177.64369006

The angle ABC in 3D space in degrees: 177.643487338

Note that the results are very close (off by about one five-thousandth of a degree), as expected for three points so close together. 请注意,结果非常接近(偏离大约五分之一度),正如预期的那样,三个点非常接近。

To get angle between two directions in lat/lon system, you can use difference of two bearings from this page : 要在lat / lon系统中获得两个方向之间的角度,您可以使用此页面中两个轴承的差异:

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

JavaScript:
(all angles in radians) 
var y = Math.sin(λ2-λ1) * Math.cos(φ2);
var x = Math.cos(φ1)*Math.sin(φ2) -
        Math.sin(φ1)*Math.cos(φ2)*Math.cos(λ2-λ1);
var brng = Math.atan2(y, x).toDegrees();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM