Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting from Decimal degrees to Degrees Minutes Seconds tenths.

Is there some sample conversion code in C# to go from decimal degrees to Degrees, Minutes, Seconds, Tenth?

like image 691
DoubleDunk Avatar asked Jul 28 '11 16:07

DoubleDunk


People also ask

How do you convert degrees to degrees minutes seconds?

The decimal part of a degree is first multiplied by 60 to get the measure in minutes. Then, the decimal part of the minutes is multiplied by 60 to get the measure in seconds.

How did you write the angle in degrees minutes and seconds to decimal degrees?

Step 1: Convert the minutes to degrees by dividing by 60. Step 2: Convert the seconds to degrees by dividing by 3,600. Step 3: Find the decimal degree by adding the degrees to the results from steps 1 and 2.


1 Answers

Here is a class I made time ago.

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();
        }
    }
}
like image 191
Mario Vernari Avatar answered Oct 18 '22 15:10

Mario Vernari