Skip to content
Snippets Groups Projects
Commit bd54a5d1 authored by Alex Reisner's avatar Alex Reisner
Browse files

Make the geographic_center method take geocoded objects as well as two-element arrays.

parent a17e9f0b
No related branches found
No related tags found
No related merge requests found
...@@ -74,8 +74,8 @@ Some utility methods are also available: ...@@ -74,8 +74,8 @@ Some utility methods are also available:
# look up coordinates of some location (like searching Google Maps) # look up coordinates of some location (like searching Google Maps)
Geocoder.fetch_coordinates("25 Main St, Cooperstown, NY") Geocoder.fetch_coordinates("25 Main St, Cooperstown, NY")
# find the geographic center (aka center of gravity) of several points # find the geographic center (aka center of gravity) of objects or points
Geocoder.geographic_center([ [40.22,-73.99], [40.72,-73.98], [40.57,-74.61] ]) Geocoder.geographic_center([ city1, city2, city3, [40.22,-73.99], city4 ])
== More On Configuration == More On Configuration
......
...@@ -213,12 +213,18 @@ module Geocoder ...@@ -213,12 +213,18 @@ module Geocoder
## ##
# Compute the geographic center (aka geographic midpoint, center of # Compute the geographic center (aka geographic midpoint, center of
# gravity) for an array of [lat,lon] points. Follows the procedure # gravity) for an array of geocoded objects and/or [lat,lon] arrays
# documented at http://www.geomidpoint.com/calculation.html. # (can be mixed). Any objects missing coordinates are ignored. Follows
# the procedure documented at http://www.geomidpoint.com/calculation.html.
# #
def self.geographic_center(points) def self.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 lat, lon pairs to radians # convert degrees to radians
points.map!{ |p| [to_radians(p[0]), to_radians(p[1])] } points.map!{ |p| [to_radians(p[0]), to_radians(p[1])] }
# convert to Cartesian coordinates # convert to Cartesian coordinates
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment