haversine_distance 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README +57 -0
- data/README.markdown +57 -0
- data/Rakefile +9 -0
- data/haversine.gemspec +24 -0
- data/lib/haversine.rb +60 -0
- data/lib/haversine/version.rb +3 -0
- data/test/test_haversine.rb +29 -0
- metadata +56 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
Haversine is a formula to calculate the great-circle distance between two points
|
2
|
+
================================================================================
|
3
|
+
|
4
|
+
Haversine is Ruby library for haversine a formula to calculate distance between
|
5
|
+
two GEO Location. Because Earth is not flat!
|
6
|
+
|
7
|
+
References
|
8
|
+
-------------------------------------------------------
|
9
|
+
Inspired by Landon Cox at http://www.esawdust.com/blog/gps/files/HaversineFormulaInRuby.html
|
10
|
+
And http://www.movable-type.co.uk/scripts/latlong.html
|
11
|
+
|
12
|
+
You can totally install it as a Gem
|
13
|
+
-----------------------------------
|
14
|
+
|
15
|
+
Haversine is readily available as a Ruby gem.
|
16
|
+
|
17
|
+
$ [sudo] gem install haversine
|
18
|
+
|
19
|
+
The Haversine source is available at GitHub:
|
20
|
+
|
21
|
+
$ git clone git://github.com/jirapong/haversine.git
|
22
|
+
|
23
|
+
Feedback & Patch are welcome.
|
24
|
+
|
25
|
+
And how to use
|
26
|
+
------------------------------------
|
27
|
+
require 'haversine'
|
28
|
+
|
29
|
+
lat_a = 51.885
|
30
|
+
lng_a = 0.235
|
31
|
+
|
32
|
+
lat_b = 49.008
|
33
|
+
lng_b = 2.549
|
34
|
+
|
35
|
+
km = Haversine.distance(lat_a, lng_a, lat_b, lng_b);
|
36
|
+
|
37
|
+
Credits
|
38
|
+
-------------------------------------------------------
|
39
|
+
This library is written by Jirapong Nanta (@jirapong)
|
40
|
+
|
41
|
+
Boring legal stuff
|
42
|
+
------------------
|
43
|
+
|
44
|
+
Copyright (c) 2011, Jirapong Nanta
|
45
|
+
|
46
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
47
|
+
purpose with or without fee is hereby granted, provided that the above
|
48
|
+
copyright notice and this permission notice appear in all copies.
|
49
|
+
|
50
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
51
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
52
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
53
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
54
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
55
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
56
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
57
|
+
|
data/README.markdown
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
Haversine is a formula to calculate the great-circle distance between two points
|
2
|
+
================================================================================
|
3
|
+
|
4
|
+
Haversine is Ruby library for haversine a formula to calculate distance between
|
5
|
+
two GEO Location. Because Earth is not flat!
|
6
|
+
|
7
|
+
References
|
8
|
+
-------------------------------------------------------
|
9
|
+
Inspired by Landon Cox at http://www.esawdust.com/blog/gps/files/HaversineFormulaInRuby.html
|
10
|
+
And http://www.movable-type.co.uk/scripts/latlong.html
|
11
|
+
|
12
|
+
You can totally install it as a Gem
|
13
|
+
-----------------------------------
|
14
|
+
|
15
|
+
Haversine is readily available as a Ruby gem.
|
16
|
+
|
17
|
+
$ [sudo] gem install haversine
|
18
|
+
|
19
|
+
The Haversine source is available at GitHub:
|
20
|
+
|
21
|
+
$ git clone git://github.com/jirapong/haversine.git
|
22
|
+
|
23
|
+
Feedback & Patch are welcome.
|
24
|
+
|
25
|
+
And how to use
|
26
|
+
------------------------------------
|
27
|
+
require 'haversine'
|
28
|
+
|
29
|
+
lat_a = 51.885
|
30
|
+
lng_a = 0.235
|
31
|
+
|
32
|
+
lat_b = 49.008
|
33
|
+
lng_b = 2.549
|
34
|
+
|
35
|
+
km = Haversine.distance(lat_a, lng_a, lat_b, lng_b);
|
36
|
+
|
37
|
+
Credits
|
38
|
+
-------------------------------------------------------
|
39
|
+
This library is written by Jirapong Nanta (@jirapong)
|
40
|
+
|
41
|
+
Boring legal stuff
|
42
|
+
------------------
|
43
|
+
|
44
|
+
Copyright (c) 2011, Jirapong Nanta
|
45
|
+
|
46
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
47
|
+
purpose with or without fee is hereby granted, provided that the above
|
48
|
+
copyright notice and this permission notice appear in all copies.
|
49
|
+
|
50
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
51
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
52
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
53
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
54
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
55
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
56
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
57
|
+
|
data/Rakefile
ADDED
data/haversine.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "haversine/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "haversine_distance"
|
7
|
+
s.version = Haversine::VERSION
|
8
|
+
s.authors = ["Jirapong Nanta"]
|
9
|
+
s.email = ["jirapong.nanta@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/Jirapong/Haversine"
|
11
|
+
s.summary = "Haversine is a formula to calculate the great-circle distance between two points"
|
12
|
+
s.description = "Because Earth is not flat!"
|
13
|
+
|
14
|
+
s.rubyforge_project = "haversine"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
data/lib/haversine.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Copyright (C) 2011, Jirapong Nanta
|
2
|
+
# http://bananacoding.com
|
3
|
+
|
4
|
+
|
5
|
+
require "haversine/version"
|
6
|
+
|
7
|
+
class Float
|
8
|
+
RADIAN_PER_DEGREE = Math::PI / 180.0
|
9
|
+
|
10
|
+
def toRad
|
11
|
+
return self * RADIAN_PER_DEGREE
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module Haversine
|
16
|
+
EARTH_RADIUS_MI = 3963.19
|
17
|
+
EARTH_RADIUS_KM = 6371.00
|
18
|
+
|
19
|
+
# Calculate the great-circle distance between two points in kilometers (km.)
|
20
|
+
#
|
21
|
+
# Example:
|
22
|
+
# >> Haversine.distance(51.885, 0.235, 49.008, 2.549)
|
23
|
+
# => 359.3823602146921
|
24
|
+
#
|
25
|
+
# Arguments:
|
26
|
+
# lat_a: (Float)
|
27
|
+
# lng_a: (Float)
|
28
|
+
# lat_b: (Float)
|
29
|
+
# lng_b: (Float)
|
30
|
+
def self.distance(lat_a, lng_a, lat_b, lng_b)
|
31
|
+
return self.distance_in_radius(lat_a, lng_a, lat_b, lng_b) * EARTH_RADIUS_KM
|
32
|
+
end
|
33
|
+
|
34
|
+
# Calculate the great-circle distance between two points in miles (mi.)
|
35
|
+
#
|
36
|
+
# Example:
|
37
|
+
# >> Haversine.distance_in_mile(51.885, 0.235, 49.008, 2.549)
|
38
|
+
# => 223.5599711472713
|
39
|
+
#
|
40
|
+
# Arguments:
|
41
|
+
# lat_a: (Float)
|
42
|
+
# lng_a: (Float)
|
43
|
+
# lat_b: (Float)
|
44
|
+
# lng_b: (Float)
|
45
|
+
def self.distance_in_mile(lat_a, lng_a, lat_b, lng_b)
|
46
|
+
return self.distance_in_radius(lat_a, lng_a, lat_b, lng_b) * EARTH_RADIUS_MI
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def self.distance_in_radius(lat_a, lng_a, lat_b, lng_b)
|
52
|
+
d_lat = (lat_a - lat_b).toRad;
|
53
|
+
d_lng = (lng_a - lng_b).toRad;
|
54
|
+
|
55
|
+
a = Math.sin(d_lat/2)**2 + Math.cos(lat_a.toRad) * Math.cos(lat_b.toRad) * Math.sin(d_lng/2) ** 2
|
56
|
+
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
|
57
|
+
|
58
|
+
return c
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "haversine"
|
3
|
+
# testing against with http://www.movable-type.co.uk/scripts/latlong.html
|
4
|
+
|
5
|
+
class HaversineTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_distance_in_km
|
8
|
+
lat_a = 51.885
|
9
|
+
lng_a = 0.235
|
10
|
+
|
11
|
+
lat_b = 49.008
|
12
|
+
lng_b = 2.549
|
13
|
+
|
14
|
+
km = Haversine.distance(lat_a, lng_a, lat_b, lng_b);
|
15
|
+
assert_equal(359.3823602146921, km);
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_distance_in_mi
|
19
|
+
lat_a = 51.885
|
20
|
+
lng_a = 0.235
|
21
|
+
|
22
|
+
lat_b = 49.008
|
23
|
+
lng_b = 2.549
|
24
|
+
|
25
|
+
mi = Haversine.distance_in_mile(lat_a, lng_a, lat_b, lng_b);
|
26
|
+
assert_equal(223.5599711472713, mi);
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: haversine_distance
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jirapong Nanta
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-03 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Because Earth is not flat!
|
15
|
+
email:
|
16
|
+
- jirapong.nanta@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- README
|
24
|
+
- README.markdown
|
25
|
+
- Rakefile
|
26
|
+
- haversine.gemspec
|
27
|
+
- lib/haversine.rb
|
28
|
+
- lib/haversine/version.rb
|
29
|
+
- test/test_haversine.rb
|
30
|
+
homepage: https://github.com/Jirapong/Haversine
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project: haversine
|
50
|
+
rubygems_version: 1.8.10
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Haversine is a formula to calculate the great-circle distance between two
|
54
|
+
points
|
55
|
+
test_files:
|
56
|
+
- test/test_haversine.rb
|