C# 将纬度和经度转换为双精度值的最简单方法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/103006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the easiest way to convert latitude and longitude to double values
提问by Nick Randell
I've got a CSV file containing latitude and longitude values, such as:
我有一个包含纬度和经度值的 CSV 文件,例如:
"25°36'55.57""E","45°39'12.52""N"
"25°36'55.57""E","45°39'12.52""N"
Anyone have a quick and simple piece of C# code to convert this to double values?
任何人都有一段快速简单的 C# 代码来将其转换为双精度值?
Thanks
谢谢
采纳答案by ine
If you mean C# code to do this:
如果您的意思是 C# 代码来执行此操作:
result = 25 + (36 / 60) + (55.57 / 3600)
结果 = 25 + (36 / 60) + (55.57 / 3600)
First you'll need to parse the expression with Regex or some other mechanism and split it into the individual parts. Then:
首先,您需要使用 Regex 或其他一些机制解析表达式并将其拆分为各个部分。然后:
String hour = "25";
String minute = "36";
String second = "55.57";
Double result = (hour) + (minute) / 60 + (second) / 3600;
And of course a switch to flip sign depending on N/S or E/S. Wikipedia has a little on that:
当然,根据 N/S 或 E/S 切换到翻转标志。维基百科对此有一些说明:
For calculations, the West/East suffix is replaced by a negative sign in the western hemisphere. Confusingly, the convention of negative for East is also sometimes seen. The preferred convention -- that East be positive -- is consistent with a right-handed Cartesian coordinate system with the North Pole up. A specific longitude may then be combined with a specific latitude (usually positive in the northern hemisphere) to give a precise position on the Earth's surface. (http://en.wikipedia.org/wiki/Longitude)
对于计算,西/东后缀在西半球被负号代替。令人困惑的是,有时也会看到对东方不利的惯例。首选的约定——东为正值——与北极在上的右手笛卡尔坐标系一致。然后可以将特定经度与特定纬度(通常在北半球为正值)结合起来,以给出地球表面上的精确位置。( http://en.wikipedia.org/wiki/Longitude)
回答by Dan Blair
What are you wanting to represent it as? Arc seconds? Then 60 min in every degree, 60 seconds in every minute. You would then have to keep E and N by yourself.
你想把它表示成什么?弧秒?然后每度60分钟,每分钟60秒。然后,您必须自己保留 E 和 N。
This is not how it's done generally though.
虽然这不是一般的做法。
The easiest representation I've seen to work with is a point plotted on the globe on a grid system that has its origin through the center of the earth.[Thus a nice position vector.] The problem with this is that while it's easy to use the data, getting it into and out of the system correctly can be tough, because the earth is not round, or for that matter uniform.
我见过的最简单的表示方法是绘制在地球上的一个点,网格系统的原点通过地球的中心。[因此是一个很好的位置向量。] 问题是,虽然它很容易使用数据,将其正确地进出系统可能很困难,因为地球不是圆的,或者就此而言是均匀的。
回答by Nick Randell
Thanks for all the quick answers. Based on the answer by amdfan, I put this code together that does the job in C#.
感谢所有快速回答。根据 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;
}