Class: Geospatial::Distance

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

Overview

This location is specifically relating to a WGS84 coordinate on Earth.

Constant Summary collapse

UNITS =
['m', 'km'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Distance

Distance in meters:



27
28
29
30
# File 'lib/geospatial/distance.rb', line 27

def initialize(value)
	@value = value
	@formatted_value = nil
end

Instance Method Details

#*(other) ⇒ Object



70
71
72
# File 'lib/geospatial/distance.rb', line 70

def * other
	Distance.new(@value * other.to_f)
end

#+(other) ⇒ Object



62
63
64
# File 'lib/geospatial/distance.rb', line 62

def + other
	Distance.new(@value + other.to_f)
end

#-(other) ⇒ Object



66
67
68
# File 'lib/geospatial/distance.rb', line 66

def - other
	Distance.new(@value - other.to_f)
end

#/(other) ⇒ Object



74
75
76
# File 'lib/geospatial/distance.rb', line 74

def / other
	Distance.new(@value / other.to_f)
end

#<=>(other) ⇒ Object



82
83
84
# File 'lib/geospatial/distance.rb', line 82

def <=> other
	@value <=> other.to_f
end

#==(other) ⇒ Object



78
79
80
# File 'lib/geospatial/distance.rb', line 78

def == other
	@value == other.to_f
end

#formatted_valueObject Also known as: to_s



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/geospatial/distance.rb', line 40

def formatted_value
	unless @formatted_value
		scale = 0
		value = @value
		
		while value > 1000 and scale < UNITS.size
			value /= 1000.0
			scale += 1
		end
		
		@formatted_value = sprintf("%0.#{scale}f%s", value, UNITS.fetch(scale))
	end
	
	return @formatted_value
end

#freezeObject



32
33
34
35
36
# File 'lib/geospatial/distance.rb', line 32

def freeze
	formatted_value
	
	super
end

#to_fObject



58
59
60
# File 'lib/geospatial/distance.rb', line 58

def to_f
	@value
end