【问题标题】:Bearing calculation in C#C#中的方位计算
【发布时间】:2018-09-25 03:10:37
【问题描述】:

我想计算 2 个 GPS 位置之间的方位,我对我的算法提出了 this page 建议:

    public static double Bearing(IPointGps pt1, IPointGps pt2)
    {
        double x = Math.Cos(pt1.Latitude) * Math.Sin(pt2.Latitude) - Math.Sin(pt1.Latitude) * Math.Cos(pt2.Latitude) * Math.Cos(pt2.Longitude - pt1.Longitude);
        double y = Math.Sin(pt2.Longitude - pt1.Longitude) * Math.Cos(pt2.Latitude);

        // Math.Atan2 can return negative value, 0 <= output value < 2*PI expected 
        return (Math.Atan2(y, x) + Math.PI * 2)%(Math.PI * 2);
    }

然后我使用这种方法以度为单位转换我的值

    public static double RadiansToDegrees(double angle)
    {
        return (angle * 180.0) / Math.PI;
    }

我有以下测试样本:

  • Point1(纬度、经度)= 43.6373638888888888888888888888889, 1.35762222222222222222222222222222
  • Point2(纬度、经度)= 43.6156444444444444444444444444444,1.380225
  • 预期方位角 = 323°

但是,我获得了 315.5° (5.5062235835910762 rad) 的方位角。如果我计算预期的弧度值,我会得到 5.637413,这毫无疑问是我的问题在于我的方位方法。

我已经使用 .Net Math 包(包括 Cos、Sin、Tan 和 ATan 方法)实现了其他计算方法,并且我的单元测试以 1e-12 精度通过。我错过了什么?

PS:我还尝试重新实现 Atan2 方法,以防它缺乏精度。我得到了完全相同的结果

编辑:我的纬度和经度按照以下界面加倍

public interface IPointGps
{
    double Latitude { get; }
    double Longitude { get; }
}

【问题讨论】:

    标签: c# bearing gis


    【解决方案1】:

    Math.Sin() 和所有类似的方法都需要以弧度为单位的参数,但您的纬度和经度以度为单位。您必须在计算方位之前将 IPointGps 转换为弧度,或修改方位计算,例如:

    public static double Bearing(IPointGps pt1, IPointGps pt2)
    {
        double x = Math.Cos(DegreesToRadians(pt1.Latitude)) * Math.Sin(DegreesToRadians(pt2.Latitude)) - Math.Sin(DegreesToRadians(pt1.Latitude)) * Math.Cos(DegreesToRadians(pt2.Latitude)) * Math.Cos(DegreesToRadians(pt2.Longitude - pt1.Longitude));
        double y = Math.Sin(DegreesToRadians(pt2.Longitude - pt1.Longitude)) * Math.Cos(DegreesToRadians(pt2.Latitude));
    
        // Math.Atan2 can return negative value, 0 <= output value < 2*PI expected 
        return (Math.Atan2(y, x) + Math.PI * 2) % (Math.PI * 2);
    }
    
    public static double DegreesToRadians(double angle)
    {
        return angle * Math.PI / 180.0d;
    }
    

    返回轴承5.637716736134105

    【讨论】:

    • Daaaaaaaaaaaaaaaaaaaaaaaaaam!非常感谢!
    • 结果转换为真方位还是相对方位?
    【解决方案2】:

    看起来您的纬度和经度变量是浮点数(单精度)。如果是这种情况,那么您将面临精度错误。

    【讨论】:

    • 不,我从文件中读取这些值,但它们存储在双精度值中。
    猜你喜欢
    • 2011-10-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多