mirror of
https://github.com/dotMorten/NmeaParser.git
synced 2025-12-06 07:12:04 +01:00
Refactor lat/long parsing
This commit is contained in:
parent
189b10d376
commit
9663da9cf2
|
|
@ -45,12 +45,8 @@ namespace NmeaParser.Nmea.Gps
|
|||
protected override void LoadMessage(string[] message)
|
||||
{
|
||||
var time = message[0];
|
||||
Latitude = int.Parse(message[1].Substring(0, 2), CultureInfo.InvariantCulture) + double.Parse(message[1].Substring(2), CultureInfo.InvariantCulture) / 60;
|
||||
if (message[2] == "S")
|
||||
Latitude *= -1;
|
||||
Longitude = int.Parse(message[3].Substring(0, 3), CultureInfo.InvariantCulture) + double.Parse(message[3].Substring(3), CultureInfo.InvariantCulture) / 60;
|
||||
if (message[4] == "W")
|
||||
Longitude *= -1;
|
||||
Latitude = NmeaMessage.StringToLatitude(message[1], message[2]);
|
||||
Longitude = NmeaMessage.StringToLongitude(message[3], message[4]);
|
||||
Quality = (FixQuality)int.Parse(message[5], CultureInfo.InvariantCulture);
|
||||
NumberOfSatellites = int.Parse(message[6], CultureInfo.InvariantCulture);
|
||||
Hdop = double.Parse(message[7], CultureInfo.InvariantCulture);
|
||||
|
|
|
|||
|
|
@ -38,12 +38,8 @@ namespace NmeaParser.Nmea.Gps
|
|||
int.Parse(message[0].Substring(2, 2)),
|
||||
int.Parse(message[0].Substring(4, 2)), DateTimeKind.Utc);
|
||||
Active = (message[1] == "A");
|
||||
Latitude = int.Parse(message[2].Substring(0, 2), CultureInfo.InvariantCulture) + double.Parse(message[2].Substring(2), CultureInfo.InvariantCulture) / 60;
|
||||
if (message[3] == "S")
|
||||
Latitude *= -1;
|
||||
Longitude = int.Parse(message[4].Substring(0, 3), CultureInfo.InvariantCulture) + double.Parse(message[4].Substring(3), CultureInfo.InvariantCulture) / 60;
|
||||
if (message[5] == "W")
|
||||
Longitude *= -1;
|
||||
Latitude = NmeaMessage.StringToLatitude(message[2], message[3]);
|
||||
Longitude = NmeaMessage.StringToLongitude(message[4], message[5]);
|
||||
Speed = double.Parse(message[6], CultureInfo.InvariantCulture);
|
||||
Course = double.Parse(message[7], CultureInfo.InvariantCulture);
|
||||
MagneticVariation = double.Parse(message[9], CultureInfo.InvariantCulture);
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
|
@ -109,5 +110,28 @@ namespace NmeaParser.Nmea
|
|||
{
|
||||
return string.Format("${0},{1}", MessageType, string.Join(",", MessageParts));
|
||||
}
|
||||
|
||||
internal static double StringToLatitude(string value, string ns)
|
||||
{
|
||||
try
|
||||
{
|
||||
double latitude = int.Parse(value.Substring(0, 2), CultureInfo.InvariantCulture) + double.Parse(value.Substring(2), CultureInfo.InvariantCulture) / 60;
|
||||
if (ns == "S")
|
||||
latitude *= -1;
|
||||
return latitude;
|
||||
}
|
||||
catch { return double.NaN; }
|
||||
}
|
||||
internal static double StringToLongitude(string value, string ew)
|
||||
{
|
||||
try
|
||||
{
|
||||
double longitude = int.Parse(value.Substring(0, 3), CultureInfo.InvariantCulture) + double.Parse(value.Substring(3), CultureInfo.InvariantCulture) / 60;
|
||||
if (ew == "W")
|
||||
longitude *= -1;
|
||||
return longitude;
|
||||
}
|
||||
catch { return double.NaN; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue