-
Notifications
You must be signed in to change notification settings - Fork 19
/
class_methods.rb
58 lines (47 loc) · 1.46 KB
/
class_methods.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class GeoDistance
# 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
module ClassMethods
# radius of the great circle in miles
# radius in kilometers...some algorithms use 6367
def earth_radius units
GeoDistance.EARTH_RADIUS[units.to_sym]
end
def radians_per_degree
0.017453293 # PI/180
end
def units
[:feet, :meters, :kms, :miles]
end
def radians_ratio unit
GeoDistance.radians_per_degree * earth_radius[unit]
end
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
def distance( lat1, lon1, lat2, lon2)
klass = case default_algorithm
when :flat
GeoDistance::Flat
when :haversine
GeoDistance::Haversine
when :spherical
GeoDistance::Spherical
when :vincenty
GeoDistance::Vincenty
else
raise ArgumentError, "Not a valid algorithm. Must be one of: #{algorithms}"
end
klass.distance lat1, lon1, lat2, lon2
end
def default_algorithm
@default_algorithm || :haversine
end
protected
def algorithms
[:flat, :haversine, :spherical, :vincenty]
end
end
extend ClassMethods
end