For a simple mean, you do not want to average the longitude and latitude coordinates. This might work pretty well at lower latitudes, but at higher latitudes it will begin to give poor results and completely break down near the poles.
The method I've used for this type of thing is to convert the longitude/latitude coordinates to 3d cartesian coordinates (x,y,z). Average these (to give a cartesian vector), and then convert back again. Note that you probably do not need to normalize the vector, so the actual average process could be a simple sum.
Edit, here is my c# code:
The following converts cartesian coordinates to latitude/longitude (in degrees): Remove the RAD2DEG
constants for radians.
Latitude = MPUtility.RAD2DEG * Math.Atan2(z, Math.Sqrt(x * x + y * y));
Longitude = MPUtility.RAD2DEG * Math.Atan2(-y, x);
And here we calculate cartesian coordinates from latitude/longitude (specified in radians):
private void CalcCartesianCoord()
{
_x = Math.Sin(LatitudeRadians) * Math.Cos(LongitudeRadians);
_y = Math.Sin(LatitudeRadians) * Math.Sin(LongitudeRadians);
_z = Math.Cos(LatitudeRadians);
}
Both are cut & pasted from real code, hence the mix of degrees and radians. There are properties here which do some of the conversions (eg. LatitudeRadians
is a property that returns a radian value).
Note that optimization is possible: the duplicate sine calculations, for instance. Also the trig calculations might be cacheable if you call them a lot.
Try this formula (assuming your source is WGS1984, if not then you'll need to adjust the ellipsoid used by the second line):
area = rad(x2 - x1) * (2 + sin(rad(y1)) + sin(rad(y2))) + rad(x3 - x2) * (2 + sin(rad(y2)) + sin(rad(y3))) + rad(x4 - x3) * (2 + sin(rad(y3)) + sin(rad(y4))) + rad(x5 - x4) * (2 + sin(rad(y4)) + sin(rad(y5)))
area = abs(area * 6378137.0 * 6378137.0 / 2.0)
rad() is a function that converts Degrees to Radians (i.e. Degrees * PI / 180)
Source: OpenLayers LinearRing
This will result in an area returned in square meters.
Best Answer
This tutorial from Boundless has some excellent examples of how to work through similar problems in the borough/neighborhood hierarchy in NYC. It doesn't take long to work through the tutorials and you'll feel a lot more at ease with PostGIS in no time.