Class: AIXM::XY

Inherits:
Object
  • Object
show all
Defined in:
lib/aixm/xy.rb

Overview

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,

Examples:

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')

See Also:

Constant Summary collapse

EARTH_RADIUS =
6_371_008.8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lat:, long:) ⇒ XY


21
22
23
# File 'lib/aixm/xy.rb', line 21

def initialize(lat:, long:)
  self.lat, self.long = lat, long
end

Instance Attribute Details

#lat(schema = nil) ⇒ String, Float


36
37
38
39
# File 'lib/aixm/xy.rb', line 36

def lat=(value)
  @lat = float_for value
  fail(ArgumentError, "invalid lat") unless (-90..90).include? @lat
end

#long(schema = nil) ⇒ Float, String


52
53
54
55
# File 'lib/aixm/xy.rb', line 52

def long=(value)
  @long = float_for value
  fail(ArgumentError, "invalid long") unless (-180..180).include? @long
end

Instance Method Details

#==(other) ⇒ Boolean


68
69
70
# File 'lib/aixm/xy.rb', line 68

def ==(other)
  other.is_a?(self.class) && lat == other.lat && long == other.long
end

#distance(other) ⇒ AIXM::D


73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/aixm/xy.rb', line 73

def distance(other)
  if self == other
    AIXM.d(0, :m)
  else
    value = 2 * EARTH_RADIUS * Math.asin(
      Math.sqrt(
        Math.sin((other.lat.to_rad - lat.to_rad) / 2) ** 2 +
          Math.cos(lat.to_rad) * Math.cos(other.lat.to_rad) *
          Math.sin((other.long.to_rad - long.to_rad) / 2) ** 2
      )
    )
    AIXM.d(value.round, :m)
  end
end

#inspectString


26
27
28
# File 'lib/aixm/xy.rb', line 26

def inspect
  %Q(#<#{self.class} #{to_s}>)
end

#to_sString


31
32
33
# File 'lib/aixm/xy.rb', line 31

def to_s
  [lat(:ofmx), long(:ofmx)].join(' ')
end