Class: GeoDistance

Inherits:
Object
  • Object
show all
Includes:
Comparable, Extract, GeoUnits, GeoUnits::UnitMaps, NumericCheckExt
Defined in:
lib/geo_vectors/util/geo_distance.rb,
lib/geo_vectors/util/geo_distance/units.rb

Defined Under Namespace

Modules: Extract, Unit

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GeoUnits::UnitMaps

#earth_radius, #meters_map

Methods included from GeoUnits::Methods

#check_unit!, #default_unit, #default_unit=, #valid_unit?, #valid_units

Methods included from NumericCheckExt

#check_numeric!, #is_numeric?

Constructor Details

#initialize(number, unit = :kms) ⇒ GeoDistance

Returns a new instance of GeoDistance.



24
25
26
27
28
29
30
# File 'lib/geo_vectors/util/geo_distance.rb', line 24

def initialize number, unit = :kms
  check_unit! unit
  check_numeric! number 

  @unit = unit
  @number = number
end

Instance Attribute Details

#numberObject

Returns the value of attribute number.



22
23
24
# File 'lib/geo_vectors/util/geo_distance.rb', line 22

def number
  @number
end

#unitObject

Returns the value of attribute unit.



22
23
24
# File 'lib/geo_vectors/util/geo_distance.rb', line 22

def unit
  @unit
end

Instance Method Details

#*(factor) ⇒ Object



36
37
38
39
40
# File 'lib/geo_vectors/util/geo_distance.rb', line 36

def * factor  
  dist = self.dup
  dist.number *= factor
  dist
end

#/(factor) ⇒ Object



42
43
44
45
46
# File 'lib/geo_vectors/util/geo_distance.rb', line 42

def / factor  
  dist = self.dup
  dist.number /= factor
  dist
end

#<=>(dist) ⇒ Object

compare 2 distances



49
50
51
52
53
54
55
56
57
58
# File 'lib/geo_vectors/util/geo_distance.rb', line 49

def <=> dist
  dist = extract_distance(dist).as(unit)
  if number < dist.number
    -1
  elsif number > dist.number
    1
  else
    0
  end
end

#as(unit) ⇒ Object

convert to unit (see GeoMagic)



65
66
67
68
69
70
71
# File 'lib/geo_vectors/util/geo_distance.rb', line 65

def as unit
  check_unit! unit
  dist = self.dup
  dist.number = convert_to_meters * meters_map[unit]
  dist.unit = unit
  dist    
end

#convert_to_metersObject



60
61
62
# File 'lib/geo_vectors/util/geo_distance.rb', line 60

def convert_to_meters
  (unit == :radians) ? radians_to(:meters) : number / meters_map[unit]
end

#radians_to(unit) ⇒ Object



95
96
97
98
# File 'lib/geo_vectors/util/geo_distance.rb', line 95

def radians_to unit
  check_unit! unit
  earth_radius[unit] * number
end

#randomObject



91
92
93
# File 'lib/geo_vectors/util/geo_distance.rb', line 91

def random
  rand(number.to_f * 100) / 100.0
end

#to_sObject



32
33
34
# File 'lib/geo_vectors/util/geo_distance.rb', line 32

def to_s
  "distance: #{number} #{unit}"
end