Class: CartesianForGeo::Point
- Inherits:
-
Object
- Object
- CartesianForGeo::Point
- Includes:
- Comparable
- Defined in:
- lib/cartesian_for_geo.rb
Overview
Class for one Point
Instance Attribute Summary collapse
-
#coords ⇒ Object
(also: #to_a, #lat_lng)
readonly
Returns the value of attribute coords.
-
#lat ⇒ Object
Returns the value of attribute lat.
-
#lng ⇒ Object
Returns the value of attribute lng.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(*coords) ⇒ Point
constructor
A new instance of Point.
- #lng_from_edge ⇒ Object
- #lng_lat ⇒ Object
- #side ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*coords) ⇒ Point
Returns a new instance of Point.
39 40 41 42 |
# File 'lib/cartesian_for_geo.rb', line 39 def initialize(*coords) @coords = coords.flatten @lat, @lng = @coords end |
Instance Attribute Details
#coords ⇒ Object (readonly) Also known as: to_a, lat_lng
Returns the value of attribute coords.
26 27 28 |
# File 'lib/cartesian_for_geo.rb', line 26 def coords @coords end |
#lat ⇒ Object
Returns the value of attribute lat.
25 26 27 |
# File 'lib/cartesian_for_geo.rb', line 25 def lat @lat end |
#lng ⇒ Object
Returns the value of attribute lng.
25 26 27 |
# File 'lib/cartesian_for_geo.rb', line 25 def lng @lng end |
Class Method Details
.parse(coords_text) ⇒ Object
34 35 36 |
# File 'lib/cartesian_for_geo.rb', line 34 def parse(coords_text) new *coords_text.gsub(/[()\s], ''/).split(',') end |
Instance Method Details
#<=>(other) ⇒ Object
72 73 74 |
# File 'lib/cartesian_for_geo.rb', line 72 def <=>(other) [to_a, other.to_a].map { |cord| cord.map { |f| f.round 9 } }.reduce(:<=>) end |
#==(other) ⇒ Object
68 69 70 |
# File 'lib/cartesian_for_geo.rb', line 68 def ==(other) super if other.is_a?(Point) end |
#empty? ⇒ Boolean
48 49 50 |
# File 'lib/cartesian_for_geo.rb', line 48 def empty? (lat && lng).nil? end |
#lng_from_edge ⇒ Object
52 53 54 |
# File 'lib/cartesian_for_geo.rb', line 52 def lng_from_edge 180 - lng.abs end |
#lng_lat ⇒ Object
60 61 62 |
# File 'lib/cartesian_for_geo.rb', line 60 def lng_lat coords.reverse end |
#side ⇒ Object
44 45 46 |
# File 'lib/cartesian_for_geo.rb', line 44 def side lng.negative? ? -1 : 1 end |
#to_json ⇒ Object
64 65 66 |
# File 'lib/cartesian_for_geo.rb', line 64 def to_json(*) JSON.generate(lat: lat, lng: lng) end |
#to_s ⇒ Object
56 57 58 |
# File 'lib/cartesian_for_geo.rb', line 56 def to_s empty? ? '' : "(#{lat},#{lng})" end |