geocoder icon indicating copy to clipboard operation
geocoder copied to clipboard

Mongoid integration

Open wiemann opened this issue 16 years ago • 11 comments

This isn't really a bug but more a feature request. Have you considered making your great plugin mongoid compatible?

wiemann avatar Nov 06 '10 18:11 wiemann

I haven't, mostly because I haven't tried MongoDB yet, but I will look into it.

alexreisner avatar Nov 11 '10 13:11 alexreisner

Sorry for hijacking this thread, but since there's already a topic I thought I'd post it here rather than create another one. Your README mentions including Geocoder::Model::Mongoid however, when doing that in model I get:

uninitialized constant Geocoder::Model (NameError)

And of course, Geocoder::Model is nowhere to be found. False advertising!

Perhaps you forgot to git add?

kiafaldorius avatar Mar 30 '11 04:03 kiafaldorius

Oops, I committed that documentation prematurely. Mongoid integration isn't done yet, but I hope to introduce it in April.

alexreisner avatar Mar 30 '11 04:03 alexreisner

ah, ok, thanks!

I actually tried extending Geocoder::ModelMethods and it seems to work for the basic functionality that I need (setting the lat and lng).

#include Geocoder::Model::Mongoid
extend Geocoder::ModelMethods

kiafaldorius avatar Mar 30 '11 04:03 kiafaldorius

Yes, it will sort of work because it has some method names in common with ActiveRecord. But obviously the distance calculations won't work, and really with Mongoid you should be storing latitude and longitude in a single attribute (array).

alexreisner avatar Mar 30 '11 05:03 alexreisner

Yeah, I expected more, but it works for now. Will be expecting the other stuff later.

You shouldn't be storing lat/lng in an array though, if anything it's a hash or an embedded document (which internally is still a hash). If you store it as an array, it makes querying based on lat or lng difficult and inefficient.

Stored as a hash, the query looks something like:

# Model.field :latlng, :type => Hash
Model.where(:'latlng.lat' => {:$gte => 12.30, :$lte => 32.10}, :'latlng.lng' => {:$gte => 45.6, :$lte => 65.4})

I'm not even sure how you would query based on them being in an array, but definitely more troublesome than as a hash. Also, I don't think indexes would work properly if they're in an array.

I'm willing to take a look at your approach once it's out though =]

Thanks!

kiafaldorius avatar Mar 30 '11 06:03 kiafaldorius

I'm not that familiar with MongoDB (and hence Mongoid) so you'll have to excuse me. After reading up on MongoDB's geospatial indexes I think I agree that the coordinates do not necessarily need to be in an array (I had mis-read that page a few weeks ago). So go ahead and use a hash, but make sure that the key order is lon, lat (not lat, lon) or the spherical calculations won't work (explained at the bottom of that page).

alexreisner avatar Mar 30 '11 17:03 alexreisner

Geocoder now supports Mongoid! (HEAD only, gem coming soon)

It should not be considered production-ready just yet, but it works. For example, you can set up a model like this:

# app/models/office.rb
class Office
  include Mongoid::Document
  field :name
  field :address
  field :coordinates, :type => Array

  include Geocoder::Model::Mongoid
  geocoded_by :address
end

Make sure the geospatial index is created:

rake db:mongoid:create_indexes

and then you can do things like this:

# fetch coordinates
o = Office.new(:name => "Google",
  :address => "1600 Amphitheatre Parkway, Mountain View, CA")
o.geocode
 # => [37.423232, -122.085569]
o.save

# find offices within 20 miles of San Jose
Office.near("San Jose, CA", 20).first
 # => #<Office _id: ...>

# miles from New York
o.distance_from(40,-74)
 # => 2567.04

It's basically the same interface as when you're using ActiveRecord. Coordinates are stored in a two-element array which you should never need to access directly (Geocoder should handle all the distance calculations for you).

If you have a chance to try it, let me know how it goes.

alexreisner avatar Mar 31 '11 18:03 alexreisner

Great! I just found about geocoder and I need mongoid support that you released an hour ago. I'll become the first user ;-)

clody69 avatar Mar 31 '11 20:03 clody69

I like the hashes because I always forget whether lat or long comes first. You may want to specify that the order is [lon, lat] in the README...

Thanks for adding Mongoid support! =]

kiafaldorius avatar Mar 31 '11 20:03 kiafaldorius

I like hashes better too, the order needs to be [lon, lat] in the database and hash order is not preserved in Ruby 1.8.

Unfortunately MongoDB's order follows the GeoJSON spec which is the opposite of every human-readable use of latitude/longitude I've seen. Hence, everywhere else in Geocoder it's [lat,lon]. In fact, you should probably forget that it's [lon,lat] internally because Geocoder always returns results as [lat,lon] and expects input to be in that order too, even when you're using Mongoid.

alexreisner avatar Mar 31 '11 21:03 alexreisner