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.



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

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

#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

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

Returns:

  • (Boolean)


48
49
50
# File 'lib/cartesian_for_geo.rb', line 48

def empty?
	(lat && lng).nil?
end

#lng_from_edgeObject



52
53
54
# File 'lib/cartesian_for_geo.rb', line 52

def lng_from_edge
	180 - lng.abs
end

#lng_latObject



60
61
62
# File 'lib/cartesian_for_geo.rb', line 60

def lng_lat
	coords.reverse
end

#sideObject



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

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

#to_jsonObject



64
65
66
# File 'lib/cartesian_for_geo.rb', line 64

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? ? '' : "(#{lat},#{lng})"
end