# Find Center Point From List of Lat Long Coords in Linq

I need to find the center point of a list of coordinates, but I don't want to simply search for the extremities and find a midpoint, as some of the coordinates could be rogues.  Is there a way that I can somehow weight the center based on the density ?  Any ideas ?
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
A good way to find a geographical centroid is to treat all the coordinates as 3-d vectors and average them.
DeveloperCommented:
In other words, something like:
``````Imports System.Device.Location

Module Module1
ReadOnly coordinates As New List(Of GeoCoordinate) From _
{ _
New GeoCoordinate() With {.Latitude = 22.9833, .Longitude = 72.5}, _
New GeoCoordinate() With {.Latitude = 18.975, .Longitude = 72.8258}, _
New GeoCoordinate() With {.Latitude = 22.3, .Longitude = 73.2003}, _
New GeoCoordinate() With {.Latitude = 26.926, .Longitude = 75.8235}, _
New GeoCoordinate() With {.Latitude = 28.61, .Longitude = 77.23}, _
New GeoCoordinate() With {.Latitude = 22.3, .Longitude = 70.7833} _
}

Sub Main()
Dim central = coordinates.GetCentralGeoCoordinate()
Console.WriteLine("Central Latitude: {0}; Central Longitude: {1}", central.Latitude, central.Longitude)
End Sub
End Module

Module Extensions
<System.Runtime.CompilerServices.Extension()> _
Public Function GetCentralGeoCoordinate(ByVal coordinates As IEnumerable(Of GeoCoordinate)) As GeoCoordinate
' Original code authored by Yodacheese - http://stackoverflow.com/questions/6671183/calculate-the-center-point-of-multiple-latitude-longitude-coordinate-pairs
If coordinates.Count = 1 Then Return coordinates.Single()

Dim CentralLatitude As Double
Dim CentralLongitude As Double

Dim x As Double = 0
Dim y As Double = 0
Dim z As Double = 0

Dim vectors = (From coordinate As GeoCoordinate In coordinates
Let latitude = coordinate.Latitude * Math.PI / 180
Let longitude = coordinate.Longitude * Math.PI / 180
Let a = Math.Cos(latitude) * Math.Cos(longitude)
Let b = Math.Cos(latitude) * Math.Sin(longitude)
Let c = Math.Sin(latitude)
Select New With {.X = a, .Y = b, .Z = c})

x = vectors.Sum(Function(vector) vector.X) / coordinates.Count
y = vectors.Sum(Function(vector) vector.Y) / coordinates.Count
z = vectors.Sum(Function(vector) vector.Z) / coordinates.Count

CentralLatitude = Math.Atan2(z, Math.Sqrt(x * y + y * y))
CentralLongitude = Math.Atan2(y, x)

Return New GeoCoordinate(CentralLatitude * 180 / Math.PI, CentralLongitude * 180 / Math.PI)
End Function
End Module
``````
Which produces the following output --saige-

Experts Exchange Solution brought to you by