lib/terraformer/coordinate.rb in terraformer-0.1.0 vs lib/terraformer/coordinate.rb in terraformer-0.2.0
- old
+ new
@@ -1,23 +1,30 @@
module Terraformer
- class Coordinate < ::Array
+ class Coordinate
# http://en.wikipedia.org/wiki/Earth_radius#Mean_radius
#
EARTH_MEAN_RADIUS = 6371009.to_d
attr_accessor :crs
+ # array holding the numeric coordinate values
+ attr_accessor :ary
+
class << self
- def from arys
- arys.map {|e| Coordinate.from_array e}
- end
-
def from_array a
- Coordinate.__send__ Numeric === a[0] ? :new : :from, a
+ if Coordinate === a
+ a.dup
+ elsif Numeric === a[0]
+ Coordinate.new a
+ elsif Array === a
+ a.map {|e| Coordinate.from_array e }
+ else
+ raise ArgumentError
+ end
end
def big_decimal n
case n
when String
@@ -32,55 +39,83 @@
end
end
def initialize _x, _y = nil, _z = nil
- super 3
- case
- when Array === _x
+ @ary = Array.new 3
+ case _x
+ when Array
raise ArgumentError if _y
self.x = _x[0]
self.y = _x[1]
self.z = _x[2] if _x[2]
- when Numeric === _x || String === _x
+
+ when Coordinate
+ raise ArgumentError if _y
+ self.x = _x.x
+ self.y = _x.y
+ self.z = _x.z if _x.z
+
+ when Numeric, String
raise ArgumentError unless _y
self.x = _x
self.y = _y
self.z = _z if _z
else
raise ArgumentError.new "invalid argument: #{_x}"
end
end
+ def dup
+ Coordinate.new @ary.dup
+ end
+
def x
- self[0]
+ @ary[0]
end
+ alias_method :lon, :x
def x= _x
- self[0] = Coordinate.big_decimal _x
+ @ary[0] = Coordinate.big_decimal _x
end
def y
- self[1]
+ @ary[1]
end
+ alias_method :lat, :y
def y= _y
- self[1] = Coordinate.big_decimal _y
+ @ary[1] = Coordinate.big_decimal _y
end
def z
- self[2]
+ @ary[2]
end
def z= _z
- self[2] = Coordinate.big_decimal _z
+ @ary[2] = Coordinate.big_decimal _z
end
- [:<<, :*, :&, :|].each do |sym|
- define_method(sym){|*a| raise NotImplementedError }
+ def [] index
+ @ary[index]
end
+ def == other
+ case other
+ when Array
+ @ary == other
+ when Coordinate
+ @ary == other.ary
+ else
+ false
+ end
+ end
+
+ def inspect
+ "#<Terraformer::Coordinate lon=#{lon.to_s 'F'} lat=#{lat.to_s 'F'} #{z if z }>"
+ end
+
def to_geographic
xerd = (x / EARTH_RADIUS).to_deg
_x = xerd - (((xerd + 180.0) / 360.0).floor * 360.0)
_y = (
(Math::PI / 2).to_d -
@@ -99,14 +134,14 @@
merc.crs = MERCATOR_CRS
merc
end
def to_s
- [x,y,z].compact.join ','
+ @ary.compact.join ','
end
def to_json *args
- compact.to_json(*args)
+ @ary.compact.to_json(*args)
end
def to_point
Point.new self
end