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,

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

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


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

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

Instance Attribute Details

#lat(schema = nil) ⇒ String, Float


41
42
43
44
# File 'lib/aixm/xy.rb', line 41

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

#long(schema = nil) ⇒ Float, String


57
58
59
60
# File 'lib/aixm/xy.rb', line 57

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

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

See Also:

  • Object#==

101
102
103
# File 'lib/aixm/xy.rb', line 101

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

#distance(other) ⇒ AIXM::D


84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/aixm/xy.rb', line 84

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

#hashInteger

See Also:

  • Object#hash

108
109
110
# File 'lib/aixm/xy.rb', line 108

def hash
  to_s.hash
end

#inspectString


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

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

#seconds?Boolean


74
75
76
# File 'lib/aixm/xy.rb', line 74

def seconds?
  !(long.to_dms[-6,5].to_f.zero? && lat.to_dms[-6,5].to_f.zero?)
end

#to_pointAIXM::Component::Geometry::Point


79
80
81
# File 'lib/aixm/xy.rb', line 79

def to_point
  AIXM.point(xy: self)
end

#to_sString


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

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