sig_mongoid_spacial 0.2.17
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +49 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/README.md +180 -0
- data/Rakefile +18 -0
- data/lib/mongoid_spacial.rb +11 -0
- data/lib/mongoid_spacial/contexts/mongo.rb +115 -0
- data/lib/mongoid_spacial/criteria.rb +5 -0
- data/lib/mongoid_spacial/criterion.rb +3 -0
- data/lib/mongoid_spacial/criterion/complex.rb +19 -0
- data/lib/mongoid_spacial/criterion/inclusion.rb +14 -0
- data/lib/mongoid_spacial/criterion/near_spacial.rb +50 -0
- data/lib/mongoid_spacial/criterion/within_spacial.rb +60 -0
- data/lib/mongoid_spacial/extentions/hash/criteria_helpers.rb +22 -0
- data/lib/mongoid_spacial/extentions/symbol/inflections.rb +46 -0
- data/lib/mongoid_spacial/field_option.rb +38 -0
- data/lib/mongoid_spacial/finders.rb +5 -0
- data/lib/mongoid_spacial/spacial.rb +56 -0
- data/lib/mongoid_spacial/spacial/core_ext.rb +27 -0
- data/lib/mongoid_spacial/spacial/document.rb +29 -0
- data/lib/mongoid_spacial/spacial/formulas.rb +52 -0
- data/lib/mongoid_spacial/spacial/geo_near_results.rb +140 -0
- data/lib/mongoid_spacial/spacial/version.rb +5 -0
- data/mongoid_spacial.gemspec +27 -0
- data/spec/config/mongod.conf +3 -0
- data/spec/config/mongoid.yml +18 -0
- data/spec/functional/mongoid/contexts/mongo_spec.rb +125 -0
- data/spec/functional/mongoid/criterion/inclusion_spec.rb +356 -0
- data/spec/functional/mongoid/spacial/geo_near_results_spec.rb +73 -0
- data/spec/functional/mongoid/spacial_spec.rb +18 -0
- data/spec/models/account.rb +19 -0
- data/spec/models/acolyte.rb +9 -0
- data/spec/models/address.rb +62 -0
- data/spec/models/address_component.rb +5 -0
- data/spec/models/agent.rb +10 -0
- data/spec/models/alert.rb +5 -0
- data/spec/models/animal.rb +21 -0
- data/spec/models/answer.rb +4 -0
- data/spec/models/bar.rb +9 -0
- data/spec/models/birthday.rb +13 -0
- data/spec/models/book.rb +5 -0
- data/spec/models/business.rb +7 -0
- data/spec/models/callbacks.rb +57 -0
- data/spec/models/category.rb +13 -0
- data/spec/models/circus.rb +7 -0
- data/spec/models/comment.rb +13 -0
- data/spec/models/country_code.rb +6 -0
- data/spec/models/description.rb +11 -0
- data/spec/models/division.rb +5 -0
- data/spec/models/drug.rb +5 -0
- data/spec/models/employer.rb +5 -0
- data/spec/models/entry.rb +6 -0
- data/spec/models/event.rb +20 -0
- data/spec/models/favorite.rb +6 -0
- data/spec/models/fruits.rb +11 -0
- data/spec/models/game.rb +18 -0
- data/spec/models/ghost.rb +7 -0
- data/spec/models/house.rb +4 -0
- data/spec/models/inheritance.rb +90 -0
- data/spec/models/league.rb +5 -0
- data/spec/models/location.rb +5 -0
- data/spec/models/login.rb +6 -0
- data/spec/models/membership.rb +4 -0
- data/spec/models/mixed_drink.rb +4 -0
- data/spec/models/name.rb +13 -0
- data/spec/models/namespacing.rb +11 -0
- data/spec/models/observed.rb +41 -0
- data/spec/models/override.rb +16 -0
- data/spec/models/owner.rb +6 -0
- data/spec/models/page.rb +5 -0
- data/spec/models/page_question.rb +4 -0
- data/spec/models/paranoid_post.rb +18 -0
- data/spec/models/parents.rb +32 -0
- data/spec/models/patient.rb +15 -0
- data/spec/models/person.rb +146 -0
- data/spec/models/pet.rb +7 -0
- data/spec/models/pet_owner.rb +6 -0
- data/spec/models/phone.rb +7 -0
- data/spec/models/player.rb +23 -0
- data/spec/models/post.rb +26 -0
- data/spec/models/preference.rb +9 -0
- data/spec/models/question.rb +8 -0
- data/spec/models/quiz.rb +6 -0
- data/spec/models/rating.rb +8 -0
- data/spec/models/river.rb +20 -0
- data/spec/models/role.rb +5 -0
- data/spec/models/service.rb +6 -0
- data/spec/models/shelf.rb +5 -0
- data/spec/models/slave_address_numbers.rb +14 -0
- data/spec/models/survey.rb +5 -0
- data/spec/models/tag.rb +6 -0
- data/spec/models/tracking_id_validation_history.rb +25 -0
- data/spec/models/translation.rb +5 -0
- data/spec/models/tree.rb +9 -0
- data/spec/models/user.rb +9 -0
- data/spec/models/user_account.rb +10 -0
- data/spec/models/vet_visit.rb +5 -0
- data/spec/models/video.rb +9 -0
- data/spec/models/wiki_page.rb +6 -0
- data/spec/spec_helper.rb +43 -0
- data/spec/support/authentication.rb +29 -0
- data/spec/unit/mongoid/criterion/complex_spec.rb +15 -0
- data/spec/unit/mongoid/criterion/inclusion_spec.rb +0 -0
- data/spec/unit/mongoid/criterion/near_spacial_spec.rb +39 -0
- data/spec/unit/mongoid/criterion/within_spacial_spec.rb +52 -0
- data/spec/unit/mongoid/spacial/formulas_spec.rb +37 -0
- data/spec/unit/mongoid/spacial_spec.rb +6 -0
- metadata +369 -0
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
Gemfile.lock
|
2
|
+
# rcov generated
|
3
|
+
coverage
|
4
|
+
|
5
|
+
# rdoc generated
|
6
|
+
rdoc
|
7
|
+
|
8
|
+
# yard generated
|
9
|
+
doc
|
10
|
+
.yardoc
|
11
|
+
|
12
|
+
# bundler
|
13
|
+
.bundle
|
14
|
+
|
15
|
+
# jeweler generated
|
16
|
+
pkg
|
17
|
+
|
18
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
19
|
+
#
|
20
|
+
# * Create a file at ~/.gitignore
|
21
|
+
# * Include files you want ignored
|
22
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
23
|
+
#
|
24
|
+
# After doing this, these files will be ignored in all your git projects,
|
25
|
+
# saving you from having to 'pollute' every project you touch with them
|
26
|
+
#
|
27
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
28
|
+
#
|
29
|
+
# For MacOS:
|
30
|
+
#
|
31
|
+
.DS_Store
|
32
|
+
|
33
|
+
# For TextMate
|
34
|
+
#*.tmproj
|
35
|
+
#tmtags
|
36
|
+
|
37
|
+
# For emacs:
|
38
|
+
#*~
|
39
|
+
#\#*
|
40
|
+
#.\#*
|
41
|
+
|
42
|
+
# For vim:
|
43
|
+
*.swp
|
44
|
+
|
45
|
+
# For redcar:
|
46
|
+
#.redcar
|
47
|
+
|
48
|
+
# For rubinius:
|
49
|
+
#*.rbc
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Ryan Ong
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
Mongoid Spacial
|
2
|
+
============
|
3
|
+
|
4
|
+
A Mongoid Extention that simplifies and adds support for MongoDB Geo Spacial Calculations.
|
5
|
+
|
6
|
+
Quick Start
|
7
|
+
-----------
|
8
|
+
Add mongoid_spacial to your Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'mongoid_spacial'
|
12
|
+
```
|
13
|
+
|
14
|
+
Set up some slugs:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
class River
|
18
|
+
include Mongoid::Document
|
19
|
+
include Mongoid::Spacial::Document
|
20
|
+
|
21
|
+
field :name, type: String
|
22
|
+
field :length, type: Integer
|
23
|
+
field :average_discharge, type: Integer
|
24
|
+
field :source, type: Array, spacial: true
|
25
|
+
|
26
|
+
# set return_array to true if you do not want a hash returned all the time
|
27
|
+
field :mouth, type: Array, spacial: {lat: :latitude, lng: :longitude, return_array: true }
|
28
|
+
|
29
|
+
# simplified spacial indexing
|
30
|
+
# you can only index one point in mongodb version below 1.9
|
31
|
+
# if you want something besides the defaults {bit: 24, min: -180, max: 180} just set index to the options on the index
|
32
|
+
spacial_index :source
|
33
|
+
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Generate indexes on MongoDB:
|
38
|
+
|
39
|
+
```
|
40
|
+
rake db:mongoid:create_indexes
|
41
|
+
```
|
42
|
+
|
43
|
+
|
44
|
+
Before we manipulate the data mongoid_spacial handles is what we call points.
|
45
|
+
|
46
|
+
Points can be:
|
47
|
+
|
48
|
+
* an unordered hash with the lat long string keys defined when setting the field (only applies for setting the field)
|
49
|
+
* longitude latitude array in that order - [long,lat]
|
50
|
+
* an unordered hash with latitude key(:lat, :latitude) and a longitude key(:lon, :long, :lng, :longitude)
|
51
|
+
* an ordered hash with longitude as the first item and latitude as the second item
|
52
|
+
This hash does not have include the latitude and longitude keys
|
53
|
+
\*only works in ruby 1.9 and up because hashes below ruby 1.9 because they are not ordered
|
54
|
+
* anything with the method to_lng_lat that converts it to a [long,lat]
|
55
|
+
We store data in the DB as a [lng,lat] array then reformat when it is returned to you
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
hudson = River.create(
|
59
|
+
name: 'Hudson',
|
60
|
+
length: 315,
|
61
|
+
average_discharge: 21_400,
|
62
|
+
# when setting array LONGITUDE MUST BE FIRST LATITUDE MUST BE SECOND
|
63
|
+
# source: [-73.935833,44.106667],
|
64
|
+
# but we can use hash in any order,
|
65
|
+
# the default keys for latitude and longitude are :lat and :lng respectively
|
66
|
+
source: {:lat => 44.106667, :lng => -73.935833},
|
67
|
+
mouth: {:latitude => 40.703056, :longitude => -74.026667}
|
68
|
+
)
|
69
|
+
|
70
|
+
# now to access this spacial information we can now do this
|
71
|
+
hudson.source #=> {:lng => -73.935833, :lat => 44.106667}
|
72
|
+
hudson.mouth #=> [-74.026667, 40.703056] # notice how this returned as a lng,lat array because return_array was true
|
73
|
+
# notice how the order of lng and lat were switched. it will always come out like this when using spacial.
|
74
|
+
# Also adds a handy distance function
|
75
|
+
hudson.distance_from(:source, [-74,40], {:unit=>:mi})
|
76
|
+
|
77
|
+
```
|
78
|
+
Mongoid Geo has extended all built in spacial symbol extentions
|
79
|
+
|
80
|
+
* near
|
81
|
+
* River.where(:source.near => [-73.98, 40.77])
|
82
|
+
* River.where(:source.near => [[-73.98, 40.77],5]) # sets max distance of 5
|
83
|
+
* River.where(:source.near => {:point => [-73.98, 40.77], :max => 5}) # sets max distance of 5
|
84
|
+
* River.where(:source.near(:sphere) => [[-73.98, 40.77],5]) # sets max distance of 5 radians
|
85
|
+
* River.where(:source.near(:sphere) => {:point => [-73.98, 40.77], :max => 5, :unit => :km}) # sets max distance of 5 km
|
86
|
+
* River.where(:source.near(:sphere) => [-73.98, 40.77])
|
87
|
+
* within
|
88
|
+
* River.where(:source.within(:box) => [[-73.99756,40.73083], [-73.988135,40.741404]])
|
89
|
+
* River.where(:source.within(:box) => [ {:lat => 40.73083, :lng => -73.99756}, [-73.988135,40.741404]])
|
90
|
+
* River.where(:source.within(:polygon) => [ [ 10, 20 ], [ 10, 40 ], [ 30, 40 ], [ 30, 20 ] ]
|
91
|
+
* River.where(:source.within(:polygon) => { a : { x : 10, y : 20 }, b : { x : 15, y : 25 }, c : { x : 20, y : 20 } })
|
92
|
+
* River.where(:source.within(:center) => [[-73.98, 40.77],5]) # same format as near
|
93
|
+
* River.where(:source.within(:center_sphere) => [[-73.98, 40.77],5]) # same format as near(:sphere)
|
94
|
+
|
95
|
+
One of the most handy features we have added is geo_near finder
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
# accepts all criteria chains except without, only, asc, desc, order\_by
|
99
|
+
River.where(:name=>'hudson').geo_near({:lat => 40.73083, :lng => -73.99756})
|
100
|
+
|
101
|
+
# geo\_near accepts a few parameters besides a point
|
102
|
+
# :num = limit
|
103
|
+
# :query = where
|
104
|
+
# :unit - [:km, :m, :mi, :ft] - converts :max\_distance to appropriate values and automatically sets :distance\_multiplier. accepts
|
105
|
+
# :max\_distance - Integer
|
106
|
+
# :distance\_multiplier - Integer
|
107
|
+
# :spherical - true - To enable spherical calculations
|
108
|
+
River.geo_near([-73.99756,40.73083], :max_distance => 4, :unit => :mi, :spherical => true)
|
109
|
+
```
|
110
|
+
|
111
|
+
There are two types of pagination!
|
112
|
+
|
113
|
+
* MongoDB Pagination - Stores start of rows to page limit in memory
|
114
|
+
* Post Query Pagination - Stores all rows in memory
|
115
|
+
|
116
|
+
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.
|
117
|
+
|
118
|
+
Post-Result has some advantages that are listed below.
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
# MongoDB pagination
|
122
|
+
# overwrites #skip chain method
|
123
|
+
# :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
|
124
|
+
# :per\_page
|
125
|
+
# :paginator - Choose which paginator to use. [default :arrary]
|
126
|
+
# Prefered method to set is Mongoid::Spacial.paginator=:array
|
127
|
+
# Available Paginators [:kaminari, :will\_paginate, :array]
|
128
|
+
# The only thing this does really is configure default per\_page so it is only kind of useful
|
129
|
+
River.geo_near([-73.99756,40.73083], :page => 1)
|
130
|
+
```
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
# Post Query Pagination
|
134
|
+
# At carzen we use Post Query Pagination because we need to re-sort our rows after fetching. Pagination is not friendly with re-sorting.
|
135
|
+
# You can jump pages continously without querying the database again.
|
136
|
+
# listens to #limit/:num & #skip before geo\_near
|
137
|
+
# #page(page\_number, opts = {})
|
138
|
+
# opts:
|
139
|
+
# :per\_page
|
140
|
+
# :paginator
|
141
|
+
# #per(per\_page\_number, opts = {})
|
142
|
+
# opts:
|
143
|
+
# :page
|
144
|
+
# :paginator
|
145
|
+
#
|
146
|
+
# both return a GeoNearResults, which is really just a modified Array
|
147
|
+
# #per really just #page but just moves the options around
|
148
|
+
rivers = River.geo_near([-73.99756,40.73083]).sort_by!{|r| r.geo[:distance] * r.multiplier }
|
149
|
+
rivers = rivers.per(25).page(1)
|
150
|
+
rivers.reset! # resets the object to it is original state right after query.
|
151
|
+
```
|
152
|
+
|
153
|
+
Troubleshooting
|
154
|
+
-------------
|
155
|
+
|
156
|
+
**Mongo::OperationFailure: can't find special index: 2d**
|
157
|
+
|
158
|
+
Indexes need to be created. Execute command: <code>rake db:mongoid:create_indexes</code>
|
159
|
+
|
160
|
+
|
161
|
+
Thanks
|
162
|
+
-----------
|
163
|
+
* Thanks to Kristian Mandrup for creating the base of the gem and a few of the tests
|
164
|
+
* Thanks to CarZen LLC. for letting me release the code we are using
|
165
|
+
|
166
|
+
Contributing to mongoid_spacial
|
167
|
+
-----------
|
168
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
169
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
170
|
+
* Fork the project
|
171
|
+
* Start a feature/bugfix branch
|
172
|
+
* Commit and push until you are happy with your contribution
|
173
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
174
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
175
|
+
|
176
|
+
Copyright
|
177
|
+
-----------
|
178
|
+
Copyright (c) 2011 Ryan Ong. See LICENSE.txt for
|
179
|
+
further details.
|
180
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
require 'rspec/core'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
7
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
11
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
12
|
+
spec.rcov = true
|
13
|
+
end
|
14
|
+
|
15
|
+
task :default => :spec
|
16
|
+
|
17
|
+
require 'yard'
|
18
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
require 'active_support/core_ext/string/inflections'
|
3
|
+
require 'active_support/concern'
|
4
|
+
require 'mongoid_spacial/contexts/mongo'
|
5
|
+
require 'mongoid_spacial/criteria'
|
6
|
+
require 'mongoid_spacial/criterion'
|
7
|
+
require 'mongoid_spacial/extentions/hash/criteria_helpers'
|
8
|
+
require 'mongoid_spacial/extentions/symbol/inflections'
|
9
|
+
require 'mongoid_spacial/field_option'
|
10
|
+
require 'mongoid_spacial/finders'
|
11
|
+
require 'mongoid_spacial/spacial'
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Contexts #:nodoc:
|
4
|
+
class Mongo #:nodoc:
|
5
|
+
|
6
|
+
# Fetches rows from the data base sorted by distance.
|
7
|
+
# In MongoDB versions 1.7 and above it returns a distance.
|
8
|
+
# Uses all criteria chains except without, only, asc, desc, order_by
|
9
|
+
#
|
10
|
+
# @example Minimal Query
|
11
|
+
#
|
12
|
+
# Address.geo_near([70,40])
|
13
|
+
#
|
14
|
+
# @example Chained Query
|
15
|
+
#
|
16
|
+
# Address.where(:state => 'ny').geo_near([70,40])
|
17
|
+
#
|
18
|
+
# @example Calc Distances Query
|
19
|
+
#
|
20
|
+
# Address.geo_near([70,40], :max_distance => 5, :unit => 5)
|
21
|
+
#
|
22
|
+
# @param [ Array, Hash, #to_lng_lat ] center The center of where to calculate distance from
|
23
|
+
# @param [ Hash ] opts the options to query with
|
24
|
+
# @options opts [Integer] :num The number of rows to fetch
|
25
|
+
# @options opts [Hash] :query The query to filter the rows by, accepts
|
26
|
+
# @options opts [Numeric] :distance_multiplier this is multiplied against the calculated distance
|
27
|
+
# @options opts [Numeric] :max_distance The max distance of a row that should be returned in :unit(s)
|
28
|
+
# @options opts [Numeric, :km, :k, :mi, :ft] :unit automatically sets :distance_multiplier and converts :max_distance
|
29
|
+
# @options opts [true,false] :spherical Will determine the distance either by spherical calculation or flat calculation
|
30
|
+
# @options opts [TrueClass,Array<Symbol>] :calculate Which extra fields to calculate distance for in ruby, if set to TrueClass it will calculate all spacial fields
|
31
|
+
#
|
32
|
+
# @return [ Array ] Sorted Rows
|
33
|
+
def geo_near(center, opts = {})
|
34
|
+
opts = self.options.merge(opts)
|
35
|
+
# convert point
|
36
|
+
center = center.to_lng_lat if center.respond_to?(:to_lng_lat)
|
37
|
+
|
38
|
+
# set default opts
|
39
|
+
opts[:skip] ||= 0
|
40
|
+
|
41
|
+
if unit = Mongoid::Spacial.earth_radius[opts[:unit]]
|
42
|
+
opts[:unit] = (opts[:spherical]) ? unit : unit * Mongoid::Spacial::RAD_PER_DEG
|
43
|
+
end
|
44
|
+
|
45
|
+
if unit = Mongoid::Spacial.earth_radius[opts[:distance_multiplier]]
|
46
|
+
opts[:distance_multiplier] = (opts[:spherical]) ? unit : unit * Mongoid::Spacial::RAD_PER_DEG
|
47
|
+
end
|
48
|
+
|
49
|
+
opts[:distance_multiplier] = opts[:unit] if opts[:unit].kind_of?(Numeric)
|
50
|
+
|
51
|
+
# setup paging.
|
52
|
+
if opts.has_key?(:page)
|
53
|
+
opts[:page] ||= 1
|
54
|
+
opts[:paginator] ||= Mongoid::Spacial.paginator()
|
55
|
+
|
56
|
+
if opts[:per_page].blank?
|
57
|
+
opts[:per_page] = case opts[:paginator]
|
58
|
+
when :will_paginate
|
59
|
+
@document.per_page
|
60
|
+
when :kaminari
|
61
|
+
Kaminari.config.default_per_page
|
62
|
+
else
|
63
|
+
Mongoid::Spacial.default_per_page
|
64
|
+
end
|
65
|
+
opts[:per_page] = opts[:per_page].to_i
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
opts[:query] = create_geo_near_query(center,opts)
|
70
|
+
results = klass.db.command(opts[:query])
|
71
|
+
Mongoid::Spacial::GeoNearResults.new(klass,results,opts)
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def create_geo_near_query(center,opts)
|
77
|
+
# minimum query
|
78
|
+
query = {
|
79
|
+
:geoNear => klass.collection_name,
|
80
|
+
:near => center,
|
81
|
+
}
|
82
|
+
|
83
|
+
# create limit and use skip
|
84
|
+
if opts[:num]
|
85
|
+
query['num'] = opts[:skip].to_i + opts[:num].to_i
|
86
|
+
elsif opts[:limit]
|
87
|
+
query['num'] = opts[:skip].to_i + opts[:limit].to_i
|
88
|
+
elsif opts[:page]
|
89
|
+
query['num'] = opts[:skip].to_i + (opts[:page].to_i * opts[:per_page].to_i)
|
90
|
+
end
|
91
|
+
|
92
|
+
# allow the use of complex werieis
|
93
|
+
if opts[:query]
|
94
|
+
query['query'] = self.criteria.where(opts[:query]).selector
|
95
|
+
elsif self.selector != {}
|
96
|
+
query['query'] = self.selector
|
97
|
+
end
|
98
|
+
|
99
|
+
if opts[:max_distance]
|
100
|
+
query['maxDistance'] = opts[:max_distance].to_f
|
101
|
+
query['maxDistance'] = query['maxDistance']/opts[:unit].to_f if opts[:unit]
|
102
|
+
end
|
103
|
+
|
104
|
+
if klass.db.connection.server_version >= '1.7'
|
105
|
+
query['spherical'] = true if opts[:spherical]
|
106
|
+
|
107
|
+
# mongodb < 1.7 returns degrees but with earth flat. in Mongodb 1.7 you can set sphere and let mongodb calculate the distance in Miles or KM
|
108
|
+
# for mongodb < 1.7 we need to run Haversine first before calculating degrees to Km or Miles. See below.
|
109
|
+
query['distanceMultiplier'] = opts[:distance_multiplier].to_f if opts[:distance_multiplier]
|
110
|
+
end
|
111
|
+
query
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Criterion #:nodoc:
|
4
|
+
# Complex criterion are used when performing operations on symbols to get
|
5
|
+
# get a shorthand syntax for where clauses.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
#
|
9
|
+
# <tt>{ :field => { "$lt" => "value" } }</tt>
|
10
|
+
# becomes:
|
11
|
+
# <tt> { :field.lt => "value }</tt>
|
12
|
+
class Complex
|
13
|
+
|
14
|
+
def to_mongo_query v
|
15
|
+
{"$#{operator}" => v}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|