Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best tutorial for doing geospatial queries with Rails/Mongoid? [closed]

I'm using Mongoid in my Rails app. Are there any tutorials for how to store and query location data and fetch objects that lie in a certain radius of a location? I've been looking and have no found anything.

like image 549
John Avatar asked May 01 '11 00:05

John


1 Answers

Because, like me, I suspect many people learn better from examples, here is one:

Assuming you have created a 2d index on your geospatial Array field (see below for more detail), and let's say your model object is Place and the location field is :loc, a bounding box query could look like this:

# Bounding Box query
Place.where(:loc.within => { "$box" => [ [ 20.73083, 30.99756 ], [ 45.741404, 51.988135 ] ] }).count

In addition to the link posted by Gates VP above, be sure to also read this doc from Moingoid: http://mongoid.org/docs/querying/criteria.html. You will find more examples like the above there.

In addition, you need to ensure your index has been created. Read the following doc from Mongoid, and the corresponding MongoDB Doc:

http://mongoid.org/docs/indexing.html

http://www.mongodb.org/display/DOCS/Geospatial+Indexing

like image 195
mydoghasworms Avatar answered Oct 20 '22 20:10

mydoghasworms