﻿ Wrap Latitude and Longitude - Microsoft Research
Share on Facebook Tweet on Twitter Share on LinkedIn Share by email
Wrap Latitude and Longitude
Wrap Latitude and Longitude

These are two simple formulae to wrap latitude and longitude back to their proper ranges.

Latitude measures along the north/south direction on earth, and is normally limited to a range of -90 degrees to +90 degrees. Longitude measures along the east/west direction, and is normally limited to -180 degrees to +180 degrees. Sometimes computations push the value of latitude or longitude beyond their proper range. For instance, if a computation adds or subtracts some number of degrees to a latitude or longitude, the result can be out of range.

As an example, a latitude of +90 degrees is at the North Pole. Adding 5 degrees would nominally give a latitude of +95 degrees, but this is out of range. This 5 degree addition actually pushes past the North Pole back to a latitude of +85 degrees. Similarly, adding 5 degrees to a longitude of +180 degrees should wrap around to -175 degrees as it crosses the antimerdian (180th meridian) at +/- 180 degrees.

The formulae to bring latitude and longitude back into range are:

latitude_new = atan(sin(latitude)/|cos(latitude)|) -- note the absolute value around cos(latitude)

longitude_new = atan2(sin(latitude),cos(latitude)

Here latitude and longitude are in radians. The atan function for latitude returns a result in the range -pi/2 to pi/2. Note that the cosine in the latitude formula has absolute value brackets around it. The atan2(y,x) function for longitude returns a result in the range -pi to pi. These are exactly the proper ranges for latitude and longitude. If the latitude or longitude is already in range, then these formulae will not change them.

To use these formulae with latitude and longitude in degrees, start by converting to radians, apply the formulae, and then convert back to degrees.

There is a plot of these two functions below, showing latitude and longitude in degrees. The horizonal axis in each corresponds to the raw input value, and the vertical axis corresponds to the in-range value.

There are more computationally efficient ways to wrap latitude and longitude, but the formulae above are probably the easiest to implement.