Class: FakeRedis::GeoSet::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/fakeredis/geo_set.rb

Constant Summary collapse

BASE32 =

(geohash-specific) Base32 map

"0123456789bcdefghjkmnpqrstuvwxyz"
EARTH_RADIUS_IN_M =
6_378_100.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lon, lat, name) ⇒ Point

Returns a new instance of Point.


9
10
11
12
13
# File 'lib/fakeredis/geo_set.rb', line 9

def initialize(lon, lat, name)
  @lon = Float(lon)
  @lat = Float(lat)
  @name = name
end

Instance Attribute Details

#latObject (readonly)

Returns the value of attribute lat


7
8
9
# File 'lib/fakeredis/geo_set.rb', line 7

def lat
  @lat
end

#lonObject (readonly)

Returns the value of attribute lon


7
8
9
# File 'lib/fakeredis/geo_set.rb', line 7

def lon
  @lon
end

#nameObject (readonly)

Returns the value of attribute name


7
8
9
# File 'lib/fakeredis/geo_set.rb', line 7

def name
  @name
end

Instance Method Details

#distance_to(other) ⇒ Object


40
41
42
43
44
45
46
# 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) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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