Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New refactor #6

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
source "http://rubygems.org"

gem 'geo_point', '~> 0.2.5'
gem 'geo_units', '~> 0.2.4.1'
gem 'i18n'
gem 'activesupport'
gem 'geo_point', :github => 'kristianmandrup/geo_point'
gem 'geo_units', :github => 'kristianmandrup/geo_units'

group :development do
gem "rspec", ">= 2.6.0"
gem "bundler", "~> 1.0.10"
gem "jeweler", "~> 1.6.2"
gem "rcov", ">= 0"
gem "rspec", "~> 2.6.0"
gem "bundler", ">= 1.11"
gem "jeweler", ">= 1.6.2"
end
70 changes: 49 additions & 21 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
GIT
remote: git://github.com/kristianmandrup/geo_point.git
revision: 057d50c266d86cb28d79f23b9c7ab14077af25bd
specs:
geo_point (0.2.6)
geo_calc (>= 0.7.7.1)

GIT
remote: git://github.com/kristianmandrup/geo_units.git
revision: 755d03a445f9bea166d665db275dba186653612b
specs:
geo_units (0.3.4)
activesupport (>= 4)
i18n (>= 0.8)
sugar-high (~> 0.7.2)
sweetloader (~> 0.1.6)

GEM
remote: http://rubygems.org/
specs:
activesupport (3.0.9)
activesupport (5.1.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
concurrent-ruby (1.0.5)
diff-lcs (1.1.2)
geo_calc (0.7.6)
geo_calc (0.7.8)
activesupport (>= 3.0.1)
geo_units (~> 0.2.1)
i18n
require_all (~> 1.2.0)
sugar-high (~> 0.4.6.3)
geo_point (0.2.5)
geo_calc (~> 0.7.5)
geo_units (~> 0.2.1)
geo_units (0.2.4.1)
sugar-high (~> 0.4.6.2)
geo_units (~> 0.3.2)
i18n (>= 0.5)
require_all (>= 1.2.0)
sugar-high (~> 0.7.2)
sweetloader
git (1.2.5)
i18n (0.6.0)
i18n (0.8.6)
jeweler (1.6.2)
bundler (~> 1.0)
git (>= 1.2.5)
rake
minitest (5.10.3)
rake (0.9.2)
rcov (0.9.9)
require_all (1.2.0)
require_all (1.4.0)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
Expand All @@ -31,15 +49,25 @@ GEM
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
sugar-high (0.4.6.4)
sugar-high (0.7.3)
sweetloader (0.1.6)
activesupport (>= 3.0.1)
i18n
thread_safe (0.3.6)
tzinfo (1.2.3)
thread_safe (~> 0.1)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.0.10)
geo_point (~> 0.2.5)
geo_units (~> 0.2.4.1)
jeweler (~> 1.6.2)
rcov
rspec (>= 2.6.0)
activesupport
bundler (>= 1.11)
geo_point!
geo_units!
i18n
jeweler (>= 1.6.2)
rspec (~> 2.6.0)

BUNDLED WITH
1.12.5
124 changes: 92 additions & 32 deletions README.textile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
h1. GeoDistance - geo distance calculations

Calculates the Geo distance between two locations using longitude and latitude.
This is done using "pure" Math formulas without resorting to Active Record or SQL DB functionality
Calculates the Geographical shortest distance between two locations using longitude and latitude.
This is done using "pure" Math formulas without resorting to Database provided functionality

This gem is meant to be a replacement for those geo distance utils out there that use built in SQL DB functionality for their calculations!

Expand All @@ -10,64 +10,124 @@ The formulas curently supported are
* _Haversine_
* _Spherical_
* _Vincenty_
* _NVector_
* _Flat_

h2. Install & Usage
The algorithm is specified using a symbol of either: [:flat, :haversine, :spherical, :vincenty, :nvector]

h2. Status

*Broken*

Has been under some heavy-duty refctoring since around 2015 when it started to break due to changes in Ruby, external libraries etc.

Currently being worked on in the `new refactor` branch. Please work from there if you intend to use this gem.

Any help greatly appreciated!

Note that this WIP version currently depends directly on [geo_point](https://github.com/kristianmandrup/geo_point) and [geo_units](https://github.com/kristianmandrup/geo_units) repos on github.
The dependent gems might need a few more changes as well...

Ideally the distance formula classes should be extracted (into separate gem) and made independent of the rest of the gems.

h2. Install

in Gemfile:

@gem 'geo-distance'@

Run bundler:

@$ bundle@

h2. Usage example (Rspec 2)

<pre>require 'geo-distance'

it "should work" do
lon1 = -104.88544
lat1 = 39.06546
describe GeoDistance do
let(:from) do
[45, 10].geo_point
end

let(:to) do
b = [42, 11].geo_point
end

describe '#distance' do
it "should calculate haversine distance as Float" do

# specify units using :units option, fx :units => :miles
dist = GeoDistance.haversine(from, to, :globe => my_globe)
dist.should be_a(Float)

puts "the distance from #{from} to #{to} is: #{dist.kms_to(:meters)} meters"

dist.should be_within(4).of 340
end
end

lon2 = -104.80
lat2 = lat1
describe '#geo_distance' do
it "should calculate haversine distance as GeoDistance" do
dist = GeoDistance.haversine_dist(from, to)
dist.should be_a(GeoDistance)

dist = GeoDistance.distance( lat1, lon1, lat2, lon2 ) # in radians
puts "the distance from #{from} to #{to} is: #{dist.meters} meters"

dist = GeoDistance::Haversine.geo_distance( lat1, lon1, lat2, lon2 ).to_meters
dist.kms.should be_within(4).of 340
end
end

# Change to (lng, lat) mode
GeoPoint.coord_mode = :lng_lat
it "should set default_algorithm to :haversine" do
GeoDistance.default_algorithm = :haversine

p1, p2 = [[lon1, lat1].geo_point, [lon2, lat2].geo_point]
dist = GeoDistance.distance(from, to, :units => :miles)
geo_dist = GeoDistance.geo_distance(from, to, :units => :meters)
end
end
</pre>

h2. Configuration and other notes

dist = GeoDistance::Haversine.geo_distance( lon1, lat1, lon2, lat2)
dist = GeoDistance::Haversine.geo_distance( p1, p2).to_miles
GeoDistance has the following global settings:

puts "the distance from #{lat1}, #{lon1} to #{lat2}, #{lon2} is: #{dist.meters} meters"
* default_formula
* default_units
* default_globe

dist.feet
dist.meters
dist.kms
dist.miles
end
</pre>
All distances are calculated relative to the globe, which by default is set to the Earth.

h2. Distance API

The call to distance returns an instance of GeoDistance
The call to _geo_distance_ returns an instance of GeoDistance

@dist = GeoDistance::Formula::Haversine.geo_distance( lat1, lon1, lat2, lon2 )@

@dist = GeoDistance::Haversine.distance( lat1, lon1, lat2, lon2 )@
The #kms, #meters, #miles and #feet methods return the length in the given unit.

The #kms, #meters, #miles and #feet methods return an instance og GeoUnit
h3. Setting default globe

@dist.kms@
GeoDistance.default_globe = MyFantasyWorld.new :radius => 2000.km
dist = GeoDistance.distance( lat1, lon1, lat2, lon2 )

h3. Setting default algorithm

You can also set a default algorithm to use...
h3. Setting default formula

You can also set a default formula to use...
The following will use the _Haversine_ algorithm:

<pre>
GeoDistance.default_algorithm = :haversine
dist = GeoDistance.distance( lat1, lon1, lat2, lon2 )
GeoDistance.default_formula = :haversine
dist = GeoDistance.distance( lat1, lon1, lat2, lon2 )
dist2 = GeoDistance.distance( [lat1, lon1], [lat2, lon2] )
</pre>

GeoDistance is used in the "geo_magic":https://github.com/kristianmandrup/geo_magic gem
Note that _GeoDistance_ uses "geo_point":https://github.com/kristianmandrup/geo_point for parsing arguments into two GeoPoints (point A to point B).
It can handle: Numeric, Array, Hash, String (even in DMS format). See geo_point for more...

GeoDistance can be used in "mongoid_geo":https://github.com/kristianmandrup/mongoid-geo gem, as a distance calculator (in case the internal Mongo DB calculation is not used).

h2. Contributing to geo-distance

* Check out the specs and add specs to spec any added features or changes!
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
* Fork the project
Expand Down
7 changes: 1 addition & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Jeweler::Tasks.new do |gem|
gem.homepage = "http://github.com/kristianmandrup/geo-distance"
gem.license = "MIT"
gem.summary = %Q{Calculates the geo distance between two locations using longitude and latitude, using Haversine, Speherical or Vincenty formula}
gem.description = %Q{Calculates the geo distance between two locations using longitude and latitude using Haversine, Speherical or Vincenty formula.
gem.description = %Q{Calculates the geo distance between two locations using longitude and latitude using Haversine, Speherical or Vincenty formula.
This is done using Math formulas without resorting to Active Record or SQL DB functionality}
gem.email = "[email protected]"
gem.authors = ["Kristian Mandrup"]
Expand All @@ -33,11 +33,6 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end

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

task :default => :spec

require 'rake/rdoctask'
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.2.1
47 changes: 34 additions & 13 deletions lib/geo-distance.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
require 'geo_point'

class GeoDistance
autoload :Conversion, 'geo-distance/conversion'
autoload :Scale, 'geo-distance/scale'
autoload :ClassMethods, 'geo-distance/class_methods'

autoload :Haversine, 'geo-distance/formula/haversine'
autoload :Spherical, 'geo-distance/formula/spherical'
autoload :Vincenty, 'geo-distance/formula/vincenty'
autoload :NVector, 'geo-distance/formula/n_vector'
autoload :Flat, 'geo-distance/formula/flat'
end

require 'geo-distance/distance'
autoload_modules :ClassMethods, :Scale, :Conversion, :Formula, :Scale, :Globe, :from => 'geo-distance'

include GeoUnits

include Comparable
include Conversion

attr_accessor :distance, :units
attr_reader :globe

def initialize distance, units = :radians, options = {}
@distance = distance
@unit = unit_key(units)
@globe = options[:globe] || GeoDistance.default_globe
end

alias_method :unit, :units

def <=> other
in_meters <=> other.in_meters
end

def number
distance.round_to(precision[unit])
end

protected

def unit_key units
GeoUnits.key units
end
end

require 'geo-distance/core_ext'
require 'geo-distance/formula'


Loading