Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

lots of improvements

  • Loading branch information...
commit 1976bb9807ce366fd8c6e38c1242bb6dfe3758a1 1 parent d26cdec
@kristianmandrup authored
Showing with 677 additions and 93 deletions.
  1. +3 −3 .gitignore
  2. +1 −0  lib/geo_magic.rb
  3. +9 −0 lib/geo_magic/core_ext.rb
  4. +5 −1 lib/geo_magic/distance.rb
  5. +1 −1  lib/geo_magic/distance/formula/haversine.rb
  6. +2 −20 lib/geo_magic/distance/formula/plane.rb
  7. +1 −1  lib/geo_magic/distance/formula/spherical.rb
  8. +1 −1  lib/geo_magic/distance/formula/vincenty.rb
  9. +18 −0 lib/geo_magic/distance/points_distance.rb
  10. +2 −0  lib/geo_magic/distance/vector.rb
  11. +5 −0 lib/geo_magic/point.rb
  12. +1 −0  lib/geo_magic/radius.rb
  13. +15 −0 lib/geo_magic/radius/within_radius.rb
  14. +1 −0  lib/geo_magic/shape.rb
  15. +17 −1 lib/geo_magic/vector.rb
  16. +379 −0 rspec_examples.rb
  17. +32 −0 spec/geo_magic/distance/class_methods_spec.rb
  18. +30 −0 spec/geo_magic/distance/formula/haversine_spec.rb
  19. +27 −12 spec/geo_magic/distance/formula/plane_spec.rb
  20. +30 −0 spec/geo_magic/distance/formula/spherical_spec.rb
  21. +30 −0 spec/geo_magic/distance/formula/vincenty_spec.rb
  22. +18 −0 spec/geo_magic/distance/points_distance_spec.rb
  23. +7 −8 spec/geo_magic/distance/unit_spec.rb
  24. +18 −24 spec/geo_magic/distance/vector_spec.rb
  25. +24 −21 spec/geo_magic/distance_spec.rb
  26. 0  spec/geo_magic/radius/within_radius_spec.rb
View
6 .gitignore
@@ -27,11 +27,11 @@ pkg
#
# For MacOS:
#
-#.DS_Store
+.DS_Store
#
# For TextMate
-#*.tmproj
-#tmtags
+*.tmproj
+tmtags
#
# For emacs:
#*~
View
1  lib/geo_magic.rb
@@ -10,5 +10,6 @@ module GeoMagic
require 'geo_magic/remote'
require 'geo_magic/calculate'
require 'geo_magic/distance'
+require 'geo_magic/radius'
require 'geo_magic/meta'
require 'geo_magic/geocoder'
View
9 lib/geo_magic/core_ext.rb
@@ -57,6 +57,15 @@ def radians_ratio
class Array
+ def is_point?
+ (0..1).all? {|n| self[n].is_a?(Numeric) }
+ end
+
+ def to_point
+ raise "For an array to be converted to a point, it must consist of two numbers, was: #{self}" if !is_point?
+ GeoMagic::Point.new self[0], self[1]
+ end
+
def are_points?
each do |p|
return false if !p.kind_of?(GeoMagic::Point)
View
6 lib/geo_magic/distance.rb
@@ -3,6 +3,7 @@
require 'geo_magic/distance/unit'
require 'geo_magic/distance/vector'
require 'geo_magic/distance/formula'
+require 'geo_magic/distance/points_distance'
module GeoMagic
class Distance
@@ -19,7 +20,10 @@ def initialize distance, unit = nil
end
# select all points within radius
- def select_within points, center
+ def select_within points, center
+ points.select do |p|
+ point.within? center
+ end
end
# reject all points within radius
View
2  lib/geo_magic/distance/formula/haversine.rb
@@ -42,7 +42,7 @@ def self.point_distance point_a, point_b
# given two lat/lon points, compute the distance between the two points using the haversine formula
# the result will be a Hash of distances which are key'd by 'mi','km','ft', and 'm'
- def self.distance( lat1, lon1, lat2, lon2)
+ def self.distance lat1, lon1, lat2, lon2
dlon = lon2 - lon1
dlat = lat2 - lat1
View
22 lib/geo_magic/distance/formula/plane.rb
@@ -1,26 +1,8 @@
module GeoMagic
class Distance
class Plane < Formula
- def distance from_point, to_point, options = { :unit => :meters }
- vector = Vector.new :formula => self
- dist = vector.distance options
- GeoMagic::Distance.new vector_distance(vector, options)
- end
-
- def vector_distance vector, options = { :unit => :meters }
- dist = Math.sqrt((delta_longitude(vector) + delta_latitude).abs)
- unit_dist = ::GeoMagic::Distance.new(dist).send unit
- unit_dist.number
- end
-
- protected
-
- def delta_longitude vector
- (vector[0].longitude - vector[1].longitude)**2
- end
-
- def delta_latitude vector
- (vector[0].latitude - vector[1].latitude)**2
+ def distance *args
+ Vector.new(*args).distance
end
end
end
View
2  lib/geo_magic/distance/formula/spherical.rb
@@ -8,7 +8,7 @@ def self.point_distance point_a, point_b
distance *points
end
- def self.distance( lat1, lon1, lat2, lon2)
+ def self.distance lat1, lon1, lat2, lon2
from_longitude = lon1.to_radians
from_latitude = lat1.to_radians
to_longitude = lon2.to_radians
View
2  lib/geo_magic/distance/formula/vincenty.rb
@@ -17,7 +17,7 @@ def self.point_distance point_a, point_b
# )
# #=> 101.070118000159
#
- def self.distance(lat1, lon1, lat2, lon2)
+ def self.distance lat1, lon1, lat2, lon2
from_longitude = lon1.to_radians
from_latitude = lat1.to_radians
to_longitude = lon2.to_radians
View
18 lib/geo_magic/distance/points_distance.rb
@@ -0,0 +1,18 @@
+module GeoMagic
+ class PointsDistance
+ attr_accessor :distance, :points
+
+ def initialize distance, points
+ raise ArgumentError, "First argument must be a GeoMagic::Distance, was #{distance}" if !distance.kind_of? GeoMagic::Distance
+ # raise ArgumentError, "First argument must be a collection of GeoMagic::Point, was #{points}" if !points.
+ self.distance = distance
+ self.points = points
+ end
+
+ def near center
+ points.select {|point| point.within(distance).of(center) }
+ end
+ end
+end
+
+
View
2  lib/geo_magic/distance/vector.rb
@@ -1,5 +1,7 @@
require 'sugar-high/kind_of'
+# This can be used to indicate a rectangle, with a latitude and logitude distance from the center in both directions to span out the rectangle.
+
module GeoMagic
class Distance
class Vector
View
5 lib/geo_magic/point.rb
@@ -25,6 +25,11 @@ def initialize *args
@longitude = points.last
end
+ def within? shape
+ raise ArgumentError, "Argument must be a GeoMagic::Shape, was #{shape}" if !shape.kind_of? GeoMagic::Shape
+ shape.contains? point
+ end
+
# factory method
def self.create_from *args
latitude, longitude = case args.size
View
1  lib/geo_magic/radius.rb
@@ -1,4 +1,5 @@
require 'geo_magic/point'
+require 'geo_magic/radius/within_radius'
require 'geo_magic/radius/random_radiant'
module GeoMagic
View
15 lib/geo_magic/radius/within_radius.rb
@@ -0,0 +1,15 @@
+module GeoMagic
+ class WithinRadius
+ attr_reader :distance, :point
+
+ def initialize point, distance
+ self.distance = distance
+ self.point = point
+ end
+
+ def of? center
+ circle = CircularRadius.new center, distance
+ point.within? circle
+ end
+ end
+end
View
1  lib/geo_magic/shape.rb
@@ -4,5 +4,6 @@ class Shape
def within? point
raise "Subclass should implement this"
end
+ alias_method :contains?, :within?
end
end
View
18 lib/geo_magic/vector.rb
@@ -28,6 +28,12 @@ def length type = nil
def vector_distance
GeoMagic::Distance::Vector.new length(:latitude), length(:longitude)
end
+
+ def distance options = { :unit => :meters }
+ dist = Math.sqrt((delta_longitude(vector) + delta_latitude).abs)
+ unit_dist = ::GeoMagic::Distance.new(dist).send unit
+ unit_dist.number
+ end
def [] key
case key
@@ -38,7 +44,17 @@ def [] key
else
raise "Vector key must be either 0/1 or :p0/:p1"
end
- end
+ end
+
+ protected
+
+ def delta_longitude
+ (p0.longitude - p1.longitude)**2
+ end
+
+ def delta_latitude vector
+ (p0.latitude - p1.latitude)**2
+ end
end
end
end
View
379 rspec_examples.rb
@@ -0,0 +1,379 @@
+# RSpec Examples (RSpec Book, The Rails 3 Way, Custom)
+
+################################################################################
+# command line
+# rspec --help (show help)
+# rake -T spec (list all rspec-rails rake tasks)
+# rspec spec (run all specs in spec folder)
+# rspec spec/models/user_spec.rb (run only user_spec)
+# rake spec:models (rails: run all model specs)
+
+################################################################################
+# configuration
+
+# global before/after/around hooks
+# (:each, :all, :suite)
+RSpec.configure do |config|
+ config.after :suite do
+ Mongoid.master.collections.select do |collection|
+ collection.name !~ /system/
+ end.each(&:drop)
+ end
+end
+
+# inclusion filter
+RSpec.configure do |c|
+ c.filter = { :focus => true }
+end
+
+describe "group" do
+ it "example 1", :focus => true do
+
+ end
+end
+
+# exclusion filter
+RSpec.configure do |c|
+ c.exclusion_filter = { :slow => true }
+end
+
+describe "group", :slow => true do
+ it "example 1" do
+ end
+ it "example 2" do
+ end
+end
+
+# lambda filter
+require 'ping'
+RSpec.configure do |c|
+ c.exclusion_filter = {
+ :if => lambda { |what|
+ case what
+ when :network_available
+ !Ping.pingecho "example.com", 10, 80
+ end
+ }
+ }
+end
+
+describe "network group" do
+ it "example 1", :if => :network_available do
+ end
+ it "example 2" do
+ end
+end
+
+################################################################################
+# describe/context (describe for things, context (alias) for context)
+describe User do
+
+ context "#create" do
+
+ end
+
+end
+
+################################################################################
+# pending (instead of adding comment to disable example)
+describe User do
+ before { pending }
+
+ it "should be successful" do
+
+ end
+end
+
+describe "an empty array" do
+ it "should be empty" do
+ pending("bug report 18976") do
+ [].should be_empty
+ end
+ end
+end
+
+################################################################################
+# before, after, around (before(:each) executed before each example)
+describe Stack do
+ context "when empty" do
+ before(:each) do
+ @stack = Stack.new
+ end
+ end
+end
+
+################################################################################
+# helper methods
+describe Thing do
+ def create_thing(options)
+ thing = Thing.new
+ thing.set_status(options[:status])
+ thing
+ end
+end
+
+describe Thing do
+ def given_thing_with(options)
+ yield Thing.new do |thing|
+ thing.set_status(options[:status])
+ end
+ end
+
+ it "should do something when ok" do
+ given_thing_with(:status => 'ok') do |thing|
+ thing.do_fancy_stuff(1, true, :move => 'left', :obstacles => nil)
+ end
+ end
+end
+
+################################################################################
+# helper modules
+module UserExampleHelpers
+ def create_valid_user
+ User.new(:email => 'email@example.com', :password => 'shhhhh')
+ end
+end
+
+describe User do
+ include UserExampleHelpers
+ it "does something when it is valid" do
+ user = create_valid_user
+ end
+end
+
+# or include in all example groups
+RSpec.configure do |config|
+ config.include(UserExampleHelpers)
+end
+
+################################################################################
+# shared examples
+# spec/support/shared_examples.rb
+shared_examples_for "a phone field" do
+ it "has 10 digits" do
+ Business.new(phone_field => '8004567890').should
+ have(:no).errors_on(phone_field)
+ end
+end
+
+describe "phone" do
+ let(:phone_field) { :phone }
+ it_should_behave_like "a phone field"
+end
+
+################################################################################
+# let (instead of instance variables in before block)
+let(:blog_post) { BlogPost.create :title => 'Hello' }
+let!(:comment) { blog_post.comments.create :text => 'first post' }
+
+################################################################################
+# specify (generated descriptions)
+# instead of repeating expectation with: it "should not be published"...)
+specify { blog_post.should_not be_published }
+
+################################################################################
+# subject (delegate should/should_not to subject)
+subject { BlogPost.new :title => 'foo', :body => 'bar' }
+ it "sets published timestamp" do
+ subject.publish!
+ subject.should be_published
+ end
+
+it { should be_valid }
+its(:errors) { should be_empty }
+its(:title) { should == 'foo' }
+its(:body) { should == 'bar' }
+its(:published_on) { should == Date.today }
+
+################################################################################
+# matchers
+# be
+target.should be
+target.should be_true
+target.should be_false
+target.should be_nil
+target.should_not be_nil
+thing.should be # passes if thing is not nil or false
+collection.should be_empty # passes if target.empty?
+target.should_not be_empty # passes unless target.empty?
+target.should_not be_under_age(16) # passes unless target.under_age?(16)
+user.should be_in_role("admin") # passes if user.in_role?("admin") returns true
+result.should be_close(5.25, 0.005)
+
+# be_a, be_an
+"a string".should be_an_instance_of(String)
+3.should be_a_kind_of(Fixnum)
+3.should be_a_kind_of(Numeric)
+3.should be_an_instance_of(Fixnum)
+3.should_not be_instance_of(Numeric) #fails
+
+# operators
+result.should == 3
+result.should =~ /some regexp/
+result.should be < 7
+result.should be <= 7
+result.should be >= 7
+result.should be > 7
+
+# include
+[1, 2, 3].should include(1)
+[1, 2, 3].should_not include(4)
+"foobar".should include("bar")
+"foobar".should_not include("baz")
+
+# have
+{:foo => "foo"}.should have_key(:foo)
+{:bar => "bar"}.should_not have_key(:foo)
+"this string".should have(11).characters
+[1, 2, 3].should have(3).items
+schedule.should have(3).days # passes if schedule.days.length == 3
+home_team.should have(9).players_on(field) # delegates players_on to home_team
+widget.should have(1).error_on(:name)
+model.should have(:no).errors_on(:title)
+model.should have(1).record
+day.should have_exactly(24).hours
+dozen_bagels.should have_at_least(12).bagels
+internet.should have_at_most(2037).killer_social_networking_apps
+
+# respond_to
+list.should respond_to(:length)
+
+# errors
+lambda { Object.new.explode! }.should raise_error(NameError)
+
+# when all else fails...
+actual.should satisfy { |actual| block } # passes if block returns true
+
+################################################################################
+# expect
+# for changes
+expect { BlogPost.create :title => "Hello"}.to change { BlogPost.count }.by(1)
+expect { seller.accept Offer.new(250_000) }.to change{agent.commission}.by(7_500)
+expect { blog_post.publish! }.to change { blog_post.published_on }.from(nil).to(Date.today)
+expect { subject.update_with_omniauth(omniauth) }.to_not change { subject.score }
+
+# for errors
+expect { do_something_risky }.to raise_error
+expect { account.withdraw 75, :dollars }.to raise_error(/attempted to withdraw 75 dollars/)
+expect { account.withdraw 75, :dollars }.to raise_error(InsufficientFundsError)
+
+course = Course.new(:seats => 20)
+20.times { course.register Student.new }
+lambda {
+ course.register Student.new
+}.should throw_symbol(:course_full, 20)
+
+################################################################################
+# custom matcher dsl
+# joe.reports_to?(beatrice).should be_true
+joe.should report_to(beatrice)
+
+RSpec::Matchers.define :report_to do |boss|
+ match do |employee|
+ employee.reports_to?(boss)
+ end
+end
+
+################################################################################
+# doubles/stubs/mocks (aliases)
+# doubles/stubs: method stubs (stub(:method).and_return(data))
+# mocks: expectations (should_receive...)
+describe Statement do
+ it "uses the customer's name in the header" do
+ customer = double('customer')
+ customer.stub(:name).and_return('Aslak')
+ statement = Statement.new(customer)
+ statement.generate.should =~ /^Statement for Aslak/
+ end
+end
+
+# shortcut
+customer = double('customer', :name => 'Aslak')
+
+# null object
+null_object = mock('null', :null_object => true)
+
+# fakes
+ages = double('ages')
+ages.stub(:age_for) do |what|
+ if what == 'drinking'
+ 21
+ elsif what == 'voting'
+ 18
+ end
+end
+
+# stub chains
+article = double()
+Article.stub_chain(:recent, :published, :authored_by).and_return(article)
+
+# partial stubbing/mocking
+describe WidgetsController do
+ describe "PUT update with valid attributes" do
+ # partial stubbing
+ it "redirects to the list of widgets" do
+ widget = Widget.new()
+ Widget.stub(:find).and_return(widget)
+ widget.stub(:update_attributes).and_return(true)
+ put :update, :id => 37
+ response.should redirect_to(widgets_path)
+ end
+ # partial mocking
+ it "updates the widget's attributes" do
+ widget = Widget.new()
+ Widget.stub(:find).and_return(widget)
+ widget.should_receive(:update_attributes).and_return(true)
+ put :update, :id => 37
+ end
+ end
+end
+
+# expectations
+echo = mock('echo')
+echo.should_receive(:sound).with("hey").and_return("hey")
+echo.should_receive(:sound).with("hey") { "hey" }
+mock_account.should_receive(:withdraw).exactly(1).times
+network_double.should_receive(:open_connection).at_most(5).times
+network_double.should_receive(:open_connection).at_least(2).times
+account_double.should_receive(:withdraw).once
+account_double.should_receive(:deposit).twice
+checking_account.should_receive(:transfer).with(50, savings_account)
+source_account.should_receive(:transfer).with(target_account, instance_of(Fixnum))
+source_account.should_receive(:transfer).with(anything(), 50)
+source_account.should_receive(:transfer).with(any_args())
+collaborator.should_receive(:message).with(no_args())
+mock_account.should_receive(:add_payment_accounts).with(hash_including('Electric' => '123', 'Gas' => '234'))
+mock_account.should_receive(:add_payment_accounts).with(hash_not_including('Electric' => '123', 'Gas' => '234'))
+mock_atm.should_receive(:login).with(/.* User/)
+network_double.should_not_receive(:open_connection)
+network_double.should_receive(:open_connection).never
+
+# ordering (ensure count is called before add)
+database.should_receive(:count).with('Roster', :course_id => 37).ordered
+database.should_receive(:add).with(student).ordered
+
+# errors
+account_double.should_receive(:withdraw).and_raise
+account_double.should_receive(:withdraw).and_raise(InsufficientFunds)
+account_double.should_receive(:withdraw).and_throw(:insufficient_funds)
+
+# custom argument matchers
+class GreaterThanMatcher
+ def initialize(expected)
+ @expected = expected
+ end
+
+ def description
+ "a number greater than #{@expected}"
+ end
+
+ def ==(actual)
+ actual > @expected
+ end
+end
+
+def greater_than(floor)
+ GreaterThanMatcher.new(floor)
+end
+
+calculator.should_receive(:add).with(greater_than(37))
View
32 spec/geo_magic/distance/class_methods_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+describe GeoMagic::Distance do
+ before do
+ @long1 = -104.88544
+ @lat1 = 39.06546
+
+ @long2 = -104.80
+ @lat2 = @lat1
+ end
+
+ subject { GeoMagic::Distance }
+
+ it "calculates distance using array args - using algorithm haversine" do
+ dist = subject.calculate [@long1, @lat1], [@long2, @lat2]
+ puts dist
+ end
+
+ it "calculates distance using array args - using algorithm haversine" do
+ dist = subject.calculate @long1, @lat1, @long2, @lat2
+ km_dist = dist[:km]
+ puts dist
+ puts "km: #{km_dist}"
+ end
+
+ it "calculates distance using array args - using algorithm haversine" do
+ dist = subject.calculate {:long => @long1, :lat => @lat1}, {:long => @long2, :lat => @lat2}
+ km_dist = dist[:km]
+ puts dist
+ puts "km: #{km_dist}"
+ end
+end
View
30 spec/geo_magic/distance/formula/haversine_spec.rb
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+describe GeoMagic::Distance::Haversine do
+
+ subject { GeoMagic::Distance::Haversine }
+
+ describe '#point_distance' do
+ before do
+ @a = [45, 10].to_point
+ @b = [42, 11].to_point
+ end
+
+ it 'should calculate the distance between points A and B' do
+ dist = subject.point_distance @a, @b
+ puts dist
+ end
+ end
+
+ describe '#distance' do
+ before do
+ @a = [45, 10]
+ @b = [42, 11]
+ end
+
+ it 'should calculate the distance between points A and B' do
+ dist = subject.distance [@a, @b].flatten
+ puts dist
+ end
+ end
+end
View
39 spec/geo_magic/distance/formula/plane_spec.rb
@@ -1,17 +1,32 @@
require 'spec_helper'
-require 'geo_magic'
-describe "GeoMagic Calculate" do
- before do
- @long1 = -104.88544
- @lat1 = 39.06546
- @long2 = -104.80
- @lat2 = @lat1
- end
+describe GeoMagic::Distance::Plane do
+
+ subject { GeoMagic::Distance::Plane }
- it "calculates distance using array args" do
- dist = GeoMagic::Calculate.plane_distance [@long1, @lat1], [@long2, @lat2]
- puts dist
+ describe '#point_distance' do
+ before do
+ @a = [45, 10].to_point
+ @b = [42, 11].to_point
+ end
+
+ it 'should calculate the distance between points A and B' do
+ dist = subject.point_distance @a, @b
+ puts dist
+ end
end
-end
+ describe '#distance' do
+ before do
+ @a = [45, 10]
+ @b = [42, 11]
+ end
+
+ it 'should calculate the distance between points A and B' do
+ dist = subject.distance [@a, @b].flatten
+ puts dist
+ end
+ end
+end
+
View
30 spec/geo_magic/distance/formula/spherical_spec.rb
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+describe GeoMagic::Distance::Spherical do
+
+ subject { GeoMagic::Distance::Spherical }
+
+ describe '#point_distance' do
+ before do
+ @a = [45, 10].to_point
+ @b = [42, 11].to_point
+ end
+
+ it 'should calculate the distance between points A and B' do
+ dist = subject.point_distance @a, @b
+ puts dist
+ end
+ end
+
+ describe '#distance' do
+ before do
+ @a = [45, 10]
+ @b = [42, 11]
+ end
+
+ it 'should calculate the distance between points A and B' do
+ dist = subject.distance [@a, @b].flatten
+ puts dist
+ end
+ end
+end
View
30 spec/geo_magic/distance/formula/vincenty_spec.rb
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+describe GeoMagic::Distance::Haversine do
+
+ subject { GeoMagic::Distance::Haversine }
+
+ describe '#point_distance' do
+ before do
+ @a = [45, 10].to_point
+ @b = [42, 11].to_point
+ end
+
+ it 'should calculate the distance between points A and B' do
+ dist = subject.point_distance @a, @b
+ puts dist
+ end
+ end
+
+ describe '#distance' do
+ before do
+ @a = [45, 10]
+ @b = [42, 11]
+ end
+
+ it 'should calculate the distance between points A and B' do
+ dist = subject.distance [@a, @b].flatten
+ puts dist
+ end
+ end
+end
View
18 spec/geo_magic/distance/points_distance_spec.rb
@@ -0,0 +1,18 @@
+require 'spec_helper'
+
+describe GeoMagic::PointsDistance do
+ before do
+ @a = [45.1, 11].to_point
+ @b = [48, 11].to_point
+ @center = [45, 11].to_point
+ end
+
+ context 'Distance vector' do
+ subject { GeoMagic::PointsDistance.new 5.km, [@a, @b].to_points }
+
+ describe '#near' do
+ subject.near(@center).should include?(@a)
+ end
+ end
+end
+
View
15 spec/geo_magic/distance/unit_spec.rb
@@ -1,12 +1,11 @@
require 'spec_helper'
-describe GeoMagic::Distance::Unit do
- it "should create 5 km" do
- km_5 = GeoMagic::Distance::Unit.new :km, 5
- km_5.number.should > 0
- km_5.radians_ratio.should > 0
-
- puts "number: #{km_5.number}"
- puts "radians_ratio: #{km_5.radians_ratio}"
+describe GeoMagic::Distance::Unit do
+ context "5 km distance" do
+
+ subject { GeoMagic::Distance::Unit.new :km, 5 }
+
+ its(:number) { should > 0 }
+ its(:radians_ratio) { should > 0 }
end
end
View
42 spec/geo_magic/distance/vector_spec.rb
@@ -5,34 +5,28 @@
@long_dist = -0.3.km
@lat_dist = 0.05.km
end
+
+ context 'Distance vector' do
+ subject { GeoMagic::Distance::Vector.new @long_dist, @lat_dist }
- it "should create a distance vector" do
- dist_vector = GeoMagic::Distance::Vector.new @long_dist, @lat_dist
- dist_vector.long_distance.should_not be_nil
- dist_vector.lat_distance.should_not be_nil
- puts dist_vector.inspect
- end
-
- describe '#multiply!' do
- let (:dvector) do
- GeoMagic::Distance::Vector.new @long_dist, @lat_dist
- end
+ its(:long_distance) { should > 0 }
+ its(:lat_distance) { should > 0 }
- it "should multiply the radius distance" do
- dvector.multiply!(5)
- puts dvector.inspect
+ describe '#multiply' do
+ it "should multiply the radius distance and return new circle" do
+ old_dist = subject.long_distance
+ dvec = subject.multiply(0.5)
+ subject.long_distance.should == old_dist
+ dvec.long_distance.should < subject.long_distance
+ end
end
- end
- describe '#multiply' do
- let (:dvector) do
- GeoMagic::Distance::Vector.new @long_dist, @lat_dist
- end
-
- it "should multiply the radius distance and return new circle" do
- dvector.multiply!(0.5)
- puts dvector.inspect
+ describe '#multiply!' do
+ it "should multiply the radius distance" do
+ old_dist = subject.long_distance
+ subject.multiply!(5)
+ subject.long_distance.should > old_dist
+ end
end
end
-
end
View
45 spec/geo_magic/distance_spec.rb
@@ -1,30 +1,33 @@
require 'spec_helper'
describe GeoMagic::Distance do
- before do
- @long1 = -104.88544
- @lat1 = 39.06546
-
- @long2 = -104.80
- @lat2 = @lat1
- end
-
- it "calculates distance using array args - using algorithm haversine" do
- dist = GeoMagic::Distance.calculate [@long1, @lat1], [@long2, @lat2]
- puts dist
+ before do
+ a = [45.1, 11].to_point
+ b = [45.1, 11.1].to_point
+ c = [48.1, 12.1].to_point
+ @center = [45, 11].to_point
+ @points = [a, b, c]
end
- it "calculates distance using array args - using algorithm haversine" do
- dist = GeoMagic::Distance.calculate @long1, @lat1, @long2, @lat2
- km_dist = dist[:km]
- puts dist
- puts "km: #{km_dist}"
+ subject { 5.km }
+
+ describe 'Select subset of points within distance' do
+ describe '#select_within'
+ subject.select_within @points, @center
+ end
+
+ describe '#select_all #near'
+ subject.select_all(@points).near(@center)
+ end
end
- it "calculates distance using array args - using algorithm haversine" do
- dist = GeoMagic::Distance.calculate {:long => @long1, :lat => @lat1}, {:long => @long2, :lat => @lat2}
- km_dist = dist[:km]
- puts dist
- puts "km: #{km_dist}"
+ describe 'Reject subset of points within distance' do
+ describe '#reject_within'
+ subject.reject_near @points, @center
+ end
+
+ describe '#reject_all #within'
+ subject.reject_all(@points).near(@center)
+ end
end
end
View
0  spec/geo_magic/radius/within_radius_spec.rb
No changes.
Please sign in to comment.
Something went wrong with that request. Please try again.