# Geographic coordinate conversion

Geographic coordinates consist of latitude and longitude.

## Ways of writing coordinates

All of the following are valid and acceptable ways to write geographic coordinates:

• -23.399437,-52.090904
• 40:26:46.302N 079:56:55.903W
• 40°26′47″N 079°58′36″W
• 40d 26′ 47″ N 079d 58′ 36″ W
• 40.446195N 79.948862W
• 40.446195, -79.948862
• 40° 26.7717, -79° 56.93172
• N40:26:46.302 W079:56:55.903
• N40°26′47″ W079°58′36″
• N40d 26′ 47″ W079d 58′ 36″
• N40.446195 W79.948862

Coordinates in this encyclopedia are generally written as 40°26′21″N 079°58′36″W﻿ / ﻿40.43917°N 79.97667°W.

## Basic forms

There are three basic forms of a coordinate.

1. Coordinate containing degrees (integer), minutes (integer), and seconds (integer, or real number) (DMS).
2. Coordinate containing degrees (integer) and minutes (real number) (MinDec).
3. Coordinate containing only degrees (real number) (DegDec).

All forms of coordinates are capable of representing the same amount of data and the same precision. Depending on which type of coordinate you are provided with, and which type you would like to work with, you may have to do some conversion.

## Components of a typical coordinate

In its most simple form a coordinate is just a number of degrees. The tricky part comes in when you need to differentiate North/South latitude or West/East longitude, or make the number more digestible by writing it with minutes and seconds instead of as a decimal number.

### Degrees

The degrees portion of the coordinate is always going to be the easiest to figure out. The degrees is always the left-most whole number. For example:

 40:26:46N         40 W87°43′41        -087

A sphere is divided into 360 degrees. The number space is divided into two halves, East and West in the case of longitude and North and South in the case of latitude. The maximum ranges are as follows:

 Longitude  180 W   = -180  180 E   =  180
 Latitude   90 N   =   90   90 S   =  -90

Technically you could have latitudes greater than 90 or less than -90, but this is an ambiguous case, since there would be an equivalent coordinate with an inverse longitude.

The minimal case is that you have only degrees:

  50.21389 or  50.21389N

### Minutes

Minutes are an optional component, as is implied by the minimal case of degrees. If there is no minutes component, the degrees component contains the entire precision of the coordinate and there must not be a seconds component. Minutes are actually the numerator component of a fraction with denominator 60 of one degree.

With the same examples as above:

 40:26:46N         26 W087°43′41         43

In the first case, the number of minutes is 26.

### Seconds

Seconds are also an optional component, and can only exist if the minutes component also exists. Seconds are the numerator component of a fraction with denominator 60 of one minute.

 40:26:46N         46 W087°43′41         41

In the second case, the number of seconds is 41.

To convert, 41 seconds is equal to $\frac{41}{60} = 0.68\bar{3}$ minutes.

## Putting it all together

### Conversion from DMS to Decimal Degree

Given a DMS (Degrees, Minutes, Seconds) coordinate such as W087°43′41″, convert it to a number of decimal degrees using the following method:

1. Calculate the total number of seconds:
43′41″ = (43*60 + 41) = 2621 seconds.
2. The fractional part is total number of seconds divided by 3600:
2621 / 3600 = ~0.728056
3. Add fractional degrees to whole degrees to produce the final result:
87 + 0.728056 = 87.728056
4. Since it is a West longitude coordinate, negate the result.
5. The final result is -87.728056.

### Conversion from MinDec to Decimal Degree

Given a MinDec (Degrees, Minutes, Decimal Minutes) coordinate such as 40°26.7717N, 79°56.93172W, convert it to a number of decimal degrees using the following method (for example for 79°56.93172W):

1. The integer number of degrees is the same (79)
2. The decimal degrees is the decimal minutes divided by 60 (56.93172/60 = 0.948862)
3. Add the two together (79 + 0.948862 = 79.948862)
4. Negate the value if it is South or West (in this case, West, so -79.948862)

### Conversion from Decimal Degree to DMS

Given a decimal longitudinal coordinate such as -87.728055 it will be necessary to know whether it is a latitudinal or longitudinal coordinate in order to fully convert it. The method is as follows:

• Subtract the whole number portion of the coordinate, leaving the fractional part. The whole number is the number of degrees. 87.728055 = 87 degrees.
• Multiply the remaining fractional part by 60. This will produce a number of minutes in the whole number portion. 0.728055 x 60 = 43.6833 = 43 minutes.
• Multiply the fractional part of the number of minutes by 60, producing a number of seconds. 0.6833 x 60 = 40.998 = 41 seconds. It is possible to count this as 40 seconds, truncating the decimal, round it to 41, or keep the entire number.
• Depending on whether the source number was a latitudinal or longitudinal coordinate, and the sign of the number, add the N/S/E/W specifier. The following table shows the possibilities:
   Type   Dir.   Sign    Test   Lat.   N      +       > 0   Lat.   S      -       < 0   Long.  E      +       > 0   Long.  W      -       < 0

A latitude of 0°0′0″ (The Equator) is neither North nor South. Similarly, a longitude of 0°0′0″ (Prime Meridian) is neither East nor West. These are referred to as zero latitude and zero longitude, respectively. A longitude of 180°0′0″ (180th meridian) is neither East nor West. This is the basis for the International Date Line when referring to the Earth.

• The final result is: W 87°43′41″.

#### Programmatical conversion

The most common programmatical use of these processes is to display a coordinate to an end user in the more common DMS form instead of decimal form. Below is a piece of pseudocode to convert from decimal degrees to degrees, minutes, and seconds:

 function deg_to_dms ( degfloat )    Input must be non-negative:    if degfloat < 0       error    end if    Compute degrees, minutes and seconds:    deg ← integerpart ( degfloat )    minfloat ← 60 * ( degfloat - deg )    min ← integerpart ( minfloat )    secfloat ← 60 * ( minfloat - min )    Round seconds to desired accuracy:    secfloat ← round( secfloat, digits )    After rounding, the seconds might become 60. These two    if-tests are not necessary if no rounding is done.    if secfloat = 60       min ← min + 1       secfloat ← 0    end if    if min = 60       deg ← deg + 1       min ← 0    end if    Return output:    return ( deg, min, secfloat ) end function

#### Java Implementation

 // Input a double latitude or longitude in the decimal format // e.g. 87.728056 String decimalToDMS(double coord) {         String output, degrees, minutes, seconds;           // gets the modulus the coordinate divided by one (MOD1).         // in other words gets all the numbers after the decimal point.         // e.g. mod = 87.728056 % 1 == 0.728056         //         // next get the integer part of the coord. On other words the whole number part.         // e.g. intPart = 87           double mod = coord % 1;         int intPart = (int)coord;           //set degrees to the value of intPart         //e.g. degrees = "87"           degrees = String.valueOf(intPart);           // next times the MOD1 of degrees by 60 so we can find the integer part for minutes.         // get the MOD1 of the new coord to find the numbers after the decimal point.         // e.g. coord = 0.728056 * 60 == 43.68336         //      mod = 43.68336 % 1 == 0.68336         //         // next get the value of the integer part of the coord.         // e.g. intPart = 43           coord = mod * 60;         mod = coord % 1;         intPart = (int)coord;         if (intPart < 0) {            // Convert number to positive if it's negative.            intPart *= -1;         }           // set minutes to the value of intPart.         // e.g. minutes = "43"         minutes = String.valueOf(intPart);           //do the same again for minutes         //e.g. coord = 0.68336 * 60 == 41.0016         //e.g. intPart = 41         coord = mod * 60;         intPart = (int)coord;         if (intPart < 0) {            // Convert number to positive if it's negative.            intPart *= -1;         }           // set seconds to the value of intPart.         // e.g. seconds = "41"         seconds = String.valueOf(intPart);           // I used this format for android but you can change it          // to return in whatever format you like         // e.g. output = "87/1,43/1,41/1"         output = degrees + "/1," + minutes + "/1," + seconds + "/1";           //Standard output of D°M′S″         //output = degrees + "°" + minutes + "'" + seconds + "\"";           return output; }          /*         * Conversion DMS to decimal          *         * Input: latitude or longitude in the DMS format ( example: N 43° 36' 15.894")         * Return: latitude or longitude in decimal format            * hemisphereOUmeridien => {W,E,S,N}         *         */         public double DMSToDecimal(String hemisphereOUmeridien,double degres,double minutes,double secondes)         {                 double LatOrLon=0;                 double signe=1.0;                   if((hemisphereOUmeridien.equals("W"))||(hemisphereOUmeridien.equals("S"))) {signe=-1.0;}                                 LatOrLon = signe*(Math.floor(degres) + Math.floor(minutes)/60.0 + secondes/3600.0);                   return(LatOrLon);                        }