haversine_distance_c 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 903489d9dc23644af74b57ffad89a61d7fddbdfb
4
+ data.tar.gz: 775349207a5278f09939ce990301dadff6816678
5
+ SHA512:
6
+ metadata.gz: 7ccaa6456f64356905c42bf263a993e3346808afdadd9bba362e76dcc244c19598ce29bffb784b20a16051897f7439122c233e0d63953ae212d90f95ffc05513
7
+ data.tar.gz: 37c99cee94179366680d4c2c577fc2a588c38d2d187bd7d4bdbb79e270d0184e3b43c07a2e243cda2c93ce423701baf13b98d521b71954e56d004e42ad7676ea
@@ -0,0 +1,5 @@
1
+ tmp
2
+ .DS_Store
3
+ .byebug_history
4
+ *.gem
5
+ *.bundle
@@ -0,0 +1,19 @@
1
+ # Haversine distance
2
+
3
+ Just a simple gem to compute Haversine Distance in C
4
+
5
+
6
+ ## Usage
7
+
8
+ Add to your Gemfile:
9
+
10
+ ```
11
+ gem 'haversine_c', '~> 0.0.2'
12
+ ```
13
+
14
+ Then:
15
+
16
+ ```
17
+ meters = HaversineDistance.m(point_a.lat,point_a.long,point_b.lat,point_b.long)
18
+ km = HaversineDistance.km(point_a.lat,point_a.long,point_b.lat,point_b.long)
19
+ ```
@@ -0,0 +1,76 @@
1
+ require './lib/haversine_distance/version'
2
+ require 'fileutils'
3
+ require 'English'
4
+
5
+ task :config do
6
+ def version
7
+ HaversineDistance::VERSION
8
+ end
9
+ # e.g. 2.13.3 becomes HaversineDistance__2-13-3
10
+ def version_tag
11
+ "HaversineDistance_#{version.gsub('.','-')}"
12
+ end
13
+ # e.g. 2.13.3 becomes haversine_distance_-2.13.3.gem
14
+ def gem_name
15
+ "haversine_distance_c-#{version}.gem"
16
+ end
17
+
18
+ def base
19
+ File.expand_path('..', __FILE__)
20
+ end
21
+ end
22
+
23
+ desc 'abort when repo is not clean or has uncommited code'
24
+ task :assert_clean_repo do
25
+ sh('git diff --exit-code')
26
+ abort 'Git repo not clean' unless $CHILD_STATUS.success?
27
+ sh('git diff-index --quiet --cached HEAD')
28
+ abort 'Git repo not commited' unless $CHILD_STATUS.success?
29
+ end
30
+
31
+ desc 'build gem'
32
+ task :build => [:config] do
33
+ sh 'gem build -V haversine_distance_c.gemspec'
34
+ if $CHILD_STATUS.success?
35
+ FileUtils.mkdir_p(File.join(base, 'pkg'))
36
+ FileUtils.mv(File.join(base, gem_name), 'pkg')
37
+ else
38
+ STDERR.puts 'Could not build gem'
39
+ exit $CHILD_STATUS.exitstatus
40
+ end
41
+ end
42
+
43
+ task :push_and_tag => [:build] do
44
+ sh "gem push #{File.join(base, 'pkg', gem_name)}"
45
+ if $CHILD_STATUS.success?
46
+ sh "git tag -a -m \"Version #{version}\" #{version_tag}"
47
+ STDOUT.puts "Tagged #{version_tag}."
48
+ sh 'git push'
49
+ sh 'git push --tags'
50
+ else
51
+ abort 'tagging aborted pushing gem failed'
52
+ end
53
+ end
54
+
55
+ desc 'Release'
56
+ task :release => [:assert_clean_repo, :push_and_tag]
57
+
58
+ desc 'Release and build the legacy way'
59
+ task :legacy_release=> ['legacy:README.html', 'legacy:extconf', 'legacy:doc', 'legacy:manifest', 'release']
60
+
61
+
62
+ require 'rake/extensiontask'
63
+ require 'rake/testtask'
64
+ require 'rspec/core/rake_task'
65
+ RSpec::Core::RakeTask.new(:spec)
66
+
67
+ Rake::ExtensionTask.new('haversine_distance') do |ext|
68
+ ext.ext_dir = 'ext/haversine_distance_c'
69
+ end
70
+
71
+ Rake::TestTask.new(:test) do |t|
72
+ t.libs << 'test'
73
+ end
74
+
75
+ task :test => :compile
76
+ task :spec => :compile
@@ -0,0 +1,6 @@
1
+ require 'mkmf'
2
+
3
+ extension_name = 'haversine_distance'
4
+
5
+ dir_config(extension_name)
6
+ create_makefile(extension_name)
@@ -0,0 +1,46 @@
1
+ #include "ruby.h"
2
+
3
+ #define RADIAN_PER_DEGREE 0.017453292519943
4
+ #define EARTH_RADIUS 6371
5
+
6
+ // Defining a space for information and references about the module to be stored internally
7
+ VALUE HaversineDistance = Qnil;
8
+
9
+ // Prototype for the initialization method - Ruby calls this, not you
10
+ void Init_haversine_distance();
11
+
12
+ VALUE method_km(VALUE self, VALUE lat1_v, VALUE lng1_v, VALUE lat2_v, VALUE lng2_v);
13
+ VALUE method_m(VALUE self, VALUE lat1_v, VALUE lng1_v, VALUE lat2_v, VALUE lng2_v);
14
+
15
+ // The initialization method for this module
16
+ void Init_haversine_distance() {
17
+ HaversineDistance = rb_define_module("HaversineDistance");
18
+ rb_define_singleton_method(HaversineDistance, "km", method_km, 4);
19
+ rb_define_singleton_method(HaversineDistance, "m", method_m, 4);
20
+ }
21
+
22
+ double compute_km(double lat1, double lng1, double lat2, double lng2) {
23
+ double lat1_radians = lat1 * RADIAN_PER_DEGREE;
24
+ double lat2_radians = lat2 * RADIAN_PER_DEGREE;
25
+ double distance_lat = (lat2-lat1) * RADIAN_PER_DEGREE;
26
+ double distance_lng = (lng2-lng1) * RADIAN_PER_DEGREE;
27
+ double a = pow(sin(distance_lat/2.0), 2.0) + cos(lat1_radians) * cos(lat2_radians) * pow(sin(distance_lng/2.0), 2.0);
28
+ double c = 2.0 * atan2(sqrt(a), sqrt(1.0-a));
29
+ return EARTH_RADIUS * c;
30
+ }
31
+
32
+ VALUE method_km(VALUE self, VALUE lat1_v, VALUE lng1_v, VALUE lat2_v, VALUE lng2_v) {
33
+ double lat1 = NUM2DBL(lat1_v);
34
+ double lng1 = NUM2DBL(lng1_v);
35
+ double lat2 = NUM2DBL(lat2_v);
36
+ double lng2 = NUM2DBL(lng2_v);
37
+ return DBL2NUM(compute_km(lat1, lng1, lat2, lng2));
38
+ }
39
+
40
+ VALUE method_m(VALUE self, VALUE lat1_v, VALUE lng1_v, VALUE lat2_v, VALUE lng2_v) {
41
+ double lat1 = NUM2DBL(lat1_v);
42
+ double lng1 = NUM2DBL(lng1_v);
43
+ double lat2 = NUM2DBL(lat2_v);
44
+ double lng2 = NUM2DBL(lng2_v);
45
+ return DBL2NUM(compute_km(lat1, lng1, lat2, lng2) * 1000);
46
+ }
@@ -0,0 +1,28 @@
1
+ require './lib/haversine_distance/version'
2
+
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'haversine_distance_c'
6
+ s.version = HaversineDistance::VERSION
7
+ s.date = Date.today.to_s
8
+ s.summary = 'C calculation of the Haversine Distance'
9
+ s.description = 'Calculates the distance between two points using the haversine distance'
10
+ s.authors = ['Ivan Pirlik']
11
+ s.email = 'ivan.pirlik@deliveroo.co.uk'
12
+ s.license = 'MIT'
13
+
14
+ tracked_files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
15
+ file_exclusion_regex = %r{(\Alib/rvg/to_c.rb)}
16
+ files = tracked_files.reject{|file| file[file_exclusion_regex] }
17
+ test_files = files.grep(%r{^(test|spec|features)/})
18
+
19
+ s.files = files
20
+ s.test_files = test_files
21
+ s.require_paths << 'ext'
22
+
23
+ s.extensions = %w{ext/haversine_distance_c/extconf.rb}
24
+ s.has_rdoc = false
25
+
26
+ s.add_development_dependency 'rake-compiler'
27
+ s.add_development_dependency 'rspec', '~> 3.2.0'
28
+ end
@@ -0,0 +1,3 @@
1
+ module HaversineDistance
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,29 @@
1
+ require 'haversine_distance'
2
+ require 'byebug'
3
+
4
+ describe HaversineDistance do
5
+ describe '.km' do
6
+ let(:point1) { [ -0.141833514017446, 51.4956699956174 ] }
7
+ let(:point2) { [ -0.171055, 51.485144 ] }
8
+
9
+ context 'when lat-long for both points are same' do
10
+ it do
11
+ expect(described_class.km(point1[0], point1[1], point1[0], point1[1])).to eql(0.0)
12
+ end
13
+ end
14
+
15
+ context 'when lat-long for both points are not same' do
16
+ it { expect(described_class.km(point1[0], point1[1], point2[0], point2[1]).round(2)).to eql(3.45) }
17
+ end
18
+ end
19
+
20
+ describe '.m' do
21
+ let(:point1) { [ -0.141833514017446, 51.4956699956174 ] }
22
+ let(:point2) { [ -0.171055, 51.485144 ] }
23
+ let(:precision) { 2 }
24
+
25
+ context 'when lat-long for both points are not same' do
26
+ it { expect(described_class.m(point1[0], point1[1], point2[0], point2[1]).round(2)).to eql(3453.66) }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ require 'haversine_distance'
2
+
3
+ root_dir = File.expand_path('../..', __FILE__)
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haversine_distance_c
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Pirlik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.0
41
+ description: Calculates the distance between two points using the haversine distance
42
+ email: ivan.pirlik@deliveroo.co.uk
43
+ executables: []
44
+ extensions:
45
+ - ext/haversine_distance_c/extconf.rb
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - README.md
50
+ - Rakefile
51
+ - ext/haversine_distance_c/extconf.rb
52
+ - ext/haversine_distance_c/haversine_distance.c
53
+ - haversine_distance_c.gemspec
54
+ - lib/haversine_distance/version.rb
55
+ - spec/haversine_distance/haversine_distance_spec.rb
56
+ - spec/spec_helper.rb
57
+ homepage:
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ - ext
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.5.1
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: C calculation of the Haversine Distance
82
+ test_files:
83
+ - spec/haversine_distance/haversine_distance_spec.rb
84
+ - spec/spec_helper.rb