Module: GeoDistance::ClassMethods

Included in:
GeoDistance
Defined in:
lib/geo-distance/class_methods.rb

Overview

this is global because if computing lots of track point distances, it didn’t make sense to new a Hash each time over potentially 100’s of thousands of points

Instance Method Summary collapse

Instance Method Details

#algorithmsObject



65
66
67
# File 'lib/geo-distance/class_methods.rb', line 65

def algorithms
  [:flat, :haversine, :spherical, :vincenty, :nvector]
end

#all_unitsObject



57
58
59
# File 'lib/geo-distance/class_methods.rb', line 57

def all_units
  GeoUnits.all_units
end

#default_algorithmObject



41
42
43
# File 'lib/geo-distance/class_methods.rb', line 41

def default_algorithm 
  @default_algorithm || :haversine
end

#default_algorithm=(name) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
# File 'lib/geo-distance/class_methods.rb', line 36

def default_algorithm= name
  raise ArgumentError, "Not a valid algorithm. Must be one of: #{algorithms}" if !algorithms.include?(name.to_sym)
  @default_algorithm = name 
end

#default_unitsObject



32
33
34
# File 'lib/geo-distance/class_methods.rb', line 32

def default_units
  @default_units || :kms
end

#default_units=(name) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
# File 'lib/geo-distance/class_methods.rb', line 27

def default_units= name
  raise ArgumentError, "Not a valid units. Must be one of: #{all_units}" if !all_units.include?(name.to_sym)
  @default_units = GeoUnits.key(name)
end

#distance(*args) ⇒ Object

radius of the great circle in miles radius in kilometers…some algorithms use 6367



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/geo-distance/class_methods.rb', line 9

def distance(*args) 
  klass = case default_algorithm
  when :flat
    GeoDistance::Flat
  when :haversine
    GeoDistance::Haversine
  when :spherical
    GeoDistance::Spherical        
  when :vincenty
    GeoDistance::Vincenty
  when :nvector
    GeoDistance::NVector
  else
    raise ArgumentError, "Not a valid algorithm. Must be one of: #{algorithms}"
  end
  klass.distance *args
end

#earth_radius(units) ⇒ Object



45
46
47
# File 'lib/geo-distance/class_methods.rb', line 45

def earth_radius units
  GeoUnits.earth_radius units
end

#radians_per_degreeObject



49
50
51
# File 'lib/geo-distance/class_methods.rb', line 49

def radians_per_degree
  0.017453293  #  PI/180
end

#radians_ratio(unit) ⇒ Object



53
54
55
# File 'lib/geo-distance/class_methods.rb', line 53

def radians_ratio unit
  GeoDistance.radians_per_degree * earth_radius[unit]          
end

#unitsObject



61
62
63
# File 'lib/geo-distance/class_methods.rb', line 61

def units 
  GeoUnits.units
end