Class: CacheableKdtree::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/cacheable_kdtree/util.rb

Constant Summary collapse

KILOMETERS_IN_A_MILE =
0.621371192
EARTH_KILOMETERS =
6371.0

Class Method Summary collapse

Class Method Details

.bounding_box(lat, long, radius_distance, sphere_radius) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/cacheable_kdtree/util.rb', line 16

def self.bounding_box(lat, long, radius_distance, sphere_radius)
  lat_amount = radians_to_degrees(radius_distance / sphere_radius)
  lat1 = lat + lat_amount
  lat2 = lat - lat_amount
  long_amount = radians_to_degrees(radius_distance / sphere_radius / Math.cos(degrees_to_radians(lat)))
  long1 = long - long_amount
  long2 = long + long_amount
  CacheableKdtree::LatitudeLongitudeRegion.new(lat1, long1, lat2, long2)
end

.bounding_box_kilometers(lat, long, distance_in_kilometers) ⇒ Object



12
13
14
# File 'lib/cacheable_kdtree/util.rb', line 12

def self.bounding_box_kilometers(lat, long, distance_in_kilometers)
  bounding_box(lat, long, distance_in_kilometers, EARTH_KILOMETERS)
end

.bounding_box_miles(lat, long, distance_in_miles) ⇒ Object



8
9
10
# File 'lib/cacheable_kdtree/util.rb', line 8

def self.bounding_box_miles(lat, long, distance_in_miles)
  bounding_box(lat, long, distance_in_miles, earth_miles)
end

.degrees_to_radians(degrees) ⇒ Object



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

def self.degrees_to_radians(degrees)
  degrees * Math::PI / 180
end

.distance_kilometers(p1_lat, p1_long, p2_lat, p2_long) ⇒ Object



26
27
28
# File 'lib/cacheable_kdtree/util.rb', line 26

def self.distance_kilometers(p1_lat, p1_long, p2_lat, p2_long)
  distance_law_of_cosines(p1_lat, p1_long, p2_lat, p2_long, EARTH_KILOMETERS)
end

.distance_law_of_cosines(p1_lat, p1_long, p2_lat, p2_long, sphere_radius) ⇒ Object

I am using the law of cosines because it is faster than Haversine…



35
36
37
38
39
40
41
42
# File 'lib/cacheable_kdtree/util.rb', line 35

def self.distance_law_of_cosines(p1_lat, p1_long, p2_lat, p2_long, sphere_radius)
  p1_lat = degrees_to_radians(p1_lat)
  p1_long = degrees_to_radians(p1_long)
  p2_lat = degrees_to_radians(p2_lat)
  p2_long = degrees_to_radians(p2_long)
  Math.acos(Math.sin(p1_lat) * Math.sin(p2_lat) +
            Math.cos(p1_lat) * Math.cos(p2_lat) * Math.cos(p2_long - p1_long)) * sphere_radius
end

.distance_miles(p1_lat, p1_long, p2_lat, p2_long) ⇒ Object



30
31
32
# File 'lib/cacheable_kdtree/util.rb', line 30

def self.distance_miles(p1_lat, p1_long, p2_lat, p2_long)
  distance_law_of_cosines(p1_lat, p1_long, p2_lat, p2_long, earth_miles)
end

.earth_milesObject



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

def self.earth_miles
  KILOMETERS_IN_A_MILE * EARTH_KILOMETERS
end

.radians_to_degrees(radians) ⇒ Object



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

def self.radians_to_degrees(radians)
  radians * 180 / Math::PI
end