C # calculater carrying between two lat and long coordinates from NMEA log file?

In the following example from the GPS log:

$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

      

I am comparing the registered GPS bearing with the calculated bearing between the last and current position with the following code that goes through each line:

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;

      

results in the following:

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

      

my calculations as none of them seem to come close to the gps value recorded around 101 degrees?

thank

+3


source to share


1 answer


There are a few problems I found in the code, to begin with, when you interpret latitude and longitude, you should look at which quadrant of the earth the positions fall into and turn negative for south or west locations:

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;

      

The rest of the problems were related to the transfer of powers, rather than radians, to mathematical functions, and the calculation of the delta longitude seemed to be the opposite. I introduced several helper functions and rewrote this section of code as follows:

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 + " <");

      



This gave me the following results with your test data, ignoring the first invalid one where the bearing is not yet defined:

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

      

I noticed with GGA speed that the device seemed to be stationary or moving very slowly. Some GPS receivers will filter or hold header information under these circumstances, so some changes can be expected. After the changes, I looked at some GPS data that I had from a moving vehicle and the results were within one degree of each other.

+1


source







All Articles