# 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:
* 402646302N 0795855903W
* 40:26:46.302N 079:58:55.903W
* 40°26′46″N 079°58′56″W
* 40d 26′ 46″ N 079d 58′ 56″ W
* 40.446195N 79.982195W
* 40.446195, -79.982195
* 40.446195,-79.982195
* 40° 26.7717, -79° 58.93172
* N40:26:46.302 W079:58:55.903
* N40°26′46″ W079°58′56″
* N40d 26′ 46″ W079d 58′ 56″
* N40.446195 W79.982195
Coordinates are generally written as 40°26'21"N 079°58'56"W.

## Basic forms

There are three basic forms of a coordinate.
* Coordinate containing degrees (integer), minutes (integer), and seconds (integer, or real number) (DMS).
* Coordinate containing degrees (integer) and minutes (real number) (MinDec).
* 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:
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:
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:

### 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:

### 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.
To convert, 56 seconds is equal to ${\frac {56}{60}}=0.9{\bar {3}}$ minutes.

## Putting it all together
### Conversion from DMS to Decimal Degree

Given a DMS (Degrees, Minutes, Seconds) cordinate such as W079°58′56″, convert it to a number of decimal degrees using the following method:
* Calculate the total number of seconds: 58′56″ = (58*60 + 56) = 3536 seconds.
* The fractional part is the total number of seconds divided by 3600: 3536 / 3600 = ~0.982222
* Add fractional degrees to whole degrees to produce the final result: 79 + 0.982222 = 79.982222
* Since it is a West longitude coordinate, negate the result.
* The final result is -79.982222.

### Conversion from MinDec to Decimal Degree

Given a MinDec (Degrees, Minutes, Decimal Minutes) coordinate such as 79°58.93172W, convert it to a number of decimal degrees using the following method:
* The integer number of degrees is the same (79)
* The decimal degrees is the decimal minutes divided by 60 (58.93172/60 = ~0.982195)
* Add the two together (79 + 0.982195= 79.982195)
* For coordinates ind the western (or southern) hemisphere, negate the result.
* The final result is -79.982195

### Conversion from Decimal Degree to DMS

Given a decimal longitudinal coordinate such as -79.982195 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. In the example of -79.982195: -79 degrees.
* Multiply the remaining fractional part by 60. This will produce a number of minutes in the whole number portion. In the example: 0.982195 x 60 = 58.9317 => 58 minutes.
* Multiply the fractional part of the number of minutes by 60, producing a number of seconds. 0.9317 x 60 = 55.903 => 55.903 seconds. It is possible to keep the entire number, truncate to the decimal 55 or round to the decimal 56.
* 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:
A latitude of 0°0′0″ (at The Equator) is neither North nor South. Similarly, a longitude of 0°0′0″ (at the Prime Meridian) is neither East nor West. These are referred to as zero latitude and zero longitude, respectively. A longitude of 180°0′0″ (the 180th meridian) is neither East nor West. This is the basis for the International Date Line when referring to the Earth.
* The final result for the above example is: W 79°58′56″.

#### Pro-grammatical conversion

The most common pro-grammatical 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: