【问题标题】:Problem converting fom latitude and longitude to position将纬度和经度转换为位置的问题
【发布时间】:2021-04-29 02:47:08
【问题描述】:

我希望能够从球体上的 3d 位置转换为纬度和经度并返回。我尝试使用以下方法转换为纬度和经度:

lat = acos(y / sphere_radius) * 180 / PI
long = atan2(x, z) * 180 / PI

而且它似乎有效。我得到的一些值是:

(0, 1, 0) -> (0, 0)
(1, 0, 0) -> (90, 90)
(0, 0, 1) -> (90, 0)
(0, 1, 1) -> (45, 0)

这意味着y 是我的向上,z 是我的前进方向,我的纬度是 0-180,我的经度是 0-360。但是现在我想将经纬度转换回位置向量,所以我在网上找到了这个sn-p(https://stackoverflow.com/a/59720312/13215204):

z = sphere_radius * cos(longitude) * cos(latitude)
x = -sphere_radius * cos(longitude) * sin(latitude)
y = sphere_radius * sin(latitude)

它确实返回了球体上的一个点,当我改变纬度和经度时,这个点甚至会移动,但位置不正确。例如,如果我首先将点(1, 0, 0) 转换为(90, 90),然后尝试将其转换回来,它会返回(-0.4005763, 0.8939967, 0.20077)。我听说您需要先转换回弧度,但这似乎没有帮助(它反而返回(4.371139e-08, 1, 1.910685e-15))。也许我只需要翻转一些正弦和余弦?

【问题讨论】:

  • 你得到的值确实是正确的(你只是弄乱了顺序)。您所需要的只是在某种程度上对这些值进行四舍五入。您也没有使用特定语言标记此问题。
  • 谢谢(尤其是舍入部分)。我已经以正确的顺序发布了我的答案,我只是无法将问题标记为已解决。尽管我使用的是 C#,但我也选择不包含语言标记,因为该语言并不是我问题的一部分。据我了解,标签用于向在某个主题方面有经验的人推荐问题(例如,如果我有关于 C# 的问题,然后可以向了解该语言的人展示)。但我的问题是关于数学部分的,所以包含 C# 或 Unity 标签似乎没有意义。还是我用错了标签?

标签: latitude-longitude


【解决方案1】:

现在可以了! 这些是我最终使用的方法(在 C# 中,Mathf 是 Unity 的一部分,但我相信您可以找到其他能够轻松执行 sin、cos、acos 和 atan 的库)

public static Vector2 ToLatLon(this Vector3 vector, float sphere_radius)
{
    return new Vector2(
        Mathf.Acos(vector.y / sphere_radius),
        Mathf.Atan2(vector.x, vector.z)
    );
}

public static Vector3 ToVector(this Vector2 latlon, float sphere_radius)
{
    return new Vector3(
         sphere_radius * Mathf.Sin(latlon.y) * Mathf.Sin(latlon.x),
         sphere_radius * Mathf.Cos(latlon.x),
         sphere_radius * Mathf.Cos(latlon.y) * Mathf.Sin(latlon.x)
    );
}

我的 x、y 和 z 方程的顺序错误,并且在计算 x 时还必须从 sphere_radius 中删除减号。我还删除了从弧度到纬度和经度的度数的转换,因为不需要它们(除了更容易阅读之外)。因此,当我需要以度为单位的值进行调试时,我改为使用 Mathf.Rad2Deg 转换为函数外的度数。

【讨论】:

    猜你喜欢
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    相关资源
    最近更新 更多