Permalink
Browse files

rectangular radius starting to work

  • Loading branch information...
1 parent b002f69 commit b87064c6e4989952c9c1f6a28823fc902dbc7f9f @kristianmandrup committed Feb 9, 2011
@@ -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
@@ -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
@@ -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 }
View
@@ -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
@@ -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
@@ -0,0 +1,11 @@
+module GeoMagic
+ module GeoSymbols
+ def lat_symbols
+ [:lat, :latitude]
+ end
+
+ def long_symbols
+ [:long, :lng, :longitude]
+ end
+ end
+end
@@ -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
@@ -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
No changes.

0 comments on commit b87064c

Please sign in to comment.