Permalink
Browse files

deep-sixed :lat and :lng on AR finders

git-svn-id: http://geokit.rubyforge.org/svn/trunk@19 9265c765-0211-4c68-b2df-6d1bd6e20c4d
  • Loading branch information...
1 parent 9442c78 commit 38851d173bc2315a956c6d3d86a5a5d15b8964a3 lewisac committed Feb 16, 2007
Showing with 27 additions and 26 deletions.
  1. +19 −18 lib/geo_kit/acts_as_mappable.rb
  2. +8 −8 test/acts_as_mappable_test.rb
View
37 lib/geo_kit/acts_as_mappable.rb
@@ -3,9 +3,12 @@ module GeoKit
# When mixed in, augments find services such that they provide distance calculation
# query services. The find method accepts additional options:
#
- # * :origin - which contains an object which has lat and lng attributes
- # * :lat, :lng - in lieue of an origin, specific lat and lng attributes can be specified
- #
+ # * :origin - can be
+ # 1. a two-element array of latititude/longitude -- :origin=>[37.792,-122.393]
+ # 2. a geocodeable string -- :origin=>'100 Spear st, San Francisco, CA'
+ # 3. an object which responds to lat and lng methods, or latitude and longitude methods,
+ # or whatever methods you have specified for lng_column_name and lat_column_name
+ #
# Other finder methods are provided for specific queries. These are:
#
# * find_within (alias: find_inside)
@@ -117,13 +120,13 @@ def distance_sql(origin, units=default_units, formula=default_formula)
# option keys from the hash.
def extract_origin_from_options(options)
origin = options[:origin]
- origin = geocode_origin(origin) if origin && origin.is_a?(String)
- origin = GeoKit::LatLng.new(options[:origin][0], options[:origin][1]) if origin && origin.is_a?(Array)
- unless origin
- origin = GeoKit::LatLng.new(options[:lat], options[:lng]) if options[:lat] && options[:lng]
+ if origin
+ res = geocode_origin(origin) if origin.is_a?(String)
+ res = GeoKit::LatLng.new(options[:origin][0], options[:origin][1]) if origin.is_a?(Array)
+ res = GeoKit::LatLng.new(extract_latitude(origin), extract_longitude(origin)) unless res
end
- [:origin, :lat, :lng].each {|option| options.delete(option)}
- origin
+ options.delete(:origin)
+ res
end
# Extract the units out of the options if it exists and returns it. If
@@ -194,8 +197,8 @@ def substitute_distance_in_conditions(options, origin, units=default_units, form
# Returns the distance SQL using the spherical world formula (Haversine). The SQL is tuned
# to the database in use.
def sphere_distance_sql(origin, units)
- lat = deg2rad(extract_latitude(origin))
- lng = deg2rad(extract_longitude(origin))
+ lat = deg2rad(origin.lat)
+ lng = deg2rad(origin.lng)
multiplier = units_sphere_multiplier(units)
case connection.adapter_name.downcase
when "mysql"
@@ -218,20 +221,18 @@ def sphere_distance_sql(origin, units)
# Returns the distance SQL using the flat-world formula (Phythagorean Theory). The SQL is tuned
# to the database in use.
def flat_distance_sql(origin, units)
- lat = extract_latitude(origin)
lat_degree_units = units_per_latitude_degree(units)
- lng = extract_longitude(origin)
- lng_degree_units = units_per_longitude_degree(lat, units)
+ lng_degree_units = units_per_longitude_degree(origin.lat, units)
case connection.adapter_name.downcase
when "mysql"
sql=<<-SQL_END
- SQRT(POW(#{lat_degree_units}*(#{lat}-#{lat_column_name}),2)+
- POW(#{lng_degree_units}*(#{lng}-#{lng_column_name}),2))
+ SQRT(POW(#{lat_degree_units}*(#{origin.lat}-#{lat_column_name}),2)+
+ POW(#{lng_degree_units}*(#{origin.lng}-#{lng_column_name}),2))
SQL_END
when "postgresql"
sql=<<-SQL_END
- SQRT(POW(#{lat_degree_units}*(#{lat}-#{lat_column_name}),2)+
- POW(#{lng_degree_units}*(#{lng}-#{lng_column_name}),2))
+ SQRT(POW(#{lat_degree_units}*(#{origin.lat}-#{lat_column_name}),2)+
+ POW(#{lng_degree_units}*(#{origin.lng}-#{lng_column_name}),2))
SQL_END
else
sql = "unhandled #{connection.adapter_name.downcase} adapter"
View
16 test/acts_as_mappable_test.rb
@@ -115,7 +115,7 @@ def test_find_within
end
def test_find_within_with_coordinates
- locations = Location.find_within(3.97, :lat => @loc_a.lat, :lng => @loc_a.lng)
+ locations = Location.find_within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
assert_equal 5, locations.size
end
@@ -135,7 +135,7 @@ def test_find_beyond
end
def test_find_beyond_with_coordinates
- locations = Location.find_beyond(3.95, :lat => @loc_a.lat, :lng => @loc_a.lng)
+ locations = Location.find_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
assert_equal 1, locations.size
end
@@ -144,15 +144,15 @@ def test_find_nearest
end
def test_find_nearest_with_coordinates
- assert_equal @loc_a, Location.find_nearest(:lat => @loc_a.lat, :lng => @loc_a.lng)
+ assert_equal @loc_a, Location.find_nearest(:origin =>[@loc_a.lat, @loc_a.lng])
end
def test_find_farthest
assert_equal @loc_e, Location.find_farthest(:origin => @loc_a)
end
def test_find_farthest_with_coordinates
- assert_equal @loc_e, Location.find_farthest(:lat => @loc_a.lat, :lng => @loc_a.lng)
+ assert_equal @loc_e, Location.find_farthest(:origin =>[@loc_a.lat, @loc_a.lng])
end
def test_scoped_distance_column_in_select
@@ -267,7 +267,7 @@ def test_find_within_with_custom
end
def test_find_within_with_coordinates_with_custom
- locations = CustomLocation.find_within(3.97, :lat => @loc_a.lat, :lng => @loc_a.lng)
+ locations = CustomLocation.find_within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
assert_equal 5, locations.size
end
@@ -287,7 +287,7 @@ def test_find_beyond_with_custom
end
def test_find_beyond_with_coordinates_with_custom
- locations = CustomLocation.find_beyond(3.95, :lat => @loc_a.lat, :lng => @loc_a.lng)
+ locations = CustomLocation.find_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
assert_equal 1, locations.size
end
@@ -296,15 +296,15 @@ def test_find_nearest_with_custom
end
def test_find_nearest_with_coordinates_with_custom
- assert_equal @custom_loc_a, CustomLocation.find_nearest(:lat => @loc_a.lat, :lng => @loc_a.lng)
+ assert_equal @custom_loc_a, CustomLocation.find_nearest(:origin =>[@loc_a.lat, @loc_a.lng])
end
def test_find_farthest_with_custom
assert_equal @custom_loc_e, CustomLocation.find_farthest(:origin => @loc_a)
end
def test_find_farthest_with_coordinates_with_custom
- assert_equal @custom_loc_e, CustomLocation.find_farthest(:lat => @loc_a.lat, :lng => @loc_a.lng)
+ assert_equal @custom_loc_e, CustomLocation.find_farthest(:origin =>[@loc_a.lat, @loc_a.lng])
end
def test_find_with_array_origin

0 comments on commit 38851d1

Please sign in to comment.