haversine_fast 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +27 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +7 -0
- data/ext/calc_haversine/calc_haversine.c +41 -0
- data/ext/calc_haversine/extconf.rb +2 -0
- data/haversine_fast.gemspec +37 -0
- data/lib/haversine_fast.rb +9 -0
- data/lib/haversine_fast/version.rb +3 -0
- data/spec/haversine_distance_spec.rb +49 -0
- data/spec/spec_helper.rb +14 -0
- metadata +161 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4abf62c56c93b58bdda0021578e9d554db0dd317
|
4
|
+
data.tar.gz: 73596d5a88494025edcc4c0cff72b329df64e56d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 68b72d9e87b7cdb25b0b4306bac87b77fdb9439cd8b10259b7df18d429b27f1abd954becd7968a78709eb0edfd1a950df9af646faa6003d0e52ab70532cda639
|
7
|
+
data.tar.gz: 9d63ae95b867a943e3809e454b9d44789dd355a3ced9bd9174529d1d86ed529e665ae681b08ac6b2dbd171eab72154f95a05d0287e03d17412110e1dd699c17b
|
data/.gitignore
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
#ignore coveragge
|
27
|
+
coverage/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Shaun Hubbard
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# HaversineFast
|
2
|
+
|
3
|
+
This takes advantage of C being faster at calculating double floating point values then say ruby. A project necessitated the haversine calculation frequently which would be sub optimal in ruby. There were gems that I found that did this which were either old (2+ years,) or they ran on ruby which defeated the purpose, or did a lot more then just calculate the minimum distance between two points on an ellipsoid. Either way this was a nice exercise to remove external dependencies and optimize a continuous calculation.
|
4
|
+
|
5
|
+
|
6
|
+
Per [wikipedia:](http://en.wikipedia.org/wiki/Haversine_formula)
|
7
|
+
|
8
|
+
|
9
|
+
<a href="http://www.codecogs.com/eqnedit.php?latex=haversine\left&space;(&space;\frac{d}{r}&space;\right&space;)&space;=&space;haversin\left&space;(&space;\phi&space;_{2}&space;-&space;\phi&space;_{1}&space;\right&space;)&space;+&space;cos\left&space;(&space;\phi&space;_{1}&space;\right&space;)cos\left&space;(&space;\phi&space;_{2}&space;\right&space;)haversin\left&space;(&space;\lambda&space;_{2}&space;-&space;\lambda&space;_{1}&space;\right&space;)" target="_blank"><img src="http://latex.codecogs.com/gif.latex?haversine\left&space;(&space;\frac{d}{r}&space;\right&space;)&space;=&space;haversin\left&space;(&space;\phi&space;_{2}&space;-&space;\phi&space;_{1}&space;\right&space;)&space;+&space;cos\left&space;(&space;\phi&space;_{1}&space;\right&space;)cos\left&space;(&space;\phi&space;_{2}&space;\right&space;)haversin\left&space;(&space;\lambda&space;_{2}&space;-&space;\lambda&space;_{1}&space;\right&space;)" title="haversine\left ( \frac{d}{r} \right ) = haversin\left ( \phi _{2} - \phi _{1} \right ) + cos\left ( \phi _{1} \right )cos\left ( \phi _{2} \right )haversin\left ( \lambda _{2} - \lambda _{1} \right )" /></a>
|
10
|
+
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'haversine_fast'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install haversine_fast
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it ( https://github.com/[my-github-username]/haversine_fast/fork )
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include <math.h>
|
3
|
+
|
4
|
+
void Init_calc_haversine();
|
5
|
+
|
6
|
+
//define the following constants
|
7
|
+
//radius of the earth, POLAR
|
8
|
+
static const double RADIUS_OF_EARTH = 6335.592;
|
9
|
+
|
10
|
+
//deg2rad
|
11
|
+
static const double DEG2RAD = M_PI / 180;
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
//ruby wants to call
|
16
|
+
//calculate_distance_by_haversine(lon1, lat1, lon2, lat2)
|
17
|
+
|
18
|
+
#define NUM2RADDBL(x) (NUM2DBL(x) * DEG2RAD)
|
19
|
+
|
20
|
+
static VALUE calculate_distance_by_haversine(VALUE self, VALUE lat1, VALUE lon1, VALUE lat2, VALUE lon2){
|
21
|
+
|
22
|
+
double dLat1 = NUM2RADDBL(lat1),
|
23
|
+
dLon1 = NUM2RADDBL(lon1),
|
24
|
+
dLat2 = NUM2RADDBL(lat2),
|
25
|
+
dLon2 = NUM2RADDBL(lon2),
|
26
|
+
result;
|
27
|
+
|
28
|
+
result = 2.0 * RADIUS_OF_EARTH *
|
29
|
+
asin(sqrt(
|
30
|
+
(1-cos(dLat2-dLat1))/2 +
|
31
|
+
cos(dLat1)*cos(dLat2) * (1-cos(dLon2-dLon1))/2
|
32
|
+
));
|
33
|
+
|
34
|
+
return rb_float_new(result);
|
35
|
+
}
|
36
|
+
|
37
|
+
void Init_calc_haversine(){
|
38
|
+
VALUE mHaversine = rb_define_module("HaversineFast");
|
39
|
+
rb_define_singleton_method(mHaversine, "calculate_distance_by_haversine", calculate_distance_by_haversine, 4);
|
40
|
+
}
|
41
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'haversine_fast/version'
|
5
|
+
|
6
|
+
gem_spec = Gem::Specification.new do |spec|
|
7
|
+
spec.name = "haversine_fast"
|
8
|
+
spec.version = HaversineFast::VERSION
|
9
|
+
spec.authors = ["Shaun Hubbard", "John F. Hogarty"]
|
10
|
+
spec.email = ["shaunhubbard2013@icloud.com", "hogihung@gmail.com"]
|
11
|
+
spec.summary = %q{A reasonable way to calculate the distance between two geographic locations given longitude and latitude for each.}
|
12
|
+
spec.description = %q{ This calculates distance between two parts of the earth via Haversine formula at the speed of C}
|
13
|
+
spec.homepage = "https://github.com/pedantix/haversine_fast"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.00"
|
25
|
+
spec.add_development_dependency "simplecov"
|
26
|
+
|
27
|
+
spec.add_development_dependency "pry"
|
28
|
+
spec.add_development_dependency "pry-awesome_print"
|
29
|
+
|
30
|
+
|
31
|
+
#for C extension
|
32
|
+
spec.add_development_dependency "rake-compiler"
|
33
|
+
|
34
|
+
spec.extensions = "ext/calc_haversine/extconf.rb"
|
35
|
+
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
class TestLocation
|
5
|
+
attr_reader :longitude, :latitude, :description
|
6
|
+
|
7
|
+
def initialize(lat, lon, desc)
|
8
|
+
@longitude, @latitude, @description = lon, lat, desc
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
RSpec.describe HaversineFast do
|
14
|
+
|
15
|
+
let(:jax_residence) { TestLocation.new(30.199647, -81.524879, "Jacksonville Residence") }
|
16
|
+
let(:altamonte_springs_residence) { TestLocation.new(28.653973, -81.406500, "Altamonte Springs Residence")}
|
17
|
+
let(:jax_residence_2) { TestLocation.new(30.213115,-81.5738050, "Jacksonville Residence 2" ) }
|
18
|
+
|
19
|
+
let(:hong_kong_residence) { TestLocation.new(22.396428, 114.109497, "Hong Kong") }
|
20
|
+
|
21
|
+
let(:tolerance) { 1.00 }
|
22
|
+
|
23
|
+
it { expect(subject).to respond_to(:calc_distance) }
|
24
|
+
|
25
|
+
describe "should result in reasonably accurate distance calculations" do
|
26
|
+
#smoke test
|
27
|
+
def distance_between(loc1, loc2)
|
28
|
+
HaversineFast.calc_distance(loc1.latitude, loc1.longitude, loc2.latitude, loc2.longitude)
|
29
|
+
end
|
30
|
+
|
31
|
+
it { expect(HaversineFast.calc_distance(0.00,0.00,0.00,0.00)).to eql 0.00 }
|
32
|
+
|
33
|
+
|
34
|
+
it "should indicate there are 4.94 km between the two jax residence" do
|
35
|
+
expect(distance_between(jax_residence, jax_residence_2)).to be_within(tolerance).of(4.94)
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
it "should indicate there are 171.71 km between the two jax residence" do
|
40
|
+
expect(distance_between(jax_residence, altamonte_springs_residence)).to be_within(tolerance).of(171.71)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should indicate there are 13950.72km between jax residence and hong kong residence" do
|
44
|
+
expect(distance_between(jax_residence, hong_kong_residence)).to be_within(tolerance * 100).of(13950.72)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: haversine_fast
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shaun Hubbard
|
8
|
+
- John F. Hogarty
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-06-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.6'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.6'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '3.00'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '3.00'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: simplecov
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: pry
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: pry-awesome_print
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: rake-compiler
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
description: " This calculates distance between two parts of the earth via Haversine
|
113
|
+
formula at the speed of C"
|
114
|
+
email:
|
115
|
+
- shaunhubbard2013@icloud.com
|
116
|
+
- hogihung@gmail.com
|
117
|
+
executables: []
|
118
|
+
extensions:
|
119
|
+
- ext/calc_haversine/extconf.rb
|
120
|
+
extra_rdoc_files: []
|
121
|
+
files:
|
122
|
+
- ".gitignore"
|
123
|
+
- Gemfile
|
124
|
+
- LICENSE.txt
|
125
|
+
- README.md
|
126
|
+
- Rakefile
|
127
|
+
- ext/calc_haversine/calc_haversine.c
|
128
|
+
- ext/calc_haversine/extconf.rb
|
129
|
+
- haversine_fast.gemspec
|
130
|
+
- lib/haversine_fast.rb
|
131
|
+
- lib/haversine_fast/version.rb
|
132
|
+
- spec/haversine_distance_spec.rb
|
133
|
+
- spec/spec_helper.rb
|
134
|
+
homepage: https://github.com/pedantix/haversine_fast
|
135
|
+
licenses:
|
136
|
+
- MIT
|
137
|
+
metadata: {}
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
requirements: []
|
153
|
+
rubyforge_project:
|
154
|
+
rubygems_version: 2.2.2
|
155
|
+
signing_key:
|
156
|
+
specification_version: 4
|
157
|
+
summary: A reasonable way to calculate the distance between two geographic locations
|
158
|
+
given longitude and latitude for each.
|
159
|
+
test_files:
|
160
|
+
- spec/haversine_distance_spec.rb
|
161
|
+
- spec/spec_helper.rb
|