Class: CartesianForGeo::Point

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/cartesian_for_geo.rb

Overview

Class for one Point

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#coordsObject (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

#latObject

Returns the value of attribute lat.



25
26
27
# File 'lib/cartesian_for_geo.rb', line 25

def lat
  @lat
end

#lngObject

Returns the value of attribute lng.



25
26
27
# File 'lib/cartesian_for_geo.rb', line 25

def lng
  @lng
end

#orderObject (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

Returns:

  • (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_edgeObject



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

#sideObject



44
45
46
# File 'lib/cartesian_for_geo.rb', line 44

def side
	lng.negative? ? -1 : 1
end

#to_jsonObject



72
73
74
# File 'lib/cartesian_for_geo.rb', line 72

def to_json(*)
	JSON.generate(lat: lat, lng: lng)
end

#to_sObject



56
57
58
# File 'lib/cartesian_for_geo.rb', line 56

def to_s
	empty? ? '' : "(#{coords.join(',')})"
end