我有一个包含纬度和经度值的 CSV 文件,例如:
"25°36'55.57""E","45°39'12.52""N"
任何人都有一段快速简单的 C# 代码来将其转换为双精度值吗?
谢谢
如果您的意思是执行此操作的 C# 代码:
结果 = 25 + (36 / 60) + (55.57 / 3600)
首先,您需要使用 Regex 或其他机制解析表达式并将其拆分为各个部分。然后:
String hour = "25";
String minute = "36";
String second = "55.57";
Double result = (hour) + (minute) / 60 + (second) / 3600;
当然,根据 N/S 或 E/S 切换到翻转标志。维基百科对此有一点:
对于计算,西/东后缀在西半球被一个负号代替。令人困惑的是,有时也会看到否定东方的惯例。首选惯例——东方为正——与北极向上的右手笛卡尔坐标系一致。然后可以将特定的经度与特定的纬度(在北半球通常为正)结合起来,以给出地球表面上的精确位置。(http://en.wikipedia.org/wiki/Longitude)
感谢所有快速回答。根据 amdfan 的回答,我将这段代码放在一起,用 C# 完成这项工作。
/// <summary>The regular expression parser used to parse the lat/long</summary>
private static Regex Parser = new Regex("^(?<deg>[-+0-9]+)[^0-9]+(?<min>[0-9]+)[^0-9]+(?<sec>[0-9.,]+)[^0-9.,ENSW]+(?<pos>[ENSW]*)$");
/// <summary>Parses the lat lon value.</summary>
/// <param name="value">The value.</param>
/// <remarks>It must have at least 3 parts 'degrees' 'minutes' 'seconds'. If it
/// has E/W and N/S this is used to change the sign.</remarks>
/// <returns></returns>
public static double ParseLatLonValue(string value)
{
// If it starts and finishes with a quote, strip them off
if (value.StartsWith("\"") && value.EndsWith("\""))
{
value = value.Substring(1, value.Length - 2).Replace("\"\"", "\"");
}
// Now parse using the regex parser
Match match = Parser.Match(value);
if (!match.Success)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentUICulture, "Lat/long value of '{0}' is not recognised", value));
}
// Convert - adjust the sign if necessary
double deg = double.Parse(match.Groups["deg"].Value);
double min = double.Parse(match.Groups["min"].Value);
double sec = double.Parse(match.Groups["sec"].Value);
double result = deg + (min / 60) + (sec / 3600);
if (match.Groups["pos"].Success)
{
char ch = match.Groups["pos"].Value[0];
result = ((ch == 'S') || (ch == 'W')) ? -result : result;
}
return result;
}
你想用什么来表示它?弧秒?然后每度60分钟,每分钟60秒。然后,您必须自己保留 E 和 N。
不过,这不是通常的做法。
我见过的最简单的表示是在一个网格系统上绘制在地球上的一个点,它的原点通过地球的中心。[因此是一个很好的位置向量。]这个问题是虽然它很容易使用数据,正确地进出系统可能很困难,因为地球不是圆形的,或者就此而言是均匀的。