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

Add calculations for distances btwn lat/lon lines.

parent 03b8f247
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,21 @@ module Geocoder ...@@ -21,6 +21,21 @@ module Geocoder
# #
KM_IN_MI = 0.621371192 KM_IN_MI = 0.621371192
##
# Calculate the distance between latitude lines in the given units.
#
def distance_between_latitude_lines(units = :mi)
2 * Math::PI * earth_radius(units) / 360
end
##
# Calculate the distance between longitude lines at the given latitude.
# This ranges from around 69 miles at the equator to zero at the poles.
#
def distance_between_longitude_lines(latitude, units = :mi)
distance_between_latitude_lines(units) * Math.cos(to_radians(latitude))
end
## ##
# Calculate the distance between two points on Earth (Haversine formula). # Calculate the distance between two points on Earth (Haversine formula).
# Takes two sets of coordinates and an options hash: # Takes two sets of coordinates and an options hash:
......
...@@ -139,6 +139,18 @@ class GeocoderTest < Test::Unit::TestCase ...@@ -139,6 +139,18 @@ class GeocoderTest < Test::Unit::TestCase
# --- calcluations --- # --- calcluations ---
def test_distance_between_longitude_lines_at_equator
assert_equal 69, Geocoder::Calculations.distance_between_longitude_lines(0).round
end
def test_distance_between_longitude_lines_at_new_york
assert_equal 53, Geocoder::Calculations.distance_between_longitude_lines(40).round
end
def test_distance_between_longitude_lines_at_north_pole
assert_equal 0, Geocoder::Calculations.distance_between_longitude_lines(89.98).round
end
def test_distance_between def test_distance_between
assert_equal 69, Geocoder::Calculations.distance_between(0,0, 0,1).round assert_equal 69, Geocoder::Calculations.distance_between(0,0, 0,1).round
la_to_ny = Geocoder::Calculations.distance_between(34.05,-118.25, 40.72,-74).round la_to_ny = Geocoder::Calculations.distance_between(34.05,-118.25, 40.72,-74).round
......
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