Class: AIXM::XY
- Inherits:
-
Object
- Object
- AIXM::XY
- 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
Constant Summary collapse
- EARTH_RADIUS =
6_371_008.8
Instance Attribute Summary collapse
-
#lat(schema = nil) ⇒ String, Float
Latitude.
-
#long(schema = nil) ⇒ Float, String
Longitude.
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
-
#distance(other) ⇒ AIXM::D
Distance as calculated by use of the Haversine formula.
- #hash ⇒ Integer
-
#initialize(lat:, long:) ⇒ XY
constructor
A new instance of XY.
- #inspect ⇒ String
-
#seconds? ⇒ Boolean
falseif both longitude and latitude have zero DMS seconds which may indicate rounded or estimated coordinates. -
#to_point ⇒ AIXM::Component::Geometry::Point
Convert to point.
-
#to_s ⇒ String
Human readable representation.
Constructor Details
#initialize(lat:, long:) ⇒ XY
Returns a new instance of 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
Returns latitude.
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
Returns longitude.
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?
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
Returns distance as calculated by use of the Haversine formula.
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 |
#hash ⇒ Integer
108 109 110 |
# File 'lib/aixm/xy.rb', line 108 def hash to_s.hash end |
#inspect ⇒ String
31 32 33 |
# File 'lib/aixm/xy.rb', line 31 def inspect %Q(#<#{self.class} #{to_s}>) end |
#seconds? ⇒ Boolean
Returns false if both longitude and latitude have zero DMS seconds which may indicate rounded or estimated coordinates.
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_point ⇒ AIXM::Component::Geometry::Point
Returns convert to point.
79 80 81 |
# File 'lib/aixm/xy.rb', line 79 def to_point AIXM.point(xy: self) end |
#to_s ⇒ String
Returns human readable representation.
36 37 38 |
# File 'lib/aixm/xy.rb', line 36 def to_s [lat(:ofmx), long(:ofmx)].join(' ') end |