【问题标题】:Python code to calculate angle between three points (lat long coordinates)Python代码计算三点之间的角度(纬度坐标)
【发布时间】:2017-03-03 16:43:52
【问题描述】:

谁能建议如何计算三点之间的角度(经纬度坐标)

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

【问题讨论】:

  • 这也适用于 lat long 吗?
  • 这可能不是一个精确的解决方案,但您的观点似乎足够接近。在这种情况下,我们可以将地球的球面近似为一个平面。
  • 不能将这些值用作平面点并获取角度。一方面,地球并不平坦,尽管它在小范围内大致是平坦的。更重要的是,经度和纬度的单位不相同。在赤道,纬度或经度是相同的。但是远离赤道,一个经度比纬度覆盖的距离更小。在(非常接近)北极或南极,一个经度覆盖 距离,而一个纬度仍然约为 69 英里。所以这个问题不是那个问题的重复
  • 但这是真实的情况吗?如果您需要真正的导航,您可以在 15 分钟内建立自己的 OSRM 实例,它会在 1 或 2 毫秒内为您提供转弯方向(步行或驾车),以实现巨大的旅程,没有任何限制且免费. Graphhopper 类似。如果您说的是实际道路,那么您目前的方法没有希望(我的全职工作是车辆路线问题)。然后你只需要requests 与你的服务器通信。

标签: python math latitude-longitude angle


【解决方案1】:

假设您想要角度 ABC(B 是角度的顶点),我看到了解决您的问题的两种主要方法。由于您的三个点彼此靠近(小于 0.0007° 纬度和 0.002° 经度),我们可以将地球近似为一个平面并使用二维矢量计算。当我们远离赤道时,经度和纬度的距​​离并不相同,但我们可以对此进行调整。另一种解决方案是将您的点视为三维空间并使用三维矢量计算。这里我们只需要将给定的球坐标转换为 3D 笛卡尔坐标即可。

这是我解决您问题的代码。为了方便起见,我在这里使用了 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

请注意,结果非常接近(相差约千分之一度),正如预期的那样,三个点如此接近。

【讨论】:

    【解决方案2】:

    要获得纬度/经度系统中两个方向之间的角度,可以使用两个轴承的差from this page

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 2015-10-04
      • 2016-02-03
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      相关资源
      最近更新 更多