Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set Earth radius to 6371008.8 #2

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading