Skip to content

Commit

Permalink
Merge pull request #2 from geocrystal/adjust-earth-radius
Browse files Browse the repository at this point in the history
set Earth radius to 6371008.8
  • Loading branch information
mamantoha authored Apr 3, 2024
2 parents f851447 + f91cbb5 commit 36d34ca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
16 changes: 8 additions & 8 deletions spec/haversine_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ describe Haversine do
end

describe "calculates the distance between the provided lat/lon pairs" do
it { Haversine.distance(0, 0, 0, 0).great_circle_distance.should eq(0) }
it { Haversine.distance([0.0, 0.0], [0.0, 0.0]).great_circle_distance.should eq(0) }
it { Haversine.distance({0.0, 0.0}, {0.0, 0.0}).great_circle_distance.should eq(0) }
it { Haversine.distance(0, 0, 0, 0).distance.should eq(0) }
it { Haversine.distance([0.0, 0.0], [0.0, 0.0]).distance.should eq(0) }
it { Haversine.distance({0.0, 0.0}, {0.0, 0.0}).distance.should eq(0) }
end

describe "computes distances correctly" do
Expand All @@ -24,11 +24,11 @@ describe Haversine do

dist = Haversine.distance(new_york, london)

it { dist.to_kilometers.should eq(5570.4744596620685) }
it { dist.to_meters.should eq(5570474.459662069) }
it { dist.to_miles.should eq(3458.9227691764468) }
it { dist.to_nautical_miles.should eq(3005.720267276497) }
it { dist.to_feet.should eq(18263112.22125164) }
it { dist.to_kilometers.should eq(5570.482153929098) }
it { dist.to_meters.should eq(5570482.153929098) }
it { dist.to_miles.should eq(3461.3371373237155) }
it { dist.to_nautical_miles.should eq(3007.8197375427094) }
it { dist.to_feet.should eq(18275860.669896744) }
end
end
end
42 changes: 16 additions & 26 deletions src/haversine/distance.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,29 @@ module Haversine
class Distance
include Comparable(self)

GREAT_CIRCLE_RADIUS_KILOMETERS = 6371
GREAT_CIRCLE_RADIUS_METERS = GREAT_CIRCLE_RADIUS_KILOMETERS * 1000
GREAT_CIRCLE_RADIUS_MILES = 3956
GREAT_CIRCLE_RADIUS_FEET = GREAT_CIRCLE_RADIUS_MILES * 5280
GREAT_CIRCLE_RADIUS_NAUTICAL_MILES = GREAT_CIRCLE_RADIUS_MILES / 1.15078
EARTH_RADIUS = 6371008.8

property great_circle_distance
FACTORS = {
kilometers: EARTH_RADIUS / 1000,
meters: EARTH_RADIUS,
miles: EARTH_RADIUS / 1609.344,
feet: EARTH_RADIUS * 3.28084,
nautical_miles: EARTH_RADIUS / 1852,
}

def initialize(@great_circle_distance : Number)
end

def to_kilometers : Number
@great_circle_distance * GREAT_CIRCLE_RADIUS_KILOMETERS
end

def to_meters : Number
@great_circle_distance * GREAT_CIRCLE_RADIUS_METERS
end

def to_miles : Number
@great_circle_distance * GREAT_CIRCLE_RADIUS_MILES
end
property distance

def to_nautical_miles : Number
@great_circle_distance * GREAT_CIRCLE_RADIUS_NAUTICAL_MILES
def initialize(@distance : Number)
end

def to_feet : Number
@great_circle_distance * GREAT_CIRCLE_RADIUS_FEET
end
{% for factor in ["meters", "kilometers", "miles", "nautical_miles", "feet"] %}
def to_{{factor.id}} : Number
@distance * FACTORS[:{{factor.id}}]
end
{% end %}

def <=>(other : Haversine::Distance)
great_circle_distance <=> other.great_circle_distance
distance <=> other.distance
end
end
end

0 comments on commit 36d34ca

Please sign in to comment.