在我的代码的一部分中,我将十进制坐标转换为度/分/秒,我使用它:
double coord = 59.345235;
int sec = (int)Math.Round(coord * 3600);
int deg = sec / 3600;
sec = Math.Abs(sec % 3600);
int min = sec / 60;
sec %= 60;
我如何从度/分/秒转换回小数点坐标?
答案 0 :(得分:37)
试试这个:
public double ConvertDegreeAngleToDouble( double degrees, double minutes, double seconds )
{
//Decimal degrees =
// whole number of degrees,
// plus minutes divided by 60,
// plus seconds divided by 3600
return degrees + (minutes/60) + (seconds/3600);
}
答案 1 :(得分:8)
为了节省他人的时间,我想补充一下拜伦的答案。如果您有字符串形式的点(例如" 17.21.18S"),您可以使用此方法:
public double ConvertDegreeAngleToDouble(string point)
{
//Example: 17.21.18S
var multiplier = (point.Contains("S") || point.Contains("W")) ? -1 : 1; //handle south and west
point = Regex.Replace(point, "[^0-9.]", ""); //remove the characters
var pointArray = point.Split('.'); //split the string.
//Decimal degrees =
// whole number of degrees,
// plus minutes divided by 60,
// plus seconds divided by 3600
var degrees = Double.Parse(pointArray[0]);
var minutes = Double.Parse(pointArray[1]) / 60;
var seconds = Double.Parse(pointArray[2]) / 3600;
return (degrees + minutes + seconds) * multiplier;
}
答案 2 :(得分:4)
由于度数总共1个坐标总数,分钟值为坐标总数的1/60,秒数值为坐标总数的1/3600,因此您应该能够将它们重新组合在一起:
new_coord = deg + min/60 + sec/3600
请注意,由于浮点舍入,它与原始版本不完全相同。
答案 3 :(得分:3)
西半球和南半球通常表示为负度,秒数包含精度的小数:-86:44:52.892记住经度是X坐标,纬度是Y坐标。这通常会混淆,因为人们经常将它们称为纬度/经度和X / Y.我修改了以下格式的代码。
private double ConvertDegreesToDecimal(string coordinate)
{
double decimalCoordinate;
string[] coordinateArray = coordinate.Split(':');
if (3 == coordinateArray.Length)
{
double degrees = Double.Parse(coordinateArray[0]);
double minutes = Double.Parse(coordinateArray[1]) / 60;
double seconds = Double.Parse(coordinateArray[2]) / 3600;
if (degrees > 0)
{
decimalCoordinate = (degrees + minutes + seconds);
}
else
{
decimalCoordinate = (degrees - minutes - seconds);
}
}
return decimalCoordinate;
}
答案 4 :(得分:0)
日期接受的答案不准确,并未考虑向正数添加负数时会发生什么。以下代码解决了问题并将正确转换。
public double ConvertDegreeAngleToDouble(double degrees, double minutes, double seconds)
{
var multiplier = (degrees < 0 ? -1 : 1);
var _deg = (double)Math.Abs(degrees);
var result = _deg + (minutes / 60) + (seconds / 3600);
return result * multiplier;
}
答案 5 :(得分:0)
CoordinateSharp可用作Nuget包,可以为您处理协调转换。它甚至可以进行UTM / MGRS转换,并提供相对于输入位置的太阳/月球时间。它真的很容易使用!
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="imageBox"></div>
<section>
<img src="https://upload.wikimedia.org/wikipedia/commons/7/70/Perspolis_FC.png" alt="Smiley face" height="42" width="42">
<img src="http://pngimg.com/uploads/manchester_united/manchester_united_PNG15.png" alt="Smiley face" height="42" width="42">
</section>
坐标属性也是iObservable。因此,如果您更改纬度分钟值,则其他所有内容都将更新。
答案 6 :(得分:0)
对于那些喜欢正则表达式并处理DDMMSS.dddS等格式的人 可以轻松更新此功能以处理其他格式。
C#
Regex reg = new Regex(@"^((?<D>\d{1,2}(\.\d+)?)(?<W>[SN])|(?<D>\d{2})(?<M>\d{2}(\.\d+)?)(?<W>[SN])|(?<D>\d{2})(?<M>\d{2})(?<S>\d{2}(\.\d+)?)(?<W>[SN])|(?<D>\d{1,3}(\.\d+)?)(?<W>[WE])|(?<D>\d{3})(?<M>\d{2}(\.\d+)?)(?<W>[WE])|(?<D>\d{3})(?<M>\d{2})(?<S>\d{2}(\.\d+)?)(?<W>[WE]))$");
private double DMS2Decimal(string dms)
{
double result = double.NaN;
var match = reg.Match(dms);
if (match.Success)
{
var degrees = double.Parse("0" + match.Groups["D"]);
var minutes = double.Parse("0" + match.Groups["M"]);
var seconds = double.Parse("0" + match.Groups["S"]);
var direction = match.Groups["W"].ToString();
var dec = (Math.Abs(degrees) + minutes / 60d + seconds / 3600d) * (direction == "S" || direction == "W" ? -1 : 1);
var absDec = Math.Abs(dec);
if ((((direction == "W" || direction == "E") && degrees <= 180 & absDec <= 180) || (degrees <= 90 && absDec <= 90)) && minutes < 60 && seconds < 60)
{
result = dec;
}
}
return result;
}