 # Calculation Methods

This page shows how to calculate the geographic midpoint (also known as the geographic center, or center of gravity) for two or more points on the earth's surface. A second method is given showing how to calculate the center of minimum distance**, and finally a third method calculates the average latitude/longitude. There are reasons for choosing one method over another. The first two methods assume that the earth is a perfect sphere, which yields sufficiently accurate results for most purposes. In contrast, the third method uses a flat earth model.

We will assume that the latitude and longitude data for the calculations comes from a list called 'Your places'. Each location may be weighted in one of three ways:

• Weight by time. Use this to specify the amount of time you have lived in each location in order to find the geographic average location for all of the places you have lived in. The time for each location is converted into days, for example: w1 = years*365.25 + months*0.4375 + days.
• Simple weighting factor, such as population for each location can be stored in w1, w2 etc.
• Equal weight. If no location is to be weighted more than another then set w1, w2 etc all = 1.

## A. Geographic midpoint

Note: See this example showing step-by-step numerical calculations using real latitude and longitude data. The formulas below show how to weight by time.

### Summary

The geographic midpoint is calculated by finding the center of gravity for the locations in the 'Your Places' list. The latitude and longitude for each location is converted into Cartesian (x,y,z) coordinates. The x,y, and z coordinates are then multiplied by the weighting factor and added together. A line can be drawn from the center of the earth out to this new x, y, z coordinate, and the point where the line intersects the surface of the earth is the geographic midpoint. This surface point is converted into the latitude and longitude for the midpoint.

### Details

1. Given the values for the first location in the list:
Lat1, lon1, years1, months1 and days1
Convert Lat1 and Lon1 from degrees to radians.
lat1 = lat1 * PI/180
lon1 = lon1 * PI/180
2. Convert lat/lon to Cartesian coordinates for first location.
X1 = cos(lat1) * cos(lon1)
Y1 = cos(lat1) * sin(lon1)
Z1 = sin(lat1)
3. Compute weight (by time) for first location.
w1= (years1 * 365.25) + (months1 * 30.4375) + days1
If locations are to be weighted equally, set w1, w2 etc all equal to 1.
4. Repeat steps 1-3 for all remaining locations in the list.
5. Compute combined total weight for all locations.
Totweight = w1 + w2 + ... + wn
6. Compute weighted average x, y and z coordinates.
x = ((x1 * w1) + (x2 * w2) + ... + (xn * wn)) / totweight
y = ((y1 * w1) + (y2 * w2) + ... + (yn * wn)) / totweight
z = ((z1 * w1) + (z2 * w2) + ... + (zn * wn)) / totweight
7. Convert average x, y, z coordinate to latitude and longitude. Note that in Excel and possibly some other applications, the parameters need to be reversed in the atan2 function, for example, use atan2(X,Y) instead of atan2(Y,X).
Lon = atan2(y, x)
Hyp = sqrt(x * x + y * y)
Lat = atan2(z, hyp)
8. Convert lat and lon to degrees.
lat = lat * 180/PI
lon = lon * 180/PI
9. Special case:
If abs(x) < 10-9 and abs(y) < 10-9 and abs(z) < 10-9 then the geographic midpoint is the center of the earth.

### Notes for method A

The (x,y,z) coordinate system is based on the xy plane lying within the equatorial plane, with its origin at the center of the earth. Looking down onto the North Pole, the positive x-axis passes through the Greenwich meridian (0°E), the positive y-axis passes through the 90°E meridian, and the positive z-axis extends from the center of the earth through the North Pole. The calculations use a unit radius (radius = 1) for the earth. Absolute cartesian coordinates in miles or km can be obtained by multiplying x, y and z by the radius of the earth. Most computer applications require that latitude and longitude data be converted to radians before using them in the trig functions.

## B. Center of minimum distance

This method uses a mathematical algorithm to find the exact point that minimizes the total travel distance from all locations in 'Your Places'. The mathematical implementation of this algorithm is fairly complex, but the general steps are described below. All distances are calculated using the spherical law of cosines given below.

### Description

1. Let CurrentPoint be the geographic midpoint found in Method A. this is used as the starting point for the search.
2. Let MinimumDistance be the sum total of all distances from the current point to all locations in 'Your Places'.
3. Find the total distance between each location in 'Your Places' and all other locations in 'Your Places'. If any one of these locations has a new smallest distance then that location becomes the new CurrentPoint and MinimumDistance.
4. Let TestDistance be PI/2 radians (6225 miles or 10018 km).
5. Find the total distance between each of 8 test points and all locations in 'Your Places'. The test points are arranged in a circular pattern around the CurrentPoint at a distance of TestDistance to the north, northeast, east, southeast, south, southwest, west and northwest.
6. If any of these 8 points has a new smallest distance then that point becomes the new CurrentPoint and MinimumDistance and go back to step 5 using that point.
7. If none of the 8 test points has a new smallest distance then divide TestDistance by 2 and go back to step 5 using the same point.
8. Repeat steps 5 to 7 until no new smallest distance can be found or until TestDistance is less than 0.00000002 radians.

### Spherical law of cosines

distance = acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(lon2 - lon1))

## C. Average latitude/longitude

This method finds a simple average latitude and longitude for the locations in 'Your Places'. This is equivalent to finding a midpoint on a flat rectangular projection map. When the distance between locations is less than 250 miles (400 km), this method gives a close approximation to the true geographic midpoint in Method A.

### Details

1. Use the values calculated in Method A for each location: lat1 to latn, lon1 to lonn, w1 to wn, and totweight.
2. All longitudes in 'Your Places' are adjusted so as to be relative to the geographic midpoint longitude calculated in Method A. This helps eliminate the problems associated with locations on both sides of the International Date Line. For example:
lon1 = lon1 - lonGeographicMidpoint
then if the resulting lon1 lies outside the range -180 to 180, then add or subtract 360 to bring it back into that range.
3. Calculate the weighted latitude and longitude.
lat = (lat1*w1 + lat2*w2 + ... + latn*wn)/totweight
lon = (lon1*w1 + lon2*w2 + ... + lonn*wn)/totweight
4. Add the longitude of the geographic midpoint to return the calculated longitude to its original reference:
lon = lon + lonGeographicMidpoint
Add or subtract 360 from lon if it falls outside the range -180 to 180.

**The center of minimum distance concept and algorithm developed by GeoMidpoint.

## Geographic Midpoint Calculator

See how the midpoint calculations can work in real life with this free online application. With the Geographic Midpoint Calculator you can find the midpoint of an airline flight between two cities. You can also find your personal center of gravity by selecting all of the locations that you have lived in, then view all of those locations displayed on a map along with a marker pointing at the location of your exact personal center of gravity.

## Random Point Generator With Maps

The Random Point Generator generates points at random locations on the surface of the earth. You can throw one or more virtual darts at a Google map and see where they land at.
Random point generator calculation methods

## Let's meet in the middle

This free tool finds the ideal restaurant or other point of interest halfway between two or more addresses. Meet your friend or business acquaintance for lunch.