# Geographical Center Problem

Given a list of locations, where is the central-most place amongst them? This question is harder than it appears. When I first worked at the problem, I averaged the latitudes and longitudes which works for a small region, but this simplistic solution has problems when you go global.

Here is an example: what is the average longitude of 175 degrees East and 175 degrees West? Numerically the average of 175 and -175 is zero, but the answer geographically is 180 degrees!

## Description

George Huxtable, a member of the Navigation Mailing List, had this to say about a possible solution to the problem, in a post dated 23 July 2001:

```I have been pondering on what would be a meaningful way to define a centre
of gravity for individuals members scattered over the surface of a sphere.

Somehow, I doubt whether averaging latitudes and then separately averaging
longitudes provides the best answer. It should be possible to do the job
properly, for anyone with a bit of time on his hands. Here is a suggested
mechanical analogy.

Take a globe of the world, that will float. For example, I have a beachball
printed with the World on its surface, crudely marked with lat and long. If
you put it into a pool, it will float any way up, like most spheres.

Now glue on to its surface a set of identical coins or weights, one for
each member, in the spot where they live, and chuck it into the pool again.
Now, the lowest point of this globe represents in some way a centre of
concentration of the members.

It should be possible to devise a computer analogy to avoid having to do
the physical experiment. Would it produce the same answer as Dan Allen's
method? I wonder...

Just a thought,

George Huxtable.
george@huxtable.u-net.com
George Huxtable, 1 Sandy Lane, Southmoor, Abingdon, Oxon OX13 5HX, UK.
Tel. 01865 820222 or (int.) +44 1865 820222.
```

## Analysis

My friend Dr. Paul Finlayson then came up with this theory behind the problem. He describes his analysis of the problem:

```The floating world-globe with glued coins idea is clever.  The lat-lon of
the lowest point when the globe is thrown into the pool can be computed as
follows:

For each coin, compute the cartesian position x,y,z from:

x = cos(lat)cos(lon)
y = cos(lat)sin(lon)
z = sin(lat)

Next, separately average the x, y, and z values to get xAv, yAv, zAv.  This
is the position of the center of mass of the coins.  The (lat, lon) of the
center of mass is computed from:

lat = asin(zAv/r)  where r = sqrt(xAv^2 + yAv^2 + zAv^2)
lon = atan2(yAv, xAv)

So, assuming my math and logic are correct, this is the lat and lon of the
lowest point of the globe when thrown back into the pool.
```

## Solution

Given this analysis, I implemented a computation of this in JavaScript which runs on your computer as part of rendering this webpage. View the source code to this page to see the JavaScript code. North latitudes and West longitudes are positive.

Here is the current dataset and the results:

```
/*
* center.js - find the geographical center of a list of places.
*
* 24 Jul 2001 - Created by Dan Allen.
* 27 Jul 2001 - Rounds result, degree symbols added.
* 18 Oct 2003 - Added new faces to the table.
* 17 Jan 2004 - Updated and removed older inactive members.
* 18 Jan 2004 - Added weightings.
*
* To extract for map:
*
* tt -s '\q' '' < ¤ | tt -d , \$3 \$2 \$1 | grep '\.' | sort -n | tt \$2 \$1 \$3
*
*/

var DTOR = Math.PI/180;
var RTOD = 180/Math.PI;

var a = new Array(
"Dan Allen",47.482,121.798,296,
"William Allen *",34.417,119.683,23,
"Yves Arrouye",37.892,122.997,52,
"Pierre Boucher",45.647,73.866,37,
"Brooke Clarke *",39.149,123.209,42,
"Robert Eno",63.75,68.517,104,
"Peter Fogg *",-33.733,-151.066,189,
"Vic Fraenckel *",42.832,73.965,48,
"Nigel Gardner",56,3.5,42,
"Ken Gebhart",37.683,97.267,24,
"Fred Hebard *",36.76,81.85,184,
"Paul Hirose *",40.69,74.045,130,
"Dan Hogan",33.147,117.958,308,
"George Huxtable",51.667,1.283,468,
"John Kabel",43,81.21,42,
"Jan Kalivoda",50.083,-14.417,84,
"Trevor Kenchington",44.65,63.583,159,
"Geoffrey Kolbe",55.25,2.712,44,
"Lee Martin",-34.994,-149.088,39,
"Hal Mueller",47.688,122.393,24,
"Rodney Myrvaagnes",40.733,74,195,
"Bill Noyce",42.743,71.592,58,
"Philip Ouvry",42.43,-3.15,14,
"Arthur Pearson",42.378,71.165,69,
"Richard Pisko",49.7,112.833,36,
"Herbert Prinz",41.240,73.441,150,
"Frank Reed *",41.35,71.96,92,
"Doug Royer",32.832,116.831,113,
"Andres Ruiz",43.317,2,21,
"Jared Sherman",40.717,74.017,257,
"Peter Smith",42.217,71.283,58,
"Bruce Stark *",44.015,123.068,105,
"Gordon Talge",34.052,117.253,67,
"Chuck Taylor",47.920,122.187,72,
"Steven Tripp",37.5,-140,39,
"Steven Wepster",52.088,-5.133,71
);

var z = x.toString();
if (x > 0) z = " " + z;
if (Math.abs(x) < 10) z = " " + z;
if (Math.abs(x) < 100) z = " " + z;
while (z.length < 5) z = z + " ";
return z;
}

var z = x.toString();
if (x > 0) z = " " + z;
if (Math.abs(x) < 10) z = " " + z;
if (Math.abs(x) < 100) z = " " + z;
z = z + "&deg;";
while (z.length < 15) z = z + " ";
return z;
}

function round(x,n) {
return Math.round(x*Math.pow(10,n))/Math.pow(10,n);
}

var i,lat,lon,r,x,y,z,n,sumX,sumY,sumZ,postN,postX,postY,postZ,posts;
sumX = sumY = sumZ = n = 0;
postX = postY = postZ = postN = 0;
document.write(" Latitude    Longitude   Posts   NAV-L Member (alphabetical by last name)<P><HR><P>");
for (i = 0; i < a.length/4; ++i) {
lat = a[i*4+1]*DTOR;
lon = a[i*4+2]*DTOR;
posts = a[i*4+3];
x = Math.cos(lat)*Math.cos(lon);
y = Math.cos(lat)*Math.sin(lon);
z = Math.sin(lat);
sumX += x;
sumY += y;
sumZ += z;
++n;
postX += x*posts;
postY += y*posts;
postZ += z*posts;
postN += posts;
document.write(paddeg(a[i*4+1]) + "  " + paddeg(a[i*4+2]) + "  " + pad(a[i*4+3]) + "  " + a[i*4] + "<BR>");
}

x = sumX/n;
y = sumY/n;
z = sumZ/n;
r = Math.sqrt(x*x + y*y + z*z);
lat = Math.asin(z/r)*RTOD;
lon = Math.atan2(y,x)*RTOD;
document.write("<P>* = my guess at their home location<P><HR><P>CENTER:<P>" + paddeg(round(lat,3)) + "  " + paddeg(round(lon,3)) + "         Center (unweighted)<BR>");
x = postX/postN;
y = postY/postN;
z = postZ/postN;
r = Math.sqrt(x*x + y*y + z*z);
lat = Math.asin(z/r)*RTOD;
lon = Math.atan2(y,x)*RTOD;
document.write(paddeg(round(lat,3)) + "  " + paddeg(round(lon,3)) + "         Center weighted by number of postings<BR>");

The center is in Canada, in northern Quebec, south of Hudson Bay:

206.3 nmi  43° NE of Pickle Lake, Canada. (nearest airport)
258.5 nmi 310° NW of Moosonee, Canada. (nearest seaport)
263.0 nmi  16°  N of Katatota Island, Canada.

The weighted center moves southeast 109 nmi from the unweighted center:

159.5 nmi 297° NW of Moosonee, Canada. (nearest seaport)
156.9 nmi  31° NE of NAKINA, CA. (nearest airport)
216.6 nmi  40° NE of Katatota Island, Canada.

```

```Created:  26 Dec 2000
Modified: 18 Jan 2004

```