Skip to content

Commit

Permalink
Merge pull request #3 from tkrajcar/master
Browse files Browse the repository at this point in the history
Update gemfile for modern versions of development gems, and add nautical miles support
  • Loading branch information
kristianmandrup committed Aug 9, 2013
2 parents 62c0497 + 1f263c2 commit 409b208
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 45 deletions.
8 changes: 4 additions & 4 deletions Gemfile
Expand Up @@ -6,8 +6,8 @@ source "http://rubygems.org"
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
gem "rspec", ">= 2.3.0"
gem "bundler", "~> 1.0.0"
gem "jeweler", "~> 1.5.2"
gem "rcov", ">= 0"
gem "rspec", "~> 2.14.0"
gem "bundler", "~> 1.3.0"
gem "jeweler", "~> 1.8.6"
gem "simplecov", "~> 0.7.1"
end
73 changes: 56 additions & 17 deletions Gemfile.lock
@@ -1,28 +1,67 @@
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.2)
addressable (2.3.5)
builder (3.2.2)
diff-lcs (1.2.4)
faraday (0.8.8)
multipart-post (~> 1.2.0)
git (1.2.5)
jeweler (1.5.2)
bundler (~> 1.0.0)
github_api (0.10.1)
addressable
faraday (~> 0.8.1)
hashie (>= 1.2)
multi_json (~> 1.4)
nokogiri (~> 1.5.2)
oauth2
hashie (2.0.5)
highline (1.6.19)
httpauth (0.2.0)
jeweler (1.8.6)
builder
bundler (~> 1.0)
git (>= 1.2.5)
github_api (= 0.10.1)
highline (>= 1.6.15)
nokogiri (= 1.5.10)
rake
rake (0.9.0)
rcov (0.9.9)
rspec (2.4.0)
rspec-core (~> 2.4.0)
rspec-expectations (~> 2.4.0)
rspec-mocks (~> 2.4.0)
rspec-core (2.4.0)
rspec-expectations (2.4.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.4.0)
rdoc
json (1.8.0)
jwt (0.1.8)
multi_json (>= 1.5)
multi_json (1.7.8)
multi_xml (0.5.5)
multipart-post (1.2.0)
nokogiri (1.5.10)
oauth2 (0.9.2)
faraday (~> 0.8)
httpauth (~> 0.2)
jwt (~> 0.1.4)
multi_json (~> 1.0)
multi_xml (~> 0.5)
rack (~> 1.2)
rack (1.5.2)
rake (10.1.0)
rdoc (4.0.1)
json (~> 1.4)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.4)
rspec-expectations (2.14.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.2)
simplecov (0.7.1)
multi_json (~> 1.0)
simplecov-html (~> 0.7.1)
simplecov-html (0.7.1)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.0.0)
jeweler (~> 1.5.2)
rcov
rspec (>= 2.3.0)
bundler (~> 1.3.0)
jeweler (~> 1.8.6)
rspec (~> 2.14.0)
simplecov (~> 0.7.1)
14 changes: 3 additions & 11 deletions README.textile
Expand Up @@ -18,6 +18,7 @@ distance.to_miles => 6032.71091869803
distance.to_kilometers => 9715.47049115903
distance.to_meters => 9715470.49115903
distance.to_feet => 31852713.6507256
distance.to_nautical_miles => 5242.2799481204265
</pre>

Convenience aliases for the measurements exist:
Expand All @@ -26,20 +27,11 @@ distance.to_kilometers == distance.to_km
distance.to_meters == distance.to_m
distance.to_miles == distance.to_mi
distance.to_feet == distance.to_ft
distance.to_nautical_miles == distance.to_nm
</pre>

Note that @to_m@ is the distance in meters, not miles.

puts "#{dist[:feet]}"
puts "#{dist.meters}"
puts "#{dist[:km]}"
puts "#{dist[:miles]}"
puts "#{dist.to_mi}"
puts "#{dist.to_miles_}"
dist[:km].to_s.should match(/7\.376*/)
dist.to_km.to_s.should match(/7\.376*/)
end

If you have lat/lon pairs stored in an array, you can alternately provide two arrays when calling @Haversine.distance@:

<pre>
Expand All @@ -64,4 +56,4 @@ h2. Contributing to haversine
h2. Copyright

Copyright (c) 2011 Kristian Mandrup. See LICENSE.txt for
further details.
further details.
6 changes: 3 additions & 3 deletions Rakefile
Expand Up @@ -34,14 +34,14 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end

RSpec::Core::RakeTask.new(:rcov) do |spec|
RSpec::Core::RakeTask.new(:coverage) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
ENV['COVERAGE'] = 'true'
end

task :default => :spec

require 'rake/rdoctask'
require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""

Expand Down
20 changes: 13 additions & 7 deletions lib/haversine/distance.rb
@@ -1,40 +1,46 @@
module Haversine
class Distance
include Comparable

GREAT_CIRCLE_RADIUS_MILES = 3956
GREAT_CIRCLE_RADIUS_KILOMETERS = 6371 # some algorithms use 6367
GREAT_CIRCLE_RADIUS_FEET = GREAT_CIRCLE_RADIUS_MILES * 5280
GREAT_CIRCLE_RADIUS_METERS = GREAT_CIRCLE_RADIUS_KILOMETERS * 1000
GREAT_CIRCLE_RADIUS_NAUTICAL_MILES = GREAT_CIRCLE_RADIUS_MILES / 1.15078

attr_reader :great_circle_distance

def initialize(great_circle_distance)
@great_circle_distance = great_circle_distance
end

def to_miles
@great_circle_distance * GREAT_CIRCLE_RADIUS_MILES
end
alias_method :to_mi, :to_miles

def to_kilometers
@great_circle_distance * GREAT_CIRCLE_RADIUS_KILOMETERS
end
alias_method :to_km, :to_kilometers

def to_meters
@great_circle_distance * GREAT_CIRCLE_RADIUS_METERS
end
alias_method :to_m, :to_meters

def to_feet
@great_circle_distance * GREAT_CIRCLE_RADIUS_FEET
end
alias_method :to_ft, :to_feet


def to_nautical_miles
@great_circle_distance * GREAT_CIRCLE_RADIUS_NAUTICAL_MILES
end
alias_method :to_nm, :to_nautical_miles

def <=>(other)
great_circle_distance <=> other.great_circle_distance
end
end
end
end
12 changes: 10 additions & 2 deletions spec/haversine_spec.rb
Expand Up @@ -16,7 +16,15 @@
array_dist.should be_a(Haversine::Distance)
point_dist.to_m.should == array_dist.to_m
end


it "computes nautical mile distances correctly" do
new_york_city = [40.71427, -74.00597]
santiago_chile = [-33.42628, -70.56656]
dist = Haversine.distance(new_york_city, santiago_chile)
dist.to_miles.should eq(5123.736179853891)
dist.to_nautical_miles.should eq(4452.402874445064)
end

it "calculates the distance between the provided lat/lon pairs" do
Haversine.distance(0,0,0,0).to_miles.should == 0
round_to(6, Haversine.distance(0,0,0,360).to_miles).should == 0
Expand All @@ -28,4 +36,4 @@
def round_to(precision, num)
(num * 10**precision).round.to_f / 10**precision
end
end
end
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -2,7 +2,8 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
require 'haversine'

require 'simplecov'
SimpleCov.start if ENV["COVERAGE"]
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
Expand Down

0 comments on commit 409b208

Please sign in to comment.