class AIXM::XY

Geographical coordinates

Recognized notations:

@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:

@see gitlab.com/openflightmaps/ofmx/wikis/Coordinates

Constants

EARTH_RADIUS

Public Class Methods

new(lat:, long:) click to toggle source
   # File lib/aixm/xy.rb
26 def initialize(lat:, long:)
27   self.lat, self.long = lat, long
28 end

Public Instance Methods

==(other) click to toggle source

@see Object#== @return [Boolean]

    # File lib/aixm/xy.rb
101 def ==(other)
102   self.class === other && lat == other.lat && long == other.long
103 end
Also aliased as: eql?
distance(other) click to toggle source

@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
eql?(other)
Alias for: ==
hash() click to toggle source

@see Object#hash @return [Integer]

    # File lib/aixm/xy.rb
108 def hash
109   to_s.hash
110 end
inspect() click to toggle source

@return [String]

   # File lib/aixm/xy.rb
31 def inspect
32   %Q(#<#{self.class} #{to_s}>)
33 end
lat(schema=nil) click to toggle source

@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
lat=(value) click to toggle source

@!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
long(schema=nil) click to toggle source

@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
long=(value) click to toggle source

@!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
seconds?() click to toggle source

@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
to_point() click to toggle source

@return [AIXM::Component::Geometry::Point] convert to point

   # File lib/aixm/xy.rb
79 def to_point
80   AIXM.point(xy: self)
81 end
to_s() click to toggle source

@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

float_for(value) click to toggle source
    # 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