c#计算器来自NMEA日志文件的两个lat和long coords之间的轴承?

时间:2015-05-30 11:36:12

标签: c# gps

使用以下GPS日志摘录:

$GPGGA,153500.009,5137.2603,N,00244.8715,W,1,10,0.8,50.6,M,51.4,M,,0000*71
$GPRMC,153500.009,A,5137.2603,N,00244.8715,W,037.7,101.7,300912,,,A*74
$GPGGA,153500.059,5137.2601,N,00244.8706,W,1,10,0.8,50.6,M,51.4,M,,0000*74
$GPRMC,153500.059,A,5137.2601,N,00244.8706,W,038.0,101.8,300912,,,A*76
$GPGGA,153500.109,5137.2600,N,00244.8697,W,1,10,0.8,50.6,M,51.4,M,,0000*78
$GPRMC,153500.109,A,5137.2600,N,00244.8697,W,038.3,101.9,300912,,,A*78
$GPGGA,153500.159,5137.2599,N,00244.8688,W,1,10,0.8,50.5,M,51.4,M,,0000*73
$GPRMC,153500.159,A,5137.2599,N,00244.8688,W,038.6,101.9,300912,,,A*75
$GPGGA,153500.209,5137.2597,N,00244.8679,W,1,10,0.8,50.5,M,51.4,M,,0000*75
$GPRMC,153500.209,A,5137.2597,N,00244.8679,W,038.9,102.0,300912,,,A*76

我将记录的GPS轴承与最后一个和当前位置之间的计算轴承进行比较,并使用以下代码循环每行:

string[] splitline = line.Split(',');
course = Convert.ToDouble(splitline[8]);
Lat = Convert.ToDouble(splitline[3]);
Long = Convert.ToDouble(splitline[5]);

LatDeg = (Convert.ToInt16(Lat) / 100) + (Lat - (Convert.ToInt16(Lat) / 100) * 100) / 60;
LongDeg = (Convert.ToInt16(Long) / 100) + (Long - (Convert.ToInt16(Long) / 100) * 100) / 60;
lastLatDeg = (Convert.ToInt16(lastLat) / 100) + (lastLat - (Convert.ToInt16(lastLat) / 100) * 100) / 60;
lastLongDeg = (Convert.ToInt16(lastLong) / 100) + (lastLong - (Convert.ToInt16(lastLong) / 100) * 100) / 60;

var dLon = lastLongDeg - LongDeg;
var y = Math.Sin(dLon) * Math.Cos(lastLatDeg);
var x = Math.Cos(lastLatDeg) * Math.Sin(LatDeg) - Math.Sin(lastLatDeg) *           Math.Cos(LatDeg) * Math.Cos(dLon);
Console.WriteLine(DEG_PER_RAD * Math.Atan2(y, x));
Console.WriteLine("> " + course + " <");

lastLat = Lat;
lastLong = Long;
lastcourse = course;

导致以下结果:

136.131182151555
> 101.8 <
117.480364881602
> 101.9 <
117.480186101881
> 101.9 <
136.130309531745
> 102 <
117.479649572813
> 102 <

是我的计算结果,因为它们似乎都没有接近大约101度的gps记录轴承?

由于

1 个答案:

答案 0 :(得分:1)

我在代码中发现了一些问题,首先在解释纬度和经度时,你应该看看位置落入地球的哪个象限,并在南部或西部位置转换为负数:

Lat = Convert.ToDouble(splitline[3]);
if (splitline[4] == "S")
    Lat = 0.0 - Lat;
Long = Convert.ToDouble(splitline[5]);
if (splitline[6] == "W")
    Long = 0.0 - Long;

其余的问题源于将度数而不是弧度传递给数学函数,而经度delta的计算似乎是相反的。我介绍了一些辅助函数,并重写了以下代码段:

public static double DegreesToRadians(double degrees)
{
    return degrees * (Math.PI / 180);
}

public static double RadiansToDegrees(double radians)
{
    return radians * 180 / Math.PI;
}

double dLon = DegreesToRadians(LongDeg - lastLongDeg);
double y = Math.Sin(dLon) * Math.Cos(DegreesToRadians(lastLatDeg));
double x = Math.Cos(DegreesToRadians(lastLatDeg)) * Math.Sin(DegreesToRadians(LatDeg)) - Math.Sin(DegreesToRadians(lastLatDeg)) * Math.Cos(DegreesToRadians(LatDeg)) * Math.Cos(dLon);
Console.WriteLine((RadiansToDegrees(Math.Atan2(y, x)) + 360.0) % 360);
Console.WriteLine("> " + course + " <");

这给了我以下结果,你的测试数据忽略了第一个尚未确定轴承的无效测试数据:

109.693614586392
> 101.8 <
100.14641169874
> 101.9 <
100.146411372034
> 101.9 <
109.693611985053
> 102 <

我从GGA的速度中注意到,该装置似乎已经静止或移动得非常缓慢。一些GPS接收器将在这些情况下过滤或保持航向信息,因此可以预期一些变化。在更改之后,我浏览了一些来自移动车辆的GPS数据,结果在一个度数内。