Convert Degrees/Minutes/Seconds to Decimal Coordinates

Solution 1:

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);
}

Solution 2:

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;
}

Solution 3:

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.

Solution 4:

Often the western and southern hemispheres are expressed as negative degrees, and seconds contain decimals for accuracy: -86:44:52.892 Remember longitude is the X-coordinate and latitude is the Y-coordinate. This often gets mixed up because people often refer to them lat/lon and X/Y. I modified the code below for the above format.

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;
}

Solution 5:

CoordinateSharp is available as a Nuget package and can handle Coordinate conversions for you. It even does UTM/MGRS conversion and provides solar/lunar times relative to the input location. It's really easy to use!

Coordinate c = new Coordinate(40.465, -75.089);

//Display DMS Format
c.FormatOptions.Format = CoordinateFormatType.Degree_Minutes_Seconds;
c.ToString();//N 40º 27' 54" W 75º 5' 20.4"
c.Latitude.ToString();//N 40º 27' 54"
c.Latitude.ToDouble();//40.465

Coordinate properties are iObservable as as well. So if you change a latitude minute value for example, everything else will update.