D to C# conversion of GPS Data
Tom via Digitalmars-d
digitalmars-d at puremagic.com
Mon May 8 18:59:07 PDT 2017
Does anyone know how to convert this D code to C# ?
string[] parse_gps_data(string data, SysTime start_time, SysTime
end_time) {
// Validate input
string[] gps;
if (!data.length) {
error("Empty GPS file");
return gps;
}
auto start_timestamp = start_time.toUnixTime();
auto end_timestamp = end_time.toUnixTime();
// Parse every line
foreach (line; splitLines(data)) {
// Detect type of line
auto match = matchFirst(line, GPS_LINE);
if (!match) {
// Unknown format
error("Unknown GPS line: " ~ line);
continue;
}
// Parse time
auto record_time = SysTime.fromISOExtString(match[1].replace("
", "T"));
auto record_timestamp = record_time.toUnixTime();
if (record_timestamp < start_timestamp || record_timestamp >
end_timestamp) {
// Skip invalid time interval
error(format(
"Invalid GPS interval: %d > %d || %d < %d\nLine: %s",
record_timestamp, start_timestamp, record_timestamp,
end_timestamp,
line
));
continue;
}
// Parse coordinates
float longitude;
float latitude;
if (to!char(match[2]) == 'N') {
latitude = to!float(match[3]);
if (to!char(match[4]) == 'W') {
longitude = to!float(match[5]) * -1;
}
else {
longitude = to!float(match[5]);
}
}
else if (to!char(match[2]) == 'W') {
longitude = to!float(match[3]) * -1;
latitude = to!float(match[5]);
}
else if (to!char(match[2]) == 'E') {
longitude = to!float(match[3]);
latitude = to!float(match[5]);
}
// Prepare gps timestamp
auto record_interval = (record_time -
start_time).split!("hours", "minutes", "seconds")();
gps ~= format(
"('%02d:%02d:%02d', '(%f, %f)'::point)::time_point",
record_interval.hours, record_interval.minutes,
record_interval.seconds,
longitude, latitude
);
}
return gps;
}
I would appreciate if anyone who has both D and C# experience.
So far I have this :
/// <summary>
///
/// </summary>
/// <param name="deviceData"></param>
/// <param name="device"></param>
/// <returns></returns>
public string[] ParseGPSData (string deviceData, DateTime
startTime, DateTime endTime)
{
string[] gps = null;
if (deviceData == string.Empty)
return gps;
var startTimeStamp = startTime.ToUniversalTime ();
var endTimeStamp = endTime.ToUniversalTime ();
foreach (var line in Regex.Split(deviceData, "\r\n
|\r |\n"))
{
var match = _rx.Match(line);
if (!match.Success)
continue;
var recordTime = match.Groups[1].Value.Replace ('
', 'T');
}
return gps;
}
}
The constructor for GPS class has this defined :
public class GPSEngine
{
private Regex _rx;
/// <summary>
///
/// </summary>
public GPSEngine ()
{
_rx = new Regex
(@"^(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})\s+(N|W|E)(\d+\.\d+)\s+(N|W|E)(\d+\.\d+)",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
More information about the Digitalmars-d
mailing list