class FakeRedis::GeoSet::Point
Constants
- BASE32
- EARTH_RADIUS_IN_M
Attributes
lat[R]
lon[R]
name[R]
Public Class Methods
new(lon, lat, name)
click to toggle source
# File lib/fakeredis/geo_set.rb, line 9 def initialize(lon, lat, name) @lon = Float(lon) @lat = Float(lat) @name = name end
Public Instance Methods
distance_to(other)
click to toggle source
# File lib/fakeredis/geo_set.rb, line 40 def distance_to(other) lat1 = deg_to_rad(@lat) lon1 = deg_to_rad(@lon) lat2 = deg_to_rad(other.lat) lon2 = deg_to_rad(other.lon) haversine_distance(lat1, lon1, lat2, lon2) end
geohash(precision = 10)
click to toggle source
# File lib/fakeredis/geo_set.rb, line 15 def geohash(precision = 10) latlon = [@lat, @lon] ranges = [[-90.0, 90.0], [-180.0, 180.0]] coordinate = 1 (0...precision).map do index = 0 # index into base32 map 5.times do |bit| mid = (ranges[coordinate][0] + ranges[coordinate][1]) / 2 if latlon[coordinate] >= mid index = index * 2 + 1 ranges[coordinate][0] = mid else index *= 2 ranges[coordinate][1] = mid end coordinate ^= 1 end BASE32[index] end.join end
Private Instance Methods
deg_to_rad(deg)
click to toggle source
# File lib/fakeredis/geo_set.rb, line 50 def deg_to_rad(deg) deg * Math::PI / 180.0 end
haversine_distance(lat1, lon1, lat2, lon2)
click to toggle source
# File lib/fakeredis/geo_set.rb, line 54 def haversine_distance(lat1, lon1, lat2, lon2) h = Math.sin((lat2 - lat1) / 2) ** 2 + Math.cos(lat1) * Math.cos(lat2) * Math.sin((lon2 - lon1) / 2) ** 2 2 * EARTH_RADIUS_IN_M * Math.asin(Math.sqrt(h)) end