Skip to content

Commit

Permalink
rectangular radius starting to work
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Feb 9, 2011
1 parent b002f69 commit b87064c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 17 deletions.
52 changes: 44 additions & 8 deletions lib/geo_magic/distance/vector.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'sugar-high/kind_of'

module GeoMagic
class Distance
class Vector
attr_accessor :lat_distance, :long_distance

# should be Distance objects!
def initialize lat_distance, long_distance
raise ArgumentError "lat and long distance arguments must be instances of GeoMagic::Distance" if ![long_distance, lat_distance].kinds_of? GeoMagic::Distance
raise ArgumentError, "lat and long distance arguments must be instances of GeoMagic::Distance, was: #{lat_distance} #{long_distance}" if ![long_distance, lat_distance].only_kinds_of? GeoMagic::Distance
@lat_distance = lat_distance
@long_distance = long_distance
end
Expand All @@ -15,7 +17,7 @@ def multiply arg
when Fixnum
[arg, arg]
when Hash
[factor(arg, [:lat, :latitude]), factor(arg, [:long, :longitude])]
[factor(arg, lat_symbols), factor(arg, long_symbols)]
else
raise ArgumentError, "Argument must be a Fixnum or a Hash specifying factor to multiply latitude and/or longitude with" if !arg.kind_of?
end
Expand All @@ -37,17 +39,51 @@ def radius center
GeoMagic::Radius.send :"create_rectangular", center, self
end

# should create distance vector from 2 points
def self.create_from *args
# extract points
# calc distance
end

def to_s
"distances: (lat: #{lat_distance}, long: #{long_distance})"
end

protected

include GeoMagic::GeoSymbols

class << self

include GeoMagic::GeoSymbols

# should create distance vector from 2 points
def create_from *args
case args.size
when 1
arg = args.first
return arg if arg.kind_of? self.class
new *extract_from_hash(arg)
when 2
new *args
else
raise ArgumentError, "Too many arguments! #{args}"
end
end

protected

def extract_from_hash hash
raise ArgumentError, "single argument must be a hash" if !hash.kind_of? Hash
[lat_dist(hash), long_dist(hash)]
end

def lat_dist hash
sym = lat_symbols.select {|s| hash[s].kind_of? GeoMagic::Distance }.first
raise ArgumentError, "latitude could not be extracted from hash" if !sym
hash[sym]
end

def long_dist hash
sym = long_symbols.select {|s| hash[s].kind_of? GeoMagic::Distance }.first
raise ArgumentError, "longitude could not be extracted from hash" if !sym
hash[sym]
end
end

def factor hash, symbols
s = symbols.select {|s| hash[s].kind_of? Fixnum }
Expand Down
9 changes: 1 addition & 8 deletions lib/geo_magic/point.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'geo_magic/point/class_methods'
require 'geo_magic/point/conversion'
require 'geo_magic/point/random'
require 'geo_magic/point/geo_symbols'

module GeoMagic
class Point
Expand Down Expand Up @@ -58,14 +59,6 @@ def to_location

def to_s
"(lat: #{latitude}, long: #{longitude})"
end

protected

def extract_from_hash hash
raise ArgumentError, "single argument must be a hash" if !hash.kind_of? Hash

end

end
end
11 changes: 11 additions & 0 deletions lib/geo_magic/point/geo_symbols.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module GeoMagic
module GeoSymbols
def lat_symbols
[:lat, :latitude]
end

def long_symbols
[:long, :lng, :longitude]
end
end
end
1 change: 1 addition & 0 deletions lib/geo_magic/radius/rectangular.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class RectangularRadius < Radius

def initialize center, vector_distance
super center
vector_distance = GeoMagic::Distance::Vector.create_from vector_distance
raise ArgumentError, "#{self.class} radius distance must be a Distance::Vector with lat and long distance" if !vector_distance.kind_of? GeoMagic::Distance::Vector
@vector_distance = vector_distance
end
Expand Down
2 changes: 1 addition & 1 deletion spec/geo_magic/radius/rectangular_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
end

let(:vector_distance) do
{:lat => 1.2, :long => 0.4}
{:lat => 1.2.km, :long => 0.4.km}
end

it "should create a new rectangular radius" do
Expand Down
Empty file added spec/geo_magic/radius_spec.rb
Empty file.

0 comments on commit b87064c

Please sign in to comment.