Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert lat long from decimal degrees to DMS format?

Tags:

javascript

Assuming I have a latitude longitude: 38.898556, -77.037852. How do I convert this to DMS?

Expected output is:

38 53 55 N
77 2 16 W

Want to be able to accept both a latitude and longitude as input parameters in the function.

Current function is as follows:

function convertDMS( lat, lng ) {

        var convertLat = Math.abs(lat);
        var LatDeg = Math.floor(convertLat);
        var LatMin = (Math.floor((convertLat - LatDeg) * 60));
        var LatCardinal = ((lat > 0) ? "n" : "s");

        var convertLng = Math.abs(lng);
        var LngDeg = Math.floor(convertLng);
        var LngMin = (Math.floor((convertLng - LngDeg) * 60));
        var LngCardinal = ((lng > 0) ? "e" : "w");

        return LatDeg + LatCardinal + LatMin  + "    " + LngDeg + LngCardinal + LngMin;
}
like image 750
Rolando Avatar asked Jun 18 '16 04:06

Rolando


People also ask

How do you convert decimal degrees to DMS in Excel?

Just change the text in brackets ([Lat/Long Cell]) to point to the DD cell you want to convert to DMS. If you want a different number of decimal places for the seconds add the desired number of 0's after the decimal point in the "00.00" part and also set the "2" to the number of places.

What is the difference between decimal degrees and DMS?

The key for knowing the difference is watching for the presence of a decimal in the coordinate string. If there is no decimal, it is DMS. If the decimal imme- diately follows the minutes coordinate (61° 34.25' or 61 34.25) then it's DM. If the decimal immediately follows the degrees coordinate (61.5708) then it's DD.


2 Answers

function toDegreesMinutesAndSeconds(coordinate) {
    var absolute = Math.abs(coordinate);
    var degrees = Math.floor(absolute);
    var minutesNotTruncated = (absolute - degrees) * 60;
    var minutes = Math.floor(minutesNotTruncated);
    var seconds = Math.floor((minutesNotTruncated - minutes) * 60);

    return degrees + " " + minutes + " " + seconds;
}

function convertDMS(lat, lng) {
    var latitude = toDegreesMinutesAndSeconds(lat);
    var latitudeCardinal = lat >= 0 ? "N" : "S";

    var longitude = toDegreesMinutesAndSeconds(lng);
    var longitudeCardinal = lng >= 0 ? "E" : "W";

    return latitude + " " + latitudeCardinal + "\n" + longitude + " " + longitudeCardinal;
}

Here's an explanation on how this code works:

  • The processing method for the latitude and longitude is pretty much the same, so I abstracted that out to the toDegreesMinutesAndSeconds function. That will return a string that will show, well, degrees, minutes, and seconds.
    • This function will start with the coordinate and truncate it. This value, in positive, is your amount of degrees.
    • The decimal portion needs to be converted to minutes. We take what's left from that rounding and we multiply it by 60.
    • We apply the same logic to get the seconds: so we use only the truncated number for our string but we keep the non-truncated to get the decimal part.
  • Finally, we check if the original value of the coordinate was positive or negative. For latitude, positive (or zero) is North, otherwise South. For longitude, positive (or zero) is East, otherwise, West.
like image 91
Alpha Avatar answered Oct 14 '22 03:10

Alpha


here's two simple functions i created for this; just give the dms to the script

function ConvertDMSToDEG(dms) {   
    var dms_Array = dms.split(/[^\d\w\.]+/); 
    var degrees = dms_Array[0];
    var minutes = dms_Array[1];
    var seconds = dms_Array[2];
    var direction = dms_Array[3];

    var deg = (Number(degrees) + Number(minutes)/60 + Number(seconds)/3600).toFixed(6);

    if (direction == "S" || direction == "W") {
        deg = deg * -1;
    } // Don't do anything for N or E
    return deg;
}

and visa versa just give the degrees to the script, and true of false for lat (latitude)

function ConvertDEGToDMS(deg, lat) {
    var absolute = Math.abs(deg);

    var degrees = Math.floor(absolute);
    var minutesNotTruncated = (absolute - degrees) * 60;
    var minutes = Math.floor(minutesNotTruncated);
    var seconds = ((minutesNotTruncated - minutes) * 60).toFixed(2);

    if (lat) {
        var direction = deg >= 0 ? "N" : "S";
    } else {
        var direction = deg >= 0 ? "E" : "W";
    }

    return degrees + "°" + minutes + "'" + seconds + "\"" + direction;
}

hope this helps people..

like image 21
Marco Tibben Avatar answered Oct 14 '22 03:10

Marco Tibben