Module: Protocol::Redis::Methods::Geospatial

Defined in:
lib/protocol/redis/methods/geospatial.rb

Overview

Methods for managing Redis geospatial indexes.

Instance Method Summary collapse

Instance Method Details

#geoadd(key, longitude, latitude, member, *arguments) ⇒ Object

Add one or more geospatial items in the geospatial index represented using a sorted set. O(log(N)) for each item added, where N is the number of elements in the sorted set. See <redis.io/commands/geoadd> for more details.



14
15
16
# File 'lib/protocol/redis/methods/geospatial.rb', line 14

def geoadd(key, longitude, latitude, member, *arguments)
  call("GEOADD", key, longitude, latitude, member, *arguments)
end

#geodist(key, from, to, unit = "m") ⇒ Object

Returns the distance between two members of a geospatial index. O(log(N)). See <redis.io/commands/geodist> for more details.



40
41
42
# File 'lib/protocol/redis/methods/geospatial.rb', line 40

def geodist(key, from, to, unit = "m")
  call("GEODIST", key, from, to, unit)
end

#geohash(key, member, *members) ⇒ Object

Returns members of a geospatial index as standard geohash strings. O(log(N)) for each member requested, where N is the number of elements in the sorted set. See <redis.io/commands/geohash> for more details.



22
23
24
# File 'lib/protocol/redis/methods/geospatial.rb', line 22

def geohash(key, member, *members)
  call("GEOHASH", key, member, *members)
end

#geopos(key, member, *members) ⇒ Object

Returns longitude and latitude of members of a geospatial index. O(log(N)) for each member requested, where N is the number of elements in the sorted set. See <redis.io/commands/geopos> for more details.



30
31
32
# File 'lib/protocol/redis/methods/geospatial.rb', line 30

def geopos(key, member, *members)
  call("GEOPOS", key, member, *members)
end

#georadius(key, longitude, latitude, radius, unit = "m", with_coordinates: false, with_distance: false, with_hash: false, count: nil, order: nil, store: nil, store_distance: nil) ⇒ Object

Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point. O(N+log(M)) where N is the number of elements inside the bounding box of the circular area delimited by center and radius and M is the number of items inside the index. See <redis.io/commands/georadius> for more details.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/protocol/redis/methods/geospatial.rb', line 58

def georadius(key, longitude, latitude, radius, unit = "m", with_coordinates: false, with_distance: false, with_hash: false, count: nil, order: nil, store: nil, store_distance: nil)
  arguments = [key, longitude, latitude, radius, unit]
  
  if with_coordinates
    arguments.append("WITHCOORD")
  end
  
  if with_distance
    arguments.append("WITHDIST")
  end
  
  if with_hash
    arguments.append("WITHHASH")
  end
  
  if count
    arguments.append("COUNT", count)
  end
  
  if order
    arguments.append(order)
  end
  
  readonly = true
  
  if store
    arguments.append("STORE", store)
    readonly = false
  end
  
  if store_distance
    arguments.append("STOREDIST", store_distance)
    readonly = false
  end
  
  # https://redis.io/commands/georadius#read-only-variants
  if readonly
    call("GEORADIUS_RO", *arguments)
  else
    call("GEORADIUS", *arguments)
  end
end

#georadiusbymember(key, member, radius, unit = "m", with_coordinates: false, with_distance: false, with_hash: false, count: nil, order: nil, store: nil, store_distance: nil) ⇒ Object

Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member. O(N+log(M)) where N is the number of elements inside the bounding box of the circular area delimited by center and radius and M is the number of items inside the index. See <redis.io/commands/georadiusbymember> for more details.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/protocol/redis/methods/geospatial.rb', line 114

def georadiusbymember(key, member, radius, unit = "m", with_coordinates: false, with_distance: false, with_hash: false, count: nil, order: nil, store: nil, store_distance: nil)
  arguments = [key, member, radius, unit]
  
  if with_coordinates
    arguments.append("WITHCOORD")
  end
  
  if with_distance
    arguments.append("WITHDIST")
  end
  
  if with_hash
    arguments.append("WITHHASH")
  end
  
  if count
    arguments.append("COUNT", count)
  end
  
  if order
    arguments.append(order)
  end
  
  readonly = true
  
  if store
    arguments.append("STORE", store)
    readonly = false
  end
  
  if store_distance
    arguments.append("STOREDIST", store_distance)
    readonly = false
  end
  
  # https://redis.io/commands/georadius#read-only-variants
  if readonly
    call("GEORADIUSBYMEMBER_RO", *arguments)
  else
    call("GEORADIUSBYMEMBER", *arguments)
  end
end