Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed May 8, 2019
0 parents commit 5529cf6
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
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
9 changes: 9 additions & 0 deletions .gitignore
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
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: crystal

script:
- crystal spec
- crystal tool format --check
21 changes: 21 additions & 0 deletions LICENSE
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.
68 changes: 68 additions & 0 deletions README.md
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
9 changes: 9 additions & 0 deletions shard.yml
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
19 changes: 19 additions & 0 deletions spec/haversine/distance_spec.cr
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
32 changes: 32 additions & 0 deletions spec/haversine_spec.cr
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
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "spec"
require "../src/haversine"
36 changes: 36 additions & 0 deletions src/haversine.cr
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
40 changes: 40 additions & 0 deletions src/haversine/distance.cr
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
3 changes: 3 additions & 0 deletions src/haversine/version.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Haversine
VERSION = "0.1.0"
end

0 comments on commit 5529cf6

Please sign in to comment.