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

Add to_coordinates public instance method.

Replaces read_coordinates (private) and fixes bug in geographic center
computation (reported by github.com/jonbuda).
parent 5ffa8447
No related branches found
No related tags found
No related merge requests found
......@@ -41,9 +41,7 @@ module Geocoder
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
points.map!{ |p| p.is_a?(Array) ? p : p.to_coordinates }.compact
# convert degrees to radians
points.map!{ |p| [to_radians(p[0]), to_radians(p[1])] }
......
......@@ -6,7 +6,14 @@ module Geocoder
# Is this object geocoded? (Does it have latitude and longitude?)
#
def geocoded?
read_coordinates.compact.size > 0
to_coordinates.compact.size > 0
end
##
# Coordinates [lat,lon] of the object.
#
def to_coordinates
[:latitude, :longitude].map{ |i| send self.class.geocoder_options[i] }
end
##
......@@ -16,7 +23,7 @@ module Geocoder
#
def distance_to(lat, lon, units = :mi)
return nil unless geocoded?
mylat,mylon = read_coordinates
mylat,mylon = to_coordinates
Geocoder::Calculations.distance_between(mylat, mylon, lat, lon, :units => units)
end
......@@ -29,7 +36,7 @@ module Geocoder
def nearbys(radius = 20, units = :mi)
return [] unless geocoded?
options = {:exclude => self, :units => units}
self.class.near(read_coordinates, radius, options)
self.class.near(to_coordinates, radius, options)
end
##
......@@ -83,14 +90,6 @@ module Geocoder
end
end
end
##
# Read the coordinates [lat,lon] of the object.
# Looks at user config to determine attributes.
#
def read_coordinates
[:latitude, :longitude].map{ |i| send self.class.geocoder_options[i] }
end
end
end
end
......@@ -23,13 +23,19 @@ class GeocoderTest < Test::Unit::TestCase
assert_equal 69, Geocoder::Calculations.distance_between(0,0, 0,1).round
end
def test_geographic_center
def test_geographic_center_with_arrays
assert_equal [0.0, 0.5],
Geocoder::Calculations.geographic_center([[0,0], [0,1]])
assert_equal [0.0, 1.0],
Geocoder::Calculations.geographic_center([[0,0], [0,1], [0,2]])
end
def test_geographic_center_with_mixed_arguments
p1 = [0, 0]
p2 = Landmark.new("Some Cold Place", 0, 1)
assert_equal [0.0, 0.5], Geocoder::Calculations.geographic_center([p1, p2])
end
def test_does_not_choke_on_nil_address
all_lookups.each do |l|
Geocoder::Configuration.lookup = l
......@@ -80,7 +86,7 @@ class GeocoderTest < Test::Unit::TestCase
e = Event.new(*venue_params(:msg))
coords = [40.750354, -73.993371]
e.geocode
assert_equal coords.map{ |c| c.to_s }.join(','), e.coordinates
assert_equal coords.map{ |c| c.to_s }.join(','), e.coords_string
end
def test_geocode_with_block_doesnt_auto_assign_coordinates
......
......@@ -132,7 +132,7 @@ end
class Event < ActiveRecord::Base
geocoded_by :address do |obj,result|
if result
obj.coordinates = "#{result.latitude},#{result.longitude}"
obj.coords_string = "#{result.latitude},#{result.longitude}"
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment