module Geocoder::Calculations

def geographic_center(points)


the procedure documented at http://www.geomidpoint.com/calculation.html.
(can be mixed). Any objects missing coordinates are ignored. Follows
gravity) for an array of geocoded objects and/or [lat,lon] arrays
Compute the geographic center (aka geographic midpoint, center of
#
def geographic_center(points)
  # convert objects to [lat,lon] arrays and remove nils
  points = points.map{ |p|
    p.is_a?(Array) ? p : (p.geocoded?? p.read_coordinates : nil)
  }.compact
  # convert degrees to radians
  points.map!{ |p| [to_radians(p[0]), to_radians(p[1])] }
  # convert to Cartesian coordinates
  x = []; y = []; z = []
  points.each do |p|
    x << Math.cos(p[0]) * Math.cos(p[1])
    y << Math.cos(p[0]) * Math.sin(p[1])
    z << Math.sin(p[0])
  end
  # compute average coordinate values
  xa, ya, za = [x,y,z].map do |c|
    c.inject(0){ |tot,i| tot += i } / c.size.to_f
  end
  # convert back to latitude/longitude
  lon = Math.atan2(ya, xa)
  hyp = Math.sqrt(xa**2 + ya**2)
  lat = Math.atan2(za, hyp)
  # return answer in degrees
  [to_degrees(lat), to_degrees(lon)]
end