mongoid_spacial 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +28 -0
- data/LICENSE.txt +20 -0
- data/README.md +126 -0
- data/Rakefile +42 -0
- data/VERSION +1 -0
- data/lib/mongoid_spacial.rb +11 -0
- data/lib/mongoid_spacial/contexts/mongo.rb +126 -0
- data/lib/mongoid_spacial/criteria.rb +5 -0
- data/lib/mongoid_spacial/criterion.rb +8 -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 +43 -0
- data/lib/mongoid_spacial/criterion/within_spacial.rb +56 -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 +36 -0
- data/lib/mongoid_spacial/finders.rb +5 -0
- data/lib/mongoid_spacial/spacial.rb +37 -0
- data/lib/mongoid_spacial/spacial/core_ext.rb +27 -0
- data/lib/mongoid_spacial/spacial/document.rb +31 -0
- data/lib/mongoid_spacial/spacial/formulas.rb +47 -0
- data/mongoid_spacial.gemspec +172 -0
- data/spec/config/mongod.conf +3 -0
- data/spec/config/mongoid.yml +18 -0
- data/spec/functional/mongoid/contexts/mongo_spec.rb +101 -0
- data/spec/functional/mongoid/criterion/inclusion_spec.rb +356 -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 +41 -0
- data/spec/support/authentication.rb +29 -0
- data/spec/unit/mongoid_spacial/criterion/complex_spec.rb +15 -0
- data/spec/unit/mongoid_spacial/criterion/inclusion_spec.rb +0 -0
- data/spec/unit/mongoid_spacial/criterion/near_spacial_spec.rb +39 -0
- data/spec/unit/mongoid_spacial/criterion/within_spacial_spec.rb +52 -0
- metadata +280 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
gem 'mongoid'
|
9
|
+
gem "bson", '>= 1.3', :platforms => [:jruby] # for non jruby apps, require bson_ext in your Gemfile to boost performance
|
10
|
+
gem "bson_ext", '>= 1.3', :platforms => [:mri]
|
11
|
+
gem 'activesupport'
|
12
|
+
|
13
|
+
group :development do
|
14
|
+
gem "rspec", "~> 2.3.0"
|
15
|
+
gem "yard", "~> 0.6.0"
|
16
|
+
gem "bundler", "~> 1.0.0"
|
17
|
+
gem "jeweler", "~> 1.6.2"
|
18
|
+
gem "rcov", ">= 0"
|
19
|
+
end
|
20
|
+
|
21
|
+
group :test, :development do
|
22
|
+
gem 'linecache19'
|
23
|
+
gem 'ruby-debug19'
|
24
|
+
end
|
25
|
+
|
26
|
+
group :test do
|
27
|
+
gem 'mocha'
|
28
|
+
end
|
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,126 @@
|
|
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
|
+
# if you want something besides the defaults {bit: 24, min: -180, max: 180} just set index to the options on the index
|
26
|
+
# field :source, type: Array, spacial: true
|
27
|
+
# try not set index for this field manually as we record what spacial fields are index for some handy fields later
|
28
|
+
|
29
|
+
# set return_array to true if you do not want a hash returned all the time
|
30
|
+
field :mouth, type: Array, spacial: {lat: 'latitude', lng: 'longitude', return_array: true }
|
31
|
+
|
32
|
+
# simplified spacial indexing
|
33
|
+
# you can only index one
|
34
|
+
spacial_index :source
|
35
|
+
# alternative
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Before we manipulate the data mongoid_spacial handles is what we call points.
|
40
|
+
Points can be:
|
41
|
+
* an unordered hash with the lat long string keys defined when setting the field (only applies for setting the field)
|
42
|
+
* longitude latitude array in that order - [long,lat]
|
43
|
+
* an unordered hash with latitude key(:lat, :latitude) and a longitude key(:lon, :long, :lng, :longitude)
|
44
|
+
* an ordered hash with longitude as the first item and latitude as the second item
|
45
|
+
This hash does not have include the latitude and longitude keys
|
46
|
+
\*only works in ruby 1.9 and up because hashes below ruby 1.9 because they are not ordered
|
47
|
+
* anything with the method to_lng_lat that converts it to a [long,lat]
|
48
|
+
We store data in the DB as a [lng,lat] array then reformat when it is returned to you
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
hudson = River.create(
|
52
|
+
name: 'Hudson',
|
53
|
+
length: 315,
|
54
|
+
average_discharge: 21_400,
|
55
|
+
# when setting array LONGITUDE MUST BE FIRST LATITUDE MUST BE SECOND
|
56
|
+
# source: [-73.935833,44.106667],
|
57
|
+
# but we can use hash in any order,
|
58
|
+
# the default keys for latitude and longitude are 'lat' and 'lng' respectively
|
59
|
+
source: {'lat' => 44.106667, 'lng' => -73.935833},
|
60
|
+
# remember keys must be strings
|
61
|
+
mouth: {'latitude' => 40.703056, 'longitude' => -74.026667}
|
62
|
+
)
|
63
|
+
|
64
|
+
# now to access this spacial information we can now do this
|
65
|
+
hudson.source #=> {'lng' => -73.935833, 'lat' => 44.106667}
|
66
|
+
hudson.mouth #=> [-74.026667, 40.703056] # notice how this returned as a lng,lat array because return_array was true
|
67
|
+
# notice how the order of lng and lat were switched. it will always come out like this when using spacial.
|
68
|
+
```
|
69
|
+
Mongoid Geo has extended all built in spacial symbol extentions
|
70
|
+
* near
|
71
|
+
River.where(:source.near => [-73.98, 40.77])
|
72
|
+
River.where(:source.near => [[-73.98, 40.77],5]) # sets max distance of 5
|
73
|
+
River.where(:source.near => {:point => [-73.98, 40.77], :max => 5}) # sets max distance of 5
|
74
|
+
River.where(:source.near(:sphere) => [[-73.98, 40.77],5]) # sets max distance of 5 radians
|
75
|
+
River.where(:source.near(:sphere) => {:point => [-73.98, 40.77], :max => 5, :unit => :km}) # sets max distance of 5 km
|
76
|
+
River.where(:source.near(:sphere) => [-73.98, 40.77])
|
77
|
+
* within
|
78
|
+
River.where(:source.within(:box) => [[-73.99756,40.73083], [-73.988135,40.741404]])
|
79
|
+
River.where(:source.within(:box) => [ {:lat => 40.73083, :lng => -73.99756}, [-73.988135,40.741404]])
|
80
|
+
River.where(:source.within(:polygon) => [ [ 10, 20 ], [ 10, 40 ], [ 30, 40 ], [ 30, 20 ] ]
|
81
|
+
River.where(:source.within(:polygon) => { a : { x : 10, y : 20 }, b : { x : 15, y : 25 }, c : { x : 20, y : 20 } })
|
82
|
+
River.where(:source.within(:center) => [[-73.98, 40.77],5]) # same format as near
|
83
|
+
River.where(:source.within(:center_sphere) => [[-73.98, 40.77],5]) # same format as near(:sphere)
|
84
|
+
|
85
|
+
One of the most handy features we have added is geo_near finder
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
# accepts all criteria chains except without, only, asc, desc, order\_by
|
89
|
+
# pagination does not work yet but will be added in the next version
|
90
|
+
River.where(:name=>'hudson').geo_near({:lat => 40.73083, :lng => -73.99756})
|
91
|
+
|
92
|
+
# geo\_near accepts a few parameters besides a point
|
93
|
+
# :num = limit
|
94
|
+
# :query = where
|
95
|
+
# :unit - [:km, :m, :mi, :ft] - converts :max\_distance to appropriate values and automatically sets :distance\_multiplier. accepts
|
96
|
+
# :max\_distance - Integer
|
97
|
+
# :distance\_multiplier - Integer
|
98
|
+
# :spherical - true - To enable spherical calculations
|
99
|
+
River.geo_near([-73.99756,40.73083], :max_distance => 4, :unit => :mi, :spherical)
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
ToDo
|
104
|
+
-----------
|
105
|
+
1. Add pagination
|
106
|
+
|
107
|
+
Thanks
|
108
|
+
-----------
|
109
|
+
* Thanks to Kristian Mandrup for creating the base of the gem and a few of the tests
|
110
|
+
* Thanks to CarZen LLC. for letting me release the code we are using
|
111
|
+
|
112
|
+
Contributing to mongoid_ranges
|
113
|
+
-----------
|
114
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
115
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
116
|
+
* Fork the project
|
117
|
+
* Start a feature/bugfix branch
|
118
|
+
* Commit and push until you are happy with your contribution
|
119
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
120
|
+
* 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.
|
121
|
+
|
122
|
+
Copyright
|
123
|
+
-----------
|
124
|
+
Copyright (c) 2011 Ryan Ong. See LICENSE.txt for
|
125
|
+
further details.
|
126
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "mongoid_spacial"
|
18
|
+
gem.homepage = "http://github.com/ryanong/mongoid_spacial"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A Mongoid Extention that simplifies and adds support for MongoDB Geo Spacial Calculations.}
|
21
|
+
gem.description = %Q{A Mongoid Extention that simplifies and adds support for MongoDB Geo Spacial Calculations.}
|
22
|
+
gem.email = "ryanong@gmail.com"
|
23
|
+
gem.authors = ["Ryan Ong"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'yard'
|
42
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -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,126 @@
|
|
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
|
+
center = center.to_lng_lat if center.respond_to?(:to_lng_lat)
|
35
|
+
|
36
|
+
if distance_multiplier = Mongoid::Spacial.earth_radius[opts.delete(:unit)]
|
37
|
+
opts[:distance_multiplier] = distance_multiplier
|
38
|
+
end
|
39
|
+
|
40
|
+
query = create_geo_near_query(center,opts)
|
41
|
+
results = klass.db.command(query)
|
42
|
+
if results['results'].kind_of?(Array) && results['results'].size > 0
|
43
|
+
rows = results['results'].collect do |result|
|
44
|
+
res = Mongoid::Factory.from_db(klass, result.delete('obj'))
|
45
|
+
res.geo = {}
|
46
|
+
# camel case is awkward in ruby when using variables...
|
47
|
+
if result['dis']
|
48
|
+
res.geo[:distance] = result.delete('dis').to_f
|
49
|
+
end
|
50
|
+
result.each do |key,value|
|
51
|
+
res.geo[key.snakecase.to_sym] = value
|
52
|
+
end
|
53
|
+
# dist_options[:formula] = opts[:formula] if opts[:formula]
|
54
|
+
opts[:calculate] = klass.spacial_fields_indexed if klass.spacial_fields_indexed.kind_of?(Array) && opts[:calculate] == true
|
55
|
+
if opts[:calculate]
|
56
|
+
opts[:calculate] = [opts[:calculate]] unless opts[:calculate].kind_of? Array
|
57
|
+
opts[:calculate] = opts[:calculate].map(&:to_sym) & geo_fields
|
58
|
+
if klass.spacial_fields_indexed.kind_of?(Array) && klass.spacial_fields_indexed.size == 1
|
59
|
+
primary = klass.spacial_fields_indexed.first
|
60
|
+
end
|
61
|
+
opts[:calculate].each do |key|
|
62
|
+
key = (key.to_s+'_distance').to_s
|
63
|
+
res.geo[key] = res.distance_from(key,center, opts[:distance_multiplier])
|
64
|
+
res.geo[:distance] = res.geo[key] if primary && key == primary
|
65
|
+
end
|
66
|
+
end
|
67
|
+
res
|
68
|
+
end
|
69
|
+
else
|
70
|
+
rows = []
|
71
|
+
end
|
72
|
+
# if opts.has_key?(:page) || opts[:paginator]
|
73
|
+
# opts[:paginator] ||= Mongoid::Spacial::Config.paginator
|
74
|
+
# if opts[:paginator] == :kaminari
|
75
|
+
|
76
|
+
# elsif opts[:paginator] == :will_paginate
|
77
|
+
# end
|
78
|
+
# end
|
79
|
+
|
80
|
+
if self.options[:skip] && rows.size > self.options[:skip]
|
81
|
+
rows[self.options[:skip]..rows.size-1]
|
82
|
+
else
|
83
|
+
rows
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def create_geo_near_query(center,opts)
|
90
|
+
# minimum query
|
91
|
+
query = {
|
92
|
+
:geoNear => klass.to_s.tableize,
|
93
|
+
:near => center,
|
94
|
+
}
|
95
|
+
|
96
|
+
# create limit and use skip
|
97
|
+
if opts[:num]
|
98
|
+
query['num'] = (self.options[:skip] || 0) + opts[:num].to_i
|
99
|
+
elsif self.options[:limit]
|
100
|
+
query['num'] = (self.options[:skip] || 0) + self.options[:limit]
|
101
|
+
end
|
102
|
+
|
103
|
+
# allow the use of complex werieis
|
104
|
+
if opts[:query]
|
105
|
+
query['query'] = self.criteria.where(opts[:query]).selector
|
106
|
+
elsif self.selector != {}
|
107
|
+
query['query'] = self.selector
|
108
|
+
end
|
109
|
+
|
110
|
+
if opts[:max_distance]
|
111
|
+
query['maxDistance'] = opts[:max_distance]
|
112
|
+
query['maxDistance'] = query['maxDistance']/opts[:distance_multiplier] if opts[:distance_multiplier]
|
113
|
+
end
|
114
|
+
|
115
|
+
if klass.db.connection.server_version >= '1.7'
|
116
|
+
query['spherical'] = true if opts[:spherical]
|
117
|
+
|
118
|
+
# 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
|
119
|
+
# for mongodb < 1.7 we need to run Haversine first before calculating degrees to Km or Miles. See below.
|
120
|
+
query['distanceMultiplier'] = opts[:distance_multiplier] if opts[:distance_multiplier]
|
121
|
+
end
|
122
|
+
query
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
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
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Criterion #:nodoc:
|
4
|
+
module Inclusion
|
5
|
+
def near(attributes = {})
|
6
|
+
update_selector(attributes, "$near")
|
7
|
+
end
|
8
|
+
|
9
|
+
def near_sphere(attributes = {})
|
10
|
+
update_selector(attributes, "$near")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Criterion #:nodoc:
|
4
|
+
|
5
|
+
# NearSpecial criterion is used when performing #near with symbols to get
|
6
|
+
# get a shorthand syntax for where clauses.
|
7
|
+
#
|
8
|
+
# @example Conversion of a simple to complex criterion.
|
9
|
+
# { :field => { "$nearSphere" => => [20,30]}, '$maxDistance' => 5 }
|
10
|
+
# becomes:
|
11
|
+
# { :field.near(:sphere) => {:point => [20,30], :max => 5, :unit => :km} }
|
12
|
+
class NearSpacial < Complex
|
13
|
+
|
14
|
+
# Convert input to query for near or nearSphere
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
# near = NearSpacial.new(:key => :field, :operator => "near")
|
18
|
+
# near.to_mongo_query({:point => [:50,50], :max_distance => 5, :unit => :km}) => { '$near : [50,50]' , '$maxDistance' : 5 }
|
19
|
+
#
|
20
|
+
# @param [Hash,Array] v input to conver to query
|
21
|
+
def to_mongo_query(v)
|
22
|
+
if v.kind_of? Hash
|
23
|
+
v[:point] = v[:point].to_lng_lat if v[:point].respond_to?(:to_lng_lat)
|
24
|
+
query = {"$#{operator}" => v[:point] }
|
25
|
+
if v[:max]
|
26
|
+
unit = Mongoid::Spacial.earth_radius[v[:unit]]
|
27
|
+
query['$maxDistance'] = (unit) ? v[:max]/unit : v[:max]
|
28
|
+
end
|
29
|
+
query
|
30
|
+
elsif v.kind_of? Array
|
31
|
+
if v.first.kind_of? Numeric
|
32
|
+
{"$#{operator}" => v }
|
33
|
+
else
|
34
|
+
v[0] = v[0].to_lng_lat if v[0].respond_to?(:to_lng_lat)
|
35
|
+
{"$#{operator}" => v[0], '$maxDistance' => v[1] }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|