【问题标题】:Converting from Decimal degrees to Degrees Minutes Seconds tenths.从十进制度转换为度分秒十分之一。
【发布时间】:2011-07-28 16:54:35
【问题描述】:

在 C# 中是否有一些示例转换代码可以从十进制度到度、分、秒、十度?

【问题讨论】:

标签: c# latitude-longitude


【解决方案1】:

这是我前段时间做的一堂课。

public class SexagesimalAngle
{
    public bool IsNegative { get; set; }
    public int Degrees { get; set; }
    public int Minutes { get; set; }
    public int Seconds { get; set; }
    public int Milliseconds { get; set; }



    public static SexagesimalAngle FromDouble(double angleInDegrees)
    {
        //ensure the value will fall within the primary range [-180.0..+180.0]
        while (angleInDegrees < -180.0)
            angleInDegrees += 360.0;

        while (angleInDegrees > 180.0)
            angleInDegrees -= 360.0;

        var result = new SexagesimalAngle();

        //switch the value to positive
        result.IsNegative = angleInDegrees < 0;
        angleInDegrees = Math.Abs(angleInDegrees);

        //gets the degree
        result.Degrees = (int)Math.Floor(angleInDegrees);
        var delta = angleInDegrees - result.Degrees;

        //gets minutes and seconds
        var seconds = (int)Math.Floor(3600.0 * delta);
        result.Seconds = seconds % 60;
        result.Minutes = (int)Math.Floor(seconds / 60.0);
        delta = delta * 3600.0 - seconds;

        //gets fractions
        result.Milliseconds = (int)(1000.0 * delta);

        return result;
    }



    public override string ToString()
    {
        var degrees = this.IsNegative
            ? -this.Degrees
            : this.Degrees;

        return string.Format(
            "{0}° {1:00}' {2:00}\"",
            degrees,
            this.Minutes,
            this.Seconds);
    }



    public string ToString(string format)
    {
        switch (format)
        {
            case "NS":
                return string.Format(
                    "{0}° {1:00}' {2:00}\".{3:000} {4}",
                    this.Degrees,
                    this.Minutes,
                    this.Seconds,
                    this.Milliseconds,
                    this.IsNegative ? 'S' : 'N');

            case "WE":
                return string.Format(
                    "{0}° {1:00}' {2:00}\".{3:000} {4}",
                    this.Degrees,
                    this.Minutes,
                    this.Seconds,
                    this.Milliseconds,
                    this.IsNegative ? 'W' : 'E');

            default:
                throw new NotImplementedException();
        }
    }
}

【讨论】:

  • 到目前为止,这是最好的答案。
  • 这很好,但我经常将秒视为浮点值,这会阻塞
  • 有趣的解决方案。由于 DMS 表示法可用于其他地方(例如天体坐标),因此可能需要考虑一个更广泛的名称,例如 SexagesimalAngle,根据 Wikipedia,这就是该系统的命名方式。
【解决方案2】:

我认为应该这样做。

double decimal_degrees; 

// set decimal_degrees value here

double minutes = (decimal_degrees - Math.Floor(decimal_degrees)) * 60.0; 
double seconds = (minutes - Math.Floor(minutes)) * 60.0;
double tenths = (seconds - Math.Floor(seconds)) * 10.0;
// get rid of fractional part
minutes = Math.Floor(minutes);
seconds = Math.Floor(seconds);
tenths = Math.Floor(tenths);

【讨论】:

    【解决方案3】:

    here的解决方案有双向转换。

    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;
    

    **来自Convert Degrees/Minutes/Seconds to Decimal Coordinates

    【讨论】:

      【解决方案4】:

      您可以简单地使用这两个功能:

      public Tuple<int,int,int> DecimalToDegrees(decimal decimalValue)
      {
       return Tuple.Create(Convert.ToInt32( decimal.Truncate(decimalValue)),Convert.ToInt32( (decimal.Truncate(Math.Abs(decimalValue)*60))%60),Convert.ToInt32( (Math.Abs(decimalValue)*3600)%60));
      }
      

      还有:

      public decimal DecimalToDegrees(int deg , int min , int sec)
      { //~2.3825224324453 Meters error due to accuracy
        return deg+(min/60m)+(sec/3600m);
      }
      

      【讨论】:

      • 第一次知道 .net 中存在 Tuple 之类的东西,谢谢
      【解决方案5】:

      位置的字符串表示,即51°09'48.2"N 10°07'28.6"E

      public static string ToDMS(this Location location)
      {
          var (lat, lon) = (location.Latitude, location.Longitude);
          var latSec = Math.Abs(lat) % 1.0 * 3600.0;
          var lonSec = Math.Abs(lon) % 1.0 * 3600.0;
          return FormattableString.Invariant(
            $@"{Math.Abs((int)lat)}°{(int)latSec / 60}'{latSec % 60:F1}\"{(lat >= 0 ? "N" : "S")
             } {Math.Abs((int)lon)}°{(int)lonSec / 60}'{lonSec % 60:F1}\"{(lon >= 0 ? "E" : "W")}");
      }
      

      你可能不喜欢这个单行:)

      public static string ToDMS(double lat, double lon) => FormattableString.Invariant($"{Math.Abs((int)lat)}°{(int)(Math.Abs(lat) % 1.0 * 60.0)}'{Math.Abs(lat) * 3600.0 % 60:F1}\"{(lat >= 0 ? "N" : "S")} {Math.Abs((int)lon)}°{(int)(Math.Abs(lon) % 1.0 * 60.0)}'{Math.Abs(lon) * 3600.0 % 60:F1}\"{(lon >= 0 ? "E" : "W")}" );
      

      欢迎提出任何改进建议...

      【讨论】:

        猜你喜欢
        • 2020-02-10
        • 1970-01-01
        • 2013-03-23
        • 2013-10-27
        • 1970-01-01
        • 2021-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多