-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from geocrystal/bearing
add a method to calculate the initial bearing between two points
- Loading branch information
Showing
4 changed files
with
70 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require "../spec_helper" | ||
require "../../src/geo/bearing.cr" | ||
|
||
describe Geo::Coord do | ||
point_a = Geo::Coord.new(48.8566, 2.3522) # Paris | ||
point_b = Geo::Coord.new(40.7128, -74.0060) # New York | ||
|
||
context "bearing" do | ||
# Checked on https://www.movable-type.co.uk/scripts/latlong.html | ||
context "calculates initial bearing" do | ||
it { point_a.bearing(point_b).should eq(291.7938627483058) } | ||
end | ||
|
||
context "calculates final bearing" do | ||
it { point_a.bearing(point_b, true).should eq(233.70448129781204) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Geo | ||
struct Coord | ||
# The formula used for calculating the initial bearing between two points on the Earth's surfaceis | ||
# is derived from the broader concepts of great-circle distance and navigation on a spherical Earth model. | ||
# https://en.wikipedia.org/wiki/Great-circle_distance | ||
# | ||
# https://github.com/Turfjs/turf/blob/master/packages/turf-bearing/index.ts | ||
def bearing(to : Geo::Coord, final = false) : Float64 | ||
if final | ||
# Calculate the bearing from the destination point back to the original point | ||
reverse_bearing = calculate_bearing(to.lat, to.lng, lat, lng) | ||
|
||
# Adjust by 180 degrees to get the final bearing in the correct direction | ||
(reverse_bearing + 180) % 360 | ||
else | ||
calculate_bearing(lat, lng, to.lat, to.lng) | ||
end | ||
end | ||
|
||
private def calculate_bearing(lat1, lng1, lat2, lng2) : Float64 | ||
rad_lat1 = Geo::Utils.degrees_to_radians(lat1) | ||
rad_lng1 = Geo::Utils.degrees_to_radians(lng1) | ||
rad_lat2 = Geo::Utils.degrees_to_radians(lat2) | ||
rad_lng2 = Geo::Utils.degrees_to_radians(lng2) | ||
|
||
delta_lng = rad_lng2 - rad_lng1 | ||
|
||
a = Math.sin(delta_lng) * Math.cos(rad_lat2) | ||
b = Math.cos(rad_lat1) * Math.sin(rad_lat2) - | ||
Math.sin(rad_lat1) * Math.cos(rad_lat2) * Math.cos(delta_lng) | ||
|
||
Geo::Utils.radians_to_degrees(Math.atan2(a, b)) % 360 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters