GPS position conversion
When it comes to the geographic coordinate system we are mainly using latitude and longitude to determine the position or location on Earth. The latitude is the angle on Earth’s surface measured to the North or South, between the equator and the place we are thinking of. The longitude is the angle to the West or East from the meridian of the British Royal Observatory in Greenwich to the place we are thinking of. Since they are angles, in most cases we are using degrees to write the position.
Common formats used
In most cases, you will spot three different formats. The accuracy of the position is the same for all of them. Let’s take a look at the same position written in these formats.
Decimal Degrees (DD) format
In this format, the space between each line of latitude or longitude representing one degree is divided and expressed as decimals. The sample position in this format can be written as follows:
N 38.897686°, W 77.036557°
This is almost 39 degrees to the North and only a little above 77 degrees to the West. Let’s take a look at the same position in the different format.
Degrees and decimal minutes (DM or DMM)
In this format, each degree is further divided into 60 minutes. Each minute is further divided and expressed as decimals. Our position will now be written as follows:
N 38° 53.8612′, W 77° 02.1934′
It is 38 degrees and almost 54 minutes to the North, 77 degrees and a little above 2 minutes to the West.
Degrees, minutes and seconds (DMS)
In this format, each degree is divided into 60 minutes and each minute into 60 seconds. To achieve even better accuracy, we sometimes use also decimals of the second. Our position will now be written like this:
N 38° 53′ 51.67″, W 77° 02′ 11.6″
It is 38 degrees, 53 minutes and over 51 seconds to the North, 77 degrees, 2 minutes and over 11 seconds to the West.
Conversion from DD
latitude = 38.897686; logitude = 77.036557; degreesLatitude = Math.floor(latitude); degreesLongitude = Math.floor(longitude); minutesLatitude = Math.round((latitude - degreesLatitude)*60*10000)/10000; minutesLongitude = Math.round((logitude - degreesLongitude)*60*10000)/10000;
As you can see, I have now separate variables with degrees and minutes stored. Degrees are rounded down to the total values, minutes are stored as degrees and four decimal places – it is good enough in most cases.
Given that we want to have DMS format now, we will use the same method to calculate seconds.
mrLatitude = Math.floor(minutesLatitude); mrLongitude = Math.floor(minutesLongitude); secondsLatitude = Math.round((minutesLatitude - mrLatitude)*60*100)/100; secondsLongitude = Math.round((minutesLongitude - mrLongitude)*60*100)/100;
First I created new variables to store minutes rounded down to the total values. Then I used them to calculate seconds. I left only two decimal places with each seconds calculation – this is also good enough in most cases.
Conversion to DD
Let’s now take a look at the opposite situation – we want to obtain DD format. Starting with DMS like this N 38° 53′ 51.67″, W 77° 02′ 11.6″ we have the clear separation of degrees, minutes and seconds (with decimals). We can use these parts to calculate DD:
latitude = parseFloat(degreesLatitude) + minutesLatitude/60 + secondsLatitude/60/60; longitude = parseFloat(degreesLongitude) + minutesLongitude/60 + secondsLongitude/60/60;
For the DM format like this N 38° 53.8612′, W 77° 02.1934′ the calculation will be even simpler:
latitude = parseFloat(degreesLatitude) + minutesLatitude/60; longitude = parseFloat(degreesLongitude) + minutesLongitude/60;