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)
readonly
Returns the value of attribute coords.
-
#lat ⇒ Object
Returns the value of attribute lat.
-
#lng ⇒ Object
Returns the value of attribute lng.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(*coords) ⇒ Point
constructor
A new instance of Point.
- #lat_lng! ⇒ Object
- #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.
38 39 40 41 42 |
# File 'lib/cartesian_for_geo.rb', line 38 def initialize(*coords) @coords = coords.flatten @lat, @lng = @coords @order = :lat_lng end |
Instance Attribute Details
#coords ⇒ Object (readonly) Also known as: to_a
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 |
#order ⇒ Object (readonly)
Returns the value of attribute order.
26 27 28 |
# File 'lib/cartesian_for_geo.rb', line 26 def order @order end |
Class Method Details
.parse(coords_text) ⇒ Object
33 34 35 |
# File 'lib/cartesian_for_geo.rb', line 33 def parse(coords_text) new *coords_text.gsub(/[()\s]/, '').split(',').map(&:to_f) end |
Instance Method Details
#<=>(other) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/cartesian_for_geo.rb', line 80 def <=>(other) [ to_a, other.to_a.public_send(order == other.order ? :itself : :reverse) ].map { |cord| cord.map { |f| f.round 9 } }.reduce(:<=>) end |
#==(other) ⇒ Object
76 77 78 |
# File 'lib/cartesian_for_geo.rb', line 76 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 |
#lat_lng! ⇒ Object
66 67 68 69 70 |
# File 'lib/cartesian_for_geo.rb', line 66 def lat_lng! @order = :lat_lng @coords = [@lat, @lng] self 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 63 64 |
# File 'lib/cartesian_for_geo.rb', line 60 def lng_lat! @order = :lng_lat @coords = [@lng, @lat] self end |
#side ⇒ Object
44 45 46 |
# File 'lib/cartesian_for_geo.rb', line 44 def side lng.negative? ? -1 : 1 end |
#to_json ⇒ Object
72 73 74 |
# File 'lib/cartesian_for_geo.rb', line 72 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? ? '' : "(#{coords.join(',')})" end |