fast_haversine 0.0.1
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 +22 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +66 -0
- data/Rakefile +12 -0
- data/ext/fast_haversine/extconf.rb +3 -0
- data/ext/fast_haversine/fast_haversine.c +56 -0
- data/fast_haversine.gemspec +28 -0
- data/lib/fast_haversine.rb +13 -0
- data/lib/fast_haversine/version.rb +3 -0
- data/spec/fast_haversine_spec.rb +21 -0
- data/spec/spec_helper.rb +13 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b07e617d5658471c451bf76a4159b4298ccafc0d
|
4
|
+
data.tar.gz: a4edd395979df2f93ee0e012ae111a3cc4541a7a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d56650f34068657a82473fcbde138273fb4ac46393f9ff50c4f547ec6a8276d3150b56738c2c4c2b58515a2ebca6b1a857b66f51771490c362d56241c80ec42a
|
7
|
+
data.tar.gz: 13f021381630c56c4e2a5aaa098fc2fff30f09acb30c848c96aeba613b0a6e9f42e8fb15a3c50ace2437e81dfcd2f06972f6bdc07f47e597d1598e363e77677d
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
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
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Paul Sorensen
|
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,66 @@
|
|
1
|
+
# FastHaversine
|
2
|
+
[](https://travis-ci.org/paulnsorensen/fast_haversine)
|
3
|
+
[](https://codeclimate.com/github/paulnsorensen/fast_haversine)
|
4
|
+
|
5
|
+
Haversine calculation written in Ruby that supports
|
6
|
+
distances calculations in both miles and kilometers
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'fast_haversine'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install fast_haversine
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
For kilometers:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
distance = FastHaversine.distance_between([lat1, lon1], [lat2, lon2], :km)
|
28
|
+
```
|
29
|
+
|
30
|
+
For miles:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
distance = FastHaversine.distance_between([lat1, lon1], [lat2, lon2], :mi)
|
34
|
+
```
|
35
|
+
|
36
|
+
## Benchmark
|
37
|
+
|
38
|
+
During specs, I ran the following benchmark:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
1_000_000.times { FastHaversine.distance_between(sf, chi, :mi) }
|
42
|
+
|
43
|
+
0.700000 0.000000 0.700000 ( 0.700143)
|
44
|
+
```
|
45
|
+
|
46
|
+
This is much faster than a common ruby implementation:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
1_000_000.times do
|
50
|
+
RubyHaversine.distance_between(
|
51
|
+
[37.7833, -122.4167],
|
52
|
+
[41.8819, -87.6278],
|
53
|
+
units: :mi
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
7.510000 0.320000 7.830000 ( 7.930863)
|
58
|
+
```
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
1. Fork it ( https://github.com/paulnsorensen/fast_haversine/fork )
|
63
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
64
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
65
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
66
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require 'rake/extensiontask'
|
4
|
+
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
task default: :spec
|
7
|
+
|
8
|
+
Rake::ExtensionTask.new('fast_haversine') do |ext|
|
9
|
+
ext.lib_dir = 'lib/fast_haversine'
|
10
|
+
end
|
11
|
+
|
12
|
+
Rake::Task[:spec].prerequisites << :compile
|
@@ -0,0 +1,56 @@
|
|
1
|
+
// Copyright 2014 Paul Sorensen <paulnsorensen@gmail.com>
|
2
|
+
|
3
|
+
#include <ruby.h>
|
4
|
+
#include <math.h>
|
5
|
+
|
6
|
+
static const double EARTH_RADIUS_IN_KM = 6371.0;
|
7
|
+
static const double EARTH_RADIUS_IN_MI = 3958.755864232;
|
8
|
+
static const double RADIAN_PER_DEGREE = M_PI / 180.0;
|
9
|
+
|
10
|
+
static double haversine_c(
|
11
|
+
double lat1,
|
12
|
+
double lon1,
|
13
|
+
double lat2,
|
14
|
+
double lon2) {
|
15
|
+
double lon_delta = (lon2 - lon1) * RADIAN_PER_DEGREE;
|
16
|
+
double lat_delta = (lat2 - lat1) * RADIAN_PER_DEGREE;
|
17
|
+
double lat1_rad = lat1 * RADIAN_PER_DEGREE;
|
18
|
+
double lat2_rad = lat2 * RADIAN_PER_DEGREE;
|
19
|
+
double a = pow(sin(lat_delta * 0.5), 2) +
|
20
|
+
cos(lat1_rad) * cos(lat2_rad) * pow(sin(lon_delta * 0.5), 2);
|
21
|
+
return 2.0 * atan2(sqrt(a), sqrt(1 - a));
|
22
|
+
}
|
23
|
+
|
24
|
+
static VALUE rb_distance_in_mi(
|
25
|
+
VALUE self,
|
26
|
+
VALUE lat1,
|
27
|
+
VALUE lon1,
|
28
|
+
VALUE lat2,
|
29
|
+
VALUE lon2) {
|
30
|
+
double c = haversine_c(
|
31
|
+
NUM2DBL(lat1),
|
32
|
+
NUM2DBL(lon1),
|
33
|
+
NUM2DBL(lat2),
|
34
|
+
NUM2DBL(lon2));
|
35
|
+
return rb_float_new(c * EARTH_RADIUS_IN_MI);
|
36
|
+
}
|
37
|
+
|
38
|
+
static VALUE rb_distance_in_km(
|
39
|
+
VALUE self,
|
40
|
+
VALUE lat1,
|
41
|
+
VALUE lon1,
|
42
|
+
VALUE lat2,
|
43
|
+
VALUE lon2) {
|
44
|
+
double c = haversine_c(
|
45
|
+
NUM2DBL(lat1),
|
46
|
+
NUM2DBL(lon1),
|
47
|
+
NUM2DBL(lat2),
|
48
|
+
NUM2DBL(lon2));
|
49
|
+
return rb_float_new(c * EARTH_RADIUS_IN_KM);
|
50
|
+
}
|
51
|
+
|
52
|
+
void Init_fast_haversine() {
|
53
|
+
VALUE module = rb_define_module("FastHaversine");
|
54
|
+
rb_define_singleton_method(module, "distance_in_mi", rb_distance_in_mi, 4);
|
55
|
+
rb_define_singleton_method(module, "distance_in_km", rb_distance_in_km, 4);
|
56
|
+
}
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fast_haversine/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'fast_haversine'
|
8
|
+
spec.version = FastHaversine::VERSION
|
9
|
+
spec.authors = ['Paul Sorensen']
|
10
|
+
spec.email = ['paulnsorensen@gmail.com']
|
11
|
+
spec.summary = 'Ruby Haversine Calculation with C extension'
|
12
|
+
spec.description = 'Haversine calculation written in Ruby that supports both
|
13
|
+
distances in miles and kilometers'
|
14
|
+
spec.homepage = 'https://github.com/paulnsorensen/fast_haversine'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
spec.extensions = ['ext/fast_haversine/extconf.rb']
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
spec.add_development_dependency 'rspec'
|
26
|
+
spec.add_development_dependency 'pry'
|
27
|
+
spec.add_development_dependency 'rake-compiler'
|
28
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'fast_haversine/version'
|
2
|
+
require 'fast_haversine/fast_haversine'
|
3
|
+
|
4
|
+
module FastHaversine
|
5
|
+
def self.distance_between(point_a, point_b, units)
|
6
|
+
case units
|
7
|
+
when :km
|
8
|
+
return distance_in_km(point_a[0], point_a[1], point_b[0], point_b[1])
|
9
|
+
when :mi
|
10
|
+
return distance_in_mi(point_a[0], point_a[1], point_b[0], point_b[1])
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'lib/fast_haversine'
|
3
|
+
|
4
|
+
describe FastHaversine do
|
5
|
+
describe '.distance_between' do
|
6
|
+
let(:sf) { [37.7833, -122.4167] }
|
7
|
+
let(:chi) { [41.8819, -87.6278] }
|
8
|
+
|
9
|
+
it 'calculates distance in miles' do
|
10
|
+
distance = FastHaversine.distance_between(sf, chi, :mi)
|
11
|
+
|
12
|
+
expect(distance.round(12)).to eql(1854.489697974166)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'calculates distance in kilometers' do
|
16
|
+
distance = FastHaversine.distance_between(sf, chi, :km)
|
17
|
+
|
18
|
+
expect(distance.round(12)).to eql(2984.511869636476)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fast_haversine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul Sorensen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake-compiler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: |-
|
84
|
+
Haversine calculation written in Ruby that supports both
|
85
|
+
distances in miles and kilometers
|
86
|
+
email:
|
87
|
+
- paulnsorensen@gmail.com
|
88
|
+
executables: []
|
89
|
+
extensions:
|
90
|
+
- ext/fast_haversine/extconf.rb
|
91
|
+
extra_rdoc_files: []
|
92
|
+
files:
|
93
|
+
- ".gitignore"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- ext/fast_haversine/extconf.rb
|
100
|
+
- ext/fast_haversine/fast_haversine.c
|
101
|
+
- fast_haversine.gemspec
|
102
|
+
- lib/fast_haversine.rb
|
103
|
+
- lib/fast_haversine/version.rb
|
104
|
+
- spec/fast_haversine_spec.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
homepage: https://github.com/paulnsorensen/fast_haversine
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.2.2
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Ruby Haversine Calculation with C extension
|
130
|
+
test_files:
|
131
|
+
- spec/fast_haversine_spec.rb
|
132
|
+
- spec/spec_helper.rb
|