ryanong / mongoid_spacial

A Mongoid Extention that simplifies and adds support for MongoDB Geo Spacial Calculations.
MIT License
138 stars 63 forks source link

Mongoid Spacial

A Mongoid Extention that simplifies and adds support for MongoDB Geo Spacial Calculations.

Quick Start

Add mongoid_spacial to your Gemfile:

gem 'mongoid_spacial'

Set up some slugs:

class River
  include Mongoid::Document
  include Mongoid::Spacial::Document

  field :name,              type: String
  field :length,            type: Integer
  field :average_discharge, type: Integer
  field :source,            type: Array,    spacial: true

  # set return_array to true if you do not want a hash returned all the time
  field :mouth,             type: Array,    spacial: {lat: :latitude, lng: :longitude, return_array: true }

  # simplified spacial indexing
  # you can only index one point in mongodb version below 1.9
  # if you want something besides the defaults {bit: 24, min: -180, max: 180} just set index to the options on the index
  spacial_index :source

end

Generate indexes on MongoDB:

rake db:mongoid:create_indexes

Before we manipulate the data mongoid_spacial handles is what we call points.

Points can be:

hudson = River.create(
  name: 'Hudson',
  length: 315,
  average_discharge: 21_400,
  # when setting array LONGITUDE MUST BE FIRST LATITUDE MUST BE SECOND
  # source: [-73.935833,44.106667],
  # but we can use hash in any order,
  # the default keys for latitude and longitude are :lat and :lng respectively
  source: {:lat => 44.106667, :lng => -73.935833},
  mouth: {:latitude => 40.703056, :longitude => -74.026667}
)

# now to access this spacial information we can now do this
hudson.source #=> {:lng => -73.935833, :lat => 44.106667}
hudson.mouth  #=> [-74.026667, 40.703056] # notice how this returned as a lng,lat array because return_array was true
# notice how the order of lng and lat were switched. it will always come out like this when using spacial.
# Also adds a handy distance function
hudson.distance_from(:source, [-74,40], {:unit=>:mi})

Mongoid Geo has extended all built in spacial symbol extentions

One of the most handy features we have added is geo_near finder

# accepts all criteria chains except without, only, asc, desc, order\_by
River.where(:name=>'hudson').geo_near({:lat => 40.73083, :lng => -73.99756})

# geo\_near accepts a few parameters besides a point
# :num = limit
# :query = where
# :unit - [:km, :m, :mi, :ft] - converts :max\_distance to appropriate values and automatically sets :distance\_multiplier. accepts
# :max\_distance - Integer
# :distance\_multiplier - Integer
# :spherical - true - To enable spherical calculations
River.geo_near([-73.99756,40.73083], :max_distance => 4, :unit => :mi, :spherical => true)

There are two types of pagination!

Post-Result is only minutely slower than MongoDB because MongoDB has to calculate distance for all of the rows anyway. The slow up is in the transfer of data from the database to ruby.

Post-Result has some advantages that are listed below.

# MongoDB pagination
# overwrites #skip chain method
# :page - pagination will be enabled if set to any variable including nil, pagination will not be enabled if either :per\_page or :paginator is set
#   :per\_page
#   :paginator - Choose which paginator to use. [default :arrary]
#     Prefered method to set is Mongoid::Spacial.paginator=:array
#     Available Paginators [:kaminari, :will\_paginate, :array]
#     The only thing this does really is configure default per\_page so it is only kind of useful
River.geo_near([-73.99756,40.73083], :page => 1)
# Post Query Pagination
# At carzen we use Post Query Pagination because we need to re-sort our rows after fetching. Pagination is not friendly with re-sorting.
# You can jump pages continously without querying the database again.
# listens to #limit/:num & #skip before geo\_near
# #page(page\_number, opts = {})
#  opts:
#   :per\_page
#   :paginator
# #per(per\_page\_number, opts = {})
#  opts:
#   :page
#   :paginator
#
# both return a GeoNearResults, which is really just a modified Array
# #per really just #page but just moves the options around
rivers = River.geo_near([-73.99756,40.73083]).sort_by!{|r| r.geo[:distance] * r.multiplier }
rivers = rivers.per(25).page(1)
rivers.reset! # resets the object to it is original state right after query.

Troubleshooting

Mongo::OperationFailure: can't find special index: 2d

Indexes need to be created. Execute command: rake db:mongoid:create_indexes

Thanks

Contributing to mongoid_spacial

Copyright

Copyright (c) 2011 Ryan Ong. See LICENSE.txt for further details.