Skip to content
Browse files

Enabling distance_to and distance_between to take string arguments wh…

…ich will be geocoded using multi geocoder.

git-svn-id: http://geokit.rubyforge.org/svn/trunk@16 9265c765-0211-4c68-b2df-6d1bd6e20c4d
  • Loading branch information...
1 parent 7acbbb5 commit 4d68901dd4cd5d3fc42548ccb55102bd366dc072 bill_eisenhauer committed Feb 15, 2007
Showing with 27 additions and 5 deletions.
  1. +14 −5 lib/geo_kit/mappable.rb
  2. +13 −0 test/acts_as_mappable_test.rb
View
19 lib/geo_kit/mappable.rb
@@ -29,19 +29,28 @@ module ClassMethods #:nodoc:
# :units - valid values are :miles or :kms (:miles is the default)
# :formula - valid values are :flat or :sphere (:sphere is the default)
def distance_between(from, to, options={})
+ from_point = from && from.is_a?(String) ? geocode(from) : from
+ to_point = to && to.is_a?(String) ? geocode(to) : to
units = options[:units] || :miles
formula = options[:formula] || :sphere
case formula
when :sphere
units_sphere_multiplier(units) *
- Math.acos( Math.sin(deg2rad(get_lat(from))) * Math.sin(deg2rad(get_lat(to))) +
- Math.cos(deg2rad(get_lat(from))) * Math.cos(deg2rad(get_lat(to))) *
- Math.cos(deg2rad(get_lng(to)) - deg2rad(get_lng(from))))
+ Math.acos( Math.sin(deg2rad(get_lat(from_point))) * Math.sin(deg2rad(get_lat(to_point))) +
+ Math.cos(deg2rad(get_lat(from_point))) * Math.cos(deg2rad(get_lat(to_point))) *
+ Math.cos(deg2rad(get_lng(to_point)) - deg2rad(get_lng(from_point))))
when :flat
- Math.sqrt((units_per_latitude_degree(units)*(get_lat(from)-get_lat(to)))**2 +
- (units_per_longitude_degree(get_lat(from), units)*(get_lng(from)-get_lng(to)))**2)
+ Math.sqrt((units_per_latitude_degree(units)*(get_lat(from_point)-get_lat(to_point)))**2 +
+ (units_per_longitude_degree(get_lat(from_point), units)*(get_lng(from_point)-get_lng(to_point)))**2)
end
end
+
+ # Geocodes a location using the multi geocoder.
+ def geocode(location)
+ res = Geocoders::MultiGeocoder.geocode(location)
+ return res if res.success
+ raise Geocoders::GeocodeError
+ end
protected
View
13 test/acts_as_mappable_test.rb
@@ -17,6 +17,8 @@
# Establish test tables.
load(File.dirname(__FILE__) + "/schema.rb")
+GeoKit::Geocoders::PROVIDER_ORDER=[:google,:us]
+
# Uses defaults
class Company < ActiveRecord::Base #:nodoc: all
has_many :locations
@@ -64,6 +66,17 @@ def setup
@custom_loc_e = custom_locations(:e)
end
+ def test_distance_between_geocoded
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("San Francisco, CA").returns(@location_a)
+ assert_equal 0, Location.distance_between("Irving, TX", "San Francisco, CA")
+ end
+
+ def test_distance_to_geocoded
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
+ assert_equal 0, @custom_loc_a.distance_to("Irving, TX")
+ end
+
def test_custom_attributes_distance_calculations
assert_equal 0, @custom_loc_a.distance_to(@loc_a)
assert_equal 0, CustomLocation.distance_between(@custom_loc_a, @loc_a)

0 comments on commit 4d68901

Please sign in to comment.
Something went wrong with that request. Please try again.