Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Degrees/Minutes/Seconds to Decimal Coordinates

Tags:

c#

In one part of my code I convert from decimal coordinates to degrees/minutes/seconds and I use this:

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;

How would I convert back from degrees/minutes/seconds to decimal coordinates?

like image 764
Justin Avatar asked Jul 14 '10 19:07

Justin


People also ask

How do you write coordinates in decimal degrees?

Here are examples of formats that work: Decimal degrees (DD): 41.40338, 2.17403. Degrees, minutes, and seconds (DMS): 41°24'12.2"N 2°10'26.5"E. Degrees and decimal minutes (DMM): 41 24.2028, 2 10.4418.


3 Answers

Try this:

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);
}
like image 144
Byron Sommardahl Avatar answered Sep 19 '22 14:09

Byron Sommardahl


Just to save others time, I wanted to add on to Byron's answer. If you have the point in string form (e.g. "17.21.18S"), you can use this method:

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;
}
like image 43
Matt Cashatt Avatar answered Sep 18 '22 14:09

Matt Cashatt


Since degrees are each worth 1 coordinate total, and minutes are worth 1/60 of a coordinate total, and seconds are worth 1/3600 of a coordinate total, you should be able to put them back together with:

new_coord = deg + min/60 + sec/3600

Beware that it won't be the exact same as the original, though, due to floating-point rounding.

like image 31
eruciform Avatar answered Sep 19 '22 14:09

eruciform