ohm-geoindex
In development. Requires Redis >= 3.2.
A plugin for Ohm models which enables basic geospatial queries via the Redis geohashing API.
Installation
Add the gem:
gem 'ohm-geoindex', require: 'ohm/geoindex'
Usage
Include the module in your model and specify the attributes to be indexed. Note that [longitude, latitude]
is the ordering convention used throughout the Redis geo API.
class Beach < Ohm::Model
include Ohm::Geoindex
attribute :latitude
attribute :longitude
geoindex [:longitude, :latitude]
end
To perform a radius query, use the within
class method.
>> @manly = Beach.create(latitude: -33.797948, longitude: 151.289414)
>> @bondi = Beach.create(latitude: -33.891472, longitude: 151.277243)
>> @coogee = Beach.create(latitude: -33.921017, longitude: 151.257566) # ~14km from manly
>> Beach.within(@coogee, '10 mi', sort: 'desc')
=> [@manly, @bondi, @coogee]
>> Beach.within(@coogee, '10 km', sort: 'asc', withdist: true)
=> [[@coogee, 0.0], [@bondi, 4.122]
>> Beach.within([151.257566, -33.921017], '10 mi', sort: 'asc') # coords are @coogee's
=> [@coogee, @bondi, @manly]
>> Beach.within([151.257566, -33.921017], '10 mi', sort: 'asc', limit: 2) # coords are @coogee's
=> [@coogee, @bondi]
See the Redis docs for GEORADIUS
and GEOADD
for allowed unit syntax.
Tests
Because the geo API is only currently available in Redis' unstable branch, rake test
attempts to start a local redis binary at ./test/redis-server port 7771 for the test run.
Requirements
This plugin works with Ohm >= 2.2.0.
Changelog
0.0.3
- Add support for
count
(limit) parameter - README fixes
0.0.2
- Correct implicit expectation of indexed attribute names.
0.0.1
- Initial release