class AIXM::XY
Geographical coordinates
Recognized notations:¶ ↑
-
DD - examples: 12.12345678 (north or east), -12.12345678 (south or west)
-
DMS - examples: 11°22'33.44“N, 1112233.44W,
@example All of the below are equivalent
AIXM.xy(lat: 11.375955555555556, long: -111.37595555555555) AIXM.xy(lat: %q(11°22'33.44"), long: %q(-111°22'33.44")) AIXM.xy(lat: %q(11°22'33.44N"), long: %q(111°22'33.44W")) AIXM.xy(lat: '112233.44N', long: '1112233.44W')
Constants:¶ ↑
-
AIXM::MIN
- characters recognized as DMS minute symbols -
AIXM::SEC
- characters recognized as DMS second symbols -
AIXM::DMS_RE
- regular expression to match DMS coordinate notations
Constants
- EARTH_RADIUS
Public Class Methods
# File lib/aixm/xy.rb 26 def initialize(lat:, long:) 27 self.lat, self.long = lat, long 28 end
Public Instance Methods
@see Object#== @return [Boolean]
# File lib/aixm/xy.rb 101 def ==(other) 102 self.class === other && lat == other.lat && long == other.long 103 end
@return [AIXM::D] distance as calculated by use of the Haversine formula
# File lib/aixm/xy.rb 84 def distance(other) 85 if self == other 86 AIXM.d(0, :m) 87 else 88 value = 2 * EARTH_RADIUS * Math.asin( 89 Math.sqrt( 90 Math.sin((other.lat.to_rad - lat.to_rad) / 2) ** 2 + 91 Math.cos(lat.to_rad) * Math.cos(other.lat.to_rad) * 92 Math.sin((other.long.to_rad - long.to_rad) / 2) ** 2 93 ) 94 ) 95 AIXM.d(value.round, :m) 96 end 97 end
@see Object#hash @return [Integer]
# File lib/aixm/xy.rb 108 def hash 109 to_s.hash 110 end
@return [String]
# File lib/aixm/xy.rb 31 def inspect 32 %Q(#<#{self.class} #{to_s}>) 33 end
@param schema [Symbol, nil] either nil, :aixm
or :ofmx
@return [String, Float] latitude
# File lib/aixm/xy.rb 48 def lat(schema=nil) 49 case schema 50 when :ofmx then ("%011.8f" % @lat.abs.round(8)) + (@lat.negative? ? 'S' : 'N') 51 when :aixm then @lat.to_dms(2).gsub(/[^\d.]/, '') + (@lat.negative? ? 'S' : 'N') 52 else @lat.round(8) 53 end 54 end
@!attribute lat
# File lib/aixm/xy.rb 41 def lat=(value) 42 @lat = float_for value 43 fail(ArgumentError, "invalid lat") unless (-90..90).include? @lat 44 end
@param schema [Symbol, nil] either nil, :aixm
or :ofmx
@return [Float, String] longitude
# File lib/aixm/xy.rb 64 def long(schema=nil) 65 case schema 66 when :ofmx then ("%012.8f" % @long.abs.round(8)) + (@long.negative? ? 'W' : 'E') 67 when :aixm then @long.to_dms(3).gsub(/[^\d.]/, '') + (@long.negative? ? 'W' : 'E') 68 else @long.round(8) 69 end 70 end
@!attribute long
# File lib/aixm/xy.rb 57 def long=(value) 58 @long = float_for value 59 fail(ArgumentError, "invalid long") unless (-180..180).include? @long 60 end
@return [Boolean] false
if both longitude and latitude have zero DMS
seconds which may indicate rounded or estimated coordinates
# File lib/aixm/xy.rb 74 def seconds? 75 !(long.to_dms[-6,5].to_f.zero? && lat.to_dms[-6,5].to_f.zero?) 76 end
@return [AIXM::Component::Geometry::Point] convert to point
# File lib/aixm/xy.rb 79 def to_point 80 AIXM.point(xy: self) 81 end
@return [String] human readable representation
# File lib/aixm/xy.rb 36 def to_s 37 [lat(:ofmx), long(:ofmx)].join(' ') 38 end
Private Instance Methods
# File lib/aixm/xy.rb 114 def float_for(value) 115 case value 116 when Numeric then value.to_f 117 when String then value.to_dd 118 else fail(ArgumentError, "invalid value class `#{value.class}'") 119 end 120 rescue 121 fail(ArgumentError, "invalid value `#{value}'") 122 end