-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5529cf6
Showing
12 changed files
with
253 additions
and
0 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,9 @@ | ||
root = true | ||
|
||
[*.cr] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 | ||
trim_trailing_whitespace = true |
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,9 @@ | ||
/docs/ | ||
/lib/ | ||
/bin/ | ||
/.shards/ | ||
*.dwarf | ||
|
||
# Libraries don't need dependency lock | ||
# Dependencies will be locked in applications that use them | ||
/shard.lock |
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,5 @@ | ||
language: crystal | ||
|
||
script: | ||
- crystal spec | ||
- crystal tool format --check |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2019 Anton Maminov | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,68 @@ | ||
# haversine | ||
|
||
Crystal implementation of the [Haversine formula](https://en.wikipedia.org/wiki/Haversine_formula) to calculate distances between two points given their latitudes and longitudes. | ||
|
||
## Installation | ||
|
||
1. Add the dependency to your `shard.yml`: | ||
|
||
```yaml | ||
dependencies: | ||
haversine: | ||
github: mamantoha/haversine | ||
``` | ||
2. Run `shards install` | ||
|
||
## Usage | ||
|
||
```crystal | ||
require "haversine" | ||
``` | ||
|
||
Calling `Haversine.distance` with four latitude/longitude coordinates returns a `Haversine::Distance` object which can provide output in kilometers, meters, miles, feet, or nautical miles. | ||
|
||
```crystal | ||
distance = Haversine.distance(35.61488, 139.5813, 48.85341, 2.3488) | ||
distance.to_kilometers # => 9715.470491159029 | ||
distance.to_meters # => 9715470.491159027 | ||
distance.to_miles # => 6032.710918698025 | ||
distance.to_feet # => 31852713.65072557 | ||
distance.to_nautical_miles # => 5242.2799481204265 | ||
``` | ||
|
||
If you have latitude/longitude pairs stored in an array, you can alternately provide two arrays when calling `Haversine.distance`: | ||
|
||
```crystal | ||
london = [51.500153, -0.126236] | ||
new_york = [40.714268, -74.005974] | ||
distance = Haversine.distance(new_york, london) | ||
distance.to_miles # => 5570474.459662069 | ||
``` | ||
|
||
Also you can compare `Haversine::Distance` objects: | ||
|
||
```crystal | ||
london = [51.500153, -0.126236] | ||
new_york = [40.714268, -74.005974] | ||
shanghai = [31.222220, 121.458060] | ||
distance1 = Haversine.distance(london, new_york) | ||
distance2 = Haversine.distance(london, shanghai) | ||
distance1 < distance2 # => true | ||
``` | ||
|
||
## Contributing | ||
|
||
1. Fork it (<https://github.com/mamantoha/haversine/fork>) | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create a new Pull Request | ||
|
||
## Contributors | ||
|
||
- [Anton Maminov](https://github.com/mamantoha) - creator and maintainer |
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,9 @@ | ||
name: haversine | ||
version: 0.1.0 | ||
|
||
authors: | ||
- Anton Maminov <[email protected]> | ||
|
||
crystal: 0.28.0 | ||
|
||
license: MIT |
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,19 @@ | ||
require "../spec_helper" | ||
|
||
describe Haversine::Distance do | ||
describe "<=>" do | ||
context "equality" do | ||
it "is true when the great circle distance is equal" do | ||
dist1 = Haversine::Distance.new(0) | ||
dist2 = Haversine::Distance.new(0) | ||
(dist1 == dist2).should be_true | ||
end | ||
|
||
it "is false when the great circle distance is not equal" do | ||
dist1 = Haversine::Distance.new(0) | ||
dist2 = Haversine::Distance.new(1) | ||
(dist1 == dist2).should be_false | ||
end | ||
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,32 @@ | ||
require "./spec_helper" | ||
|
||
describe Haversine do | ||
it "have version" do | ||
Haversine::VERSION.should_not be_nil | ||
end | ||
|
||
describe "#self.distance" do | ||
it "returns Haversine::Distance" do | ||
Haversine.distance(0, 0, 0, 0).should be_a(Haversine::Distance) | ||
Haversine.distance([0.0, 0.0], [0.0, 0.0]).should be_a(Haversine::Distance) | ||
end | ||
|
||
it "calculates the distance between the provided lat/lon pairs" do | ||
Haversine.distance(0, 0, 0, 0).great_circle_distance.should eq(0) | ||
Haversine.distance([0.0, 0.0], [0.0, 0.0]).great_circle_distance.should eq(0) | ||
end | ||
|
||
it "computes distances correctly" do | ||
new_york = [40.714268, -74.005974] | ||
london = [51.500153, -0.126236] | ||
|
||
dist = Haversine.distance(new_york, london) | ||
|
||
dist.to_kilometers.should eq(5570.4744596620685) | ||
dist.to_meters.should eq(5570474.459662069) | ||
dist.to_miles.should eq(3458.9227691764468) | ||
dist.to_nautical_miles.should eq(3005.720267276497) | ||
dist.to_feet.should eq(18263112.22125164) | ||
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,2 @@ | ||
require "spec" | ||
require "../src/haversine" |
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,36 @@ | ||
require "./haversine/*" | ||
|
||
# The haversine formula determines the great-circle distance between two points on a sphere | ||
# given their latitudes and longitudes. | ||
# | ||
# https://en.wikipedia.org/wiki/Haversine_formula | ||
module Haversine | ||
RAD_PER_DEG = Math::PI / 180 | ||
|
||
# Calculates the haversine distance between two locations using latitude and longitude. | ||
def self.distance(lat1 : Float64, lon1 : Float64, lat2 : Float64, lon2 : Float64) | ||
dlon = lon2 - lon1 | ||
dlat = lat2 - lat1 | ||
|
||
a = calc(dlat, lat1, lat2, dlon) | ||
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)) | ||
|
||
Haversine::Distance.new(c) | ||
end | ||
|
||
# ditto | ||
def self.distance(cord1 : Array(Float64), cord2 : Array(Float64)) | ||
lat1, lon1 = cord1 | ||
lat2, lon2 = cord2 | ||
|
||
distance(lat1, lon1, lat2, lon2) | ||
end | ||
|
||
private def self.calc(dlat, lat1, lat2, dlon) | ||
(Math.sin(rpd(dlat) / 2))**2 + Math.cos(rpd(lat1)) * Math.cos((rpd(lat2))) * (Math.sin(rpd(dlon) / 2))**2 | ||
end | ||
|
||
private def self.rpd(num) | ||
num * RAD_PER_DEG | ||
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,40 @@ | ||
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 | ||
|
||
property great_circle_distance | ||
|
||
def initialize(@great_circle_distance : Float64) | ||
end | ||
|
||
def to_kilometers | ||
@great_circle_distance * GREAT_CIRCLE_RADIUS_KILOMETERS | ||
end | ||
|
||
def to_meters | ||
@great_circle_distance * GREAT_CIRCLE_RADIUS_METERS | ||
end | ||
|
||
def to_miles | ||
@great_circle_distance * GREAT_CIRCLE_RADIUS_MILES | ||
end | ||
|
||
def to_nautical_miles | ||
@great_circle_distance * GREAT_CIRCLE_RADIUS_NAUTICAL_MILES | ||
end | ||
|
||
def to_feet | ||
@great_circle_distance * GREAT_CIRCLE_RADIUS_FEET | ||
end | ||
|
||
def <=>(other : Haversine::Distance) | ||
great_circle_distance <=> other.great_circle_distance | ||
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,3 @@ | ||
module Haversine | ||
VERSION = "0.1.0" | ||
end |