mongoid-geospatial 3.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +49 -0
- data/.travis.yml +19 -0
- data/Gemfile +30 -0
- data/Guardfile +16 -0
- data/MIT-LICENSE +20 -0
- data/README.md +519 -0
- data/Rakefile +17 -0
- data/lib/mongoid/geospatial.rb +106 -0
- data/lib/mongoid/geospatial/ext/rgeo_spherical_point_impl.rb +18 -0
- data/lib/mongoid/geospatial/fields/box.rb +6 -0
- data/lib/mongoid/geospatial/fields/circle.rb +18 -0
- data/lib/mongoid/geospatial/fields/geometry_field.rb +41 -0
- data/lib/mongoid/geospatial/fields/line.rb +6 -0
- data/lib/mongoid/geospatial/fields/point.rb +143 -0
- data/lib/mongoid/geospatial/fields/polygon.rb +6 -0
- data/lib/mongoid/geospatial/helpers/delegate.rb +30 -0
- data/lib/mongoid/geospatial/helpers/spatial.rb +19 -0
- data/lib/mongoid/geospatial/helpers/sphere.rb +18 -0
- data/lib/mongoid/geospatial/version.rb +6 -0
- data/lib/mongoid/geospatial/wrappers/georuby.rb +33 -0
- data/lib/mongoid/geospatial/wrappers/rgeo.rb +43 -0
- data/mongoid-geospatial.gemspec +22 -0
- data/spec/models/address.rb +69 -0
- data/spec/models/alarm.rb +12 -0
- data/spec/models/bar.rb +13 -0
- data/spec/models/bus.rb +12 -0
- data/spec/models/event.rb +17 -0
- data/spec/models/farm.rb +13 -0
- data/spec/models/person.rb +97 -0
- data/spec/models/phone.rb +8 -0
- data/spec/models/place.rb +13 -0
- data/spec/models/river.rb +22 -0
- data/spec/mongoid/geospatial/fields/box_spec.rb +10 -0
- data/spec/mongoid/geospatial/fields/circle_spec.rb +10 -0
- data/spec/mongoid/geospatial/fields/line_spec.rb +40 -0
- data/spec/mongoid/geospatial/fields/point_spec.rb +254 -0
- data/spec/mongoid/geospatial/fields/polygon_spec.rb +84 -0
- data/spec/mongoid/geospatial/geospatial_spec.rb +143 -0
- data/spec/mongoid/geospatial/helpers/core_spec.rb +27 -0
- data/spec/mongoid/geospatial/helpers/delegate_spec.rb +54 -0
- data/spec/mongoid/geospatial/helpers/spatial_spec.rb +36 -0
- data/spec/mongoid/geospatial/helpers/sphere_spec.rb +26 -0
- data/spec/mongoid/geospatial/wrappers/georuby_spec.rb +66 -0
- data/spec/mongoid/geospatial/wrappers/rgeo_spec.rb +121 -0
- data/spec/spec_helper.rb +64 -0
- data/spec/support/authentication.rb +29 -0
- data/spec/support/mongod.conf +3 -0
- data/spec/support/mongoid.yml +19 -0
- metadata +164 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 93a8802d5a645b356af69d76585004ed4e080a16
|
4
|
+
data.tar.gz: 0ec2e2b4b87d28bf04af6d2ed0ef7d1ff49aa412
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5142eaf9e0aed0a78e2eb581b7d23b14c50b7050baf030f2e23d66aa9b50806a60ebd87b692f1cb9ba2790a5b8b6aa128480980e2efa3c43fce0d38709493032
|
7
|
+
data.tar.gz: 1e678afee79c30fd9965c7773d7cb2673f2c0a44d3a23a66cac69082b12cb86e914048a54f438f02322d32d22c3f7273f0893fa4a0d414bad59e4a8f4977d58b
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
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/.travis.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
language: ruby
|
2
|
+
bundler_args: --without development
|
3
|
+
rvm:
|
4
|
+
- 1.9.3
|
5
|
+
- 2.0.0
|
6
|
+
- ruby-head
|
7
|
+
- rbx-19mode
|
8
|
+
- jruby
|
9
|
+
- jruby-head
|
10
|
+
jdk:
|
11
|
+
- oraclejdk7
|
12
|
+
- openjdk7
|
13
|
+
env: JRUBY_OPTS="--server -Xcompile.invokedynamic=false -J-XX:+TieredCompilation -J-XX:TieredStopAtLevel=1 -J-noverify -J-Xms512m -J-Xmx1024m"
|
14
|
+
matrix:
|
15
|
+
allow_failures:
|
16
|
+
- rvm: ruby-head
|
17
|
+
- rvm: jruby-head
|
18
|
+
services:
|
19
|
+
- mongodb
|
data/Gemfile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
gemspec # Specify gem's dependencies in mongoid_geospatial.gemspec
|
3
|
+
|
4
|
+
gem 'rake'
|
5
|
+
gem 'mongoid' #, github: 'mongoid/mongoid'
|
6
|
+
|
7
|
+
group :development do
|
8
|
+
gem 'pry'
|
9
|
+
gem 'yard'
|
10
|
+
end
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem 'dbf'
|
14
|
+
gem 'rspec'
|
15
|
+
gem 'nokogiri'
|
16
|
+
gem 'rgeo'
|
17
|
+
gem 'georuby'
|
18
|
+
|
19
|
+
if ENV['CI']
|
20
|
+
gem 'coveralls', require: false
|
21
|
+
gem 'yard'
|
22
|
+
else
|
23
|
+
gem 'rubocop'
|
24
|
+
gem 'fuubar'
|
25
|
+
gem 'guard'
|
26
|
+
gem 'guard-rubocop'
|
27
|
+
gem 'guard-rspec'
|
28
|
+
# gem 'rb-fsevent'
|
29
|
+
end
|
30
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#
|
2
|
+
# Mongoid Geospatial Guardfile
|
3
|
+
#
|
4
|
+
ignore(/\/.#.+/)
|
5
|
+
|
6
|
+
# notification :off
|
7
|
+
|
8
|
+
guard :rubocop, all_on_start: false, keep_failed: false, notification: false, cli: ['--format', 'emacs'] do
|
9
|
+
watch(/^lib\/(.+)\.rb$/)
|
10
|
+
end
|
11
|
+
|
12
|
+
guard :rspec, cmd: 'bundle exec rspec', notification: true do
|
13
|
+
watch(/^spec\/.+_spec\.rb$/)
|
14
|
+
watch(/^lib\/(.+)\.rb$/) { |m| "spec/#{m[1]}_spec.rb" }
|
15
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
16
|
+
end
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009-2013 Mongoid Geospatial Authors
|
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,519 @@
|
|
1
|
+
Mongoid Geospatial
|
2
|
+
==================
|
3
|
+
|
4
|
+
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/nofxx/mongoid-geospatial?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
5
|
+
|
6
|
+
A Mongoid Extension that simplifies the use of MongoDB spatial features.
|
7
|
+
|
8
|
+
** On beta again **
|
9
|
+
|
10
|
+
Removing some trash, improving and adding support for RGeo and GeoRuby.
|
11
|
+
Version 3+ is going to be beta testing, when it's ready I'll release v4,
|
12
|
+
So the major version stays the same as mongoid.
|
13
|
+
|
14
|
+
[![Gem Version](https://badge.fury.io/rb/mongoid-geospatial.png)](http://badge.fury.io/rb/mongoid-geospatial)
|
15
|
+
[![Code Climate](https://codeclimate.com/github/nofxx/mongoid-geospatial.png)](https://codeclimate.com/github/nofxx/mongoid-geospatial)
|
16
|
+
[![Coverage Status](https://coveralls.io/repos/nofxx/mongoid-geospatial/badge.png)](https://coveralls.io/r/nofxx/mongoid-geospatial)
|
17
|
+
[![Build Status](https://travis-ci.org/nofxx/mongoid-geospatial.png?branch=master)](https://travis-ci.org/nofxx/mongoid-geospatial)
|
18
|
+
|
19
|
+
|
20
|
+
Quick Start
|
21
|
+
-----------
|
22
|
+
|
23
|
+
This gem focus on (making helpers for) MongoDB's spatial features.
|
24
|
+
But you may also use an external Geometric/Spatial gem alongside.
|
25
|
+
|
26
|
+
# Gemfile
|
27
|
+
gem 'mongoid-geospatial'
|
28
|
+
|
29
|
+
|
30
|
+
A place to illustrate Point, Line and Polygon
|
31
|
+
|
32
|
+
class Place
|
33
|
+
include Mongoid::Document
|
34
|
+
|
35
|
+
# Include the module
|
36
|
+
include Mongoid::Geospatial
|
37
|
+
|
38
|
+
# Just like mongoid,
|
39
|
+
field :name, type: String
|
40
|
+
|
41
|
+
# define your field, but choose a geometry type:
|
42
|
+
field :location, type: Point
|
43
|
+
field :route, type: Linestring
|
44
|
+
field :area, type: Polygon
|
45
|
+
|
46
|
+
# If your are going to query on your points, don't forget to index:
|
47
|
+
spatial_index :location
|
48
|
+
|
49
|
+
# Or you can index points with 'spatial: true' option too:
|
50
|
+
field :location, type: Point, spatial: true
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
For geo points, an extra macro `geo_field` is available
|
55
|
+
|
56
|
+
|
57
|
+
geo_field :location
|
58
|
+
# Will generate:
|
59
|
+
field :location, type: Point, spatial: true
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
Generate indexes on MongoDB:
|
64
|
+
|
65
|
+
|
66
|
+
rake db:mongoid:create_indexes
|
67
|
+
|
68
|
+
|
69
|
+
Index programatically via https://github.com/kristianmandrup/mongoid_indexing gem
|
70
|
+
|
71
|
+
|
72
|
+
Mongoid::Indexing.create_indexes
|
73
|
+
|
74
|
+
|
75
|
+
This is fx useful when running specs or when working with spatials on a gem or engine.
|
76
|
+
|
77
|
+
|
78
|
+
Points
|
79
|
+
------
|
80
|
+
|
81
|
+
Currently, MongoDB supports query operations on 2D points only, so that's
|
82
|
+
what this lib does. All geometries apart from points are just arrays
|
83
|
+
in the database. Here's is how you can input a point as:
|
84
|
+
|
85
|
+
* longitude latitude array in that order - [long,lat] ([x, y])
|
86
|
+
* an unordered hash with latitude key(:lat, :latitude) and a longitude key(:lon, :long, :lng, :longitude)
|
87
|
+
* an ordered hash with longitude as the first item and latitude as the second item
|
88
|
+
This hash does not have include the latitude and longitude keys
|
89
|
+
\*only works in ruby 1.9 and up because hashes below ruby 1.9 because they are not ordered
|
90
|
+
* anything with the a method #to_xy or #to_lng_lat that converts itself to [long, lat] array
|
91
|
+
|
92
|
+
We store data in the DB as a [x, y] array then reformat when it is returned to you
|
93
|
+
|
94
|
+
|
95
|
+
cafe = Place.create(
|
96
|
+
name: 'Café Rider',
|
97
|
+
location: {:lat => 44.106667, :lng => -73.935833},
|
98
|
+
# or
|
99
|
+
location: {latitude: 40.703056, longitude: -74.026667}
|
100
|
+
#...
|
101
|
+
|
102
|
+
Now to access this spatial information we can do this
|
103
|
+
|
104
|
+
cafe.location # => [-74.026667, 40.703056]
|
105
|
+
|
106
|
+
If you need a hash
|
107
|
+
|
108
|
+
cafe.location.to_hsh # => { x: -74.026667, y: 40.703056 }
|
109
|
+
|
110
|
+
If you are using GeoRuby or RGeo
|
111
|
+
|
112
|
+
cafe.location.to_geo # => GeoRuby::Point
|
113
|
+
|
114
|
+
cafe.location.to_rgeo # => RGeo::Point
|
115
|
+
|
116
|
+
|
117
|
+
Conventions:
|
118
|
+
|
119
|
+
This lib uses #x and #y everywhere.
|
120
|
+
It's shorter than lat or lng or another variation that also confuses.
|
121
|
+
A point is a 2D mathematical notation, longitude/latitude is when you use that notation to map an sphere.
|
122
|
+
|
123
|
+
In other words, all longitudes are 'xs' where not all 'xs' are longitudes.
|
124
|
+
In the eyes of a moralist it's not even a valid position point, it does not have #z or #m.
|
125
|
+
|
126
|
+
Distance and other geometrical calculations are delegated to the external
|
127
|
+
library you choosed. More info about using RGeo or GeoRuby below.
|
128
|
+
Some built in helpers for mongoid queries:
|
129
|
+
|
130
|
+
# Returns middle point + radius
|
131
|
+
# Useful to search #within_circle
|
132
|
+
cafe.location.radius(5) # [[-74.., 40..], 5]
|
133
|
+
cafe.location.radius_sphere(5) # [[-74.., 40..], 0.00048..]
|
134
|
+
|
135
|
+
# Returns hash if needed
|
136
|
+
cafe.location.to_hsh # {:x => -74.., :y => 40..}
|
137
|
+
cafe.location.to_hsh(:lon, :lat) # {:lon => -74.., :lat => 40..}
|
138
|
+
|
139
|
+
|
140
|
+
And for polygons and lines:
|
141
|
+
|
142
|
+
house.area.bbox # Returns polygon bounding_box (envelope)
|
143
|
+
house.area.center # Returns calculate middle point
|
144
|
+
|
145
|
+
|
146
|
+
Query
|
147
|
+
-----
|
148
|
+
|
149
|
+
Before you read about mongoid_spatial have sure you read this:
|
150
|
+
|
151
|
+
http://mongoid.org/en/origin/docs/selection.html#standard
|
152
|
+
|
153
|
+
All MongoDB queries are handled by Mongoid.
|
154
|
+
|
155
|
+
|
156
|
+
You can use Geometry instance directly on any query:
|
157
|
+
|
158
|
+
* near
|
159
|
+
* Bar.near(location: person.house)
|
160
|
+
* Bar.where(:location.near => person.house)
|
161
|
+
|
162
|
+
|
163
|
+
* near_sphere
|
164
|
+
* Bar.near_sphere(location: person.house)
|
165
|
+
* Bar.where(:location.near_sphere => person.house)
|
166
|
+
|
167
|
+
|
168
|
+
* within_box
|
169
|
+
* Bar.within_box(location: hood.area)
|
170
|
+
|
171
|
+
|
172
|
+
* within_circle
|
173
|
+
* Bar.within_circle(location: hood.area)
|
174
|
+
|
175
|
+
|
176
|
+
* within_circle_sphere
|
177
|
+
* Bar.within_circle_sphere(location: hood.area)
|
178
|
+
|
179
|
+
|
180
|
+
* within_polygon
|
181
|
+
* Bar.within_polygon(location: city.area)
|
182
|
+
|
183
|
+
|
184
|
+
External Libraries
|
185
|
+
------------------
|
186
|
+
|
187
|
+
Use RGeo?
|
188
|
+
https://github.com/dazuma/rgeo
|
189
|
+
|
190
|
+
RGeo is a Ruby wrapper for Proj/GEOS.
|
191
|
+
It's perfect when you need to work with complex calculations and projections.
|
192
|
+
It'll require more stuff installed to compile/work.
|
193
|
+
|
194
|
+
|
195
|
+
Use GeoRuby?
|
196
|
+
https://github.com/nofxx/geo_ruby
|
197
|
+
|
198
|
+
GeoRuby is a pure Ruby Geometry Library.
|
199
|
+
It's perfect if you want simple calculations and/or keep your stack in pure ruby.
|
200
|
+
Albeit not full featured in maths it has a handful of methods and good import/export helpers.
|
201
|
+
|
202
|
+
Use Nothing?
|
203
|
+
|
204
|
+
This lib won't stand in your way.
|
205
|
+
Write your own wrapper if you want.
|
206
|
+
|
207
|
+
|
208
|
+
Geometry Helpers
|
209
|
+
----------------
|
210
|
+
|
211
|
+
We currently support GeoRuby and RGeo.
|
212
|
+
If you require one of those, a #to_geo and #to_rgeo, respectivelly,
|
213
|
+
method(s) will be available to all spatial fields, returning the
|
214
|
+
external library corresponding object.
|
215
|
+
|
216
|
+
To illustrate:
|
217
|
+
|
218
|
+
class Person
|
219
|
+
include Mongoid::Document
|
220
|
+
include Mongoid::Geospatial
|
221
|
+
|
222
|
+
field :location, type: Point
|
223
|
+
end
|
224
|
+
|
225
|
+
me = Person.new(location: [8, 8])
|
226
|
+
|
227
|
+
# Example with GeoRuby
|
228
|
+
point.class # Mongoid::Geospatial::Point
|
229
|
+
point.to_geo.class # GeoRuby::SimpleFeatures::Point
|
230
|
+
|
231
|
+
# Example with RGeo
|
232
|
+
point.class # Mongoid::Geospatial::Point
|
233
|
+
point.to_rgeo.class # RGeo::Geographic::SphericalPointImpl
|
234
|
+
|
235
|
+
|
236
|
+
Configure
|
237
|
+
---------
|
238
|
+
|
239
|
+
Assemble it as you need (use a initializer file):
|
240
|
+
|
241
|
+
With RGeo
|
242
|
+
|
243
|
+
Mongoid::Geospatial.with_rgeo!
|
244
|
+
# Optional
|
245
|
+
# Mongoid::Geospatial.factory = RGeo::Geographic.spherical_factory
|
246
|
+
|
247
|
+
|
248
|
+
With GeoRuby
|
249
|
+
|
250
|
+
Mongoid::Geospatial.with_georuby!
|
251
|
+
|
252
|
+
|
253
|
+
Defaults (change if you know what you're doing)
|
254
|
+
|
255
|
+
Mongoid::Geospatial.lng_symbol = :x
|
256
|
+
Mongoid::Geospatial.lat_symbol = :y
|
257
|
+
Mongoid::Geospatial.earth_radius = EARTH_RADIUS
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
Model Setup
|
262
|
+
-----------
|
263
|
+
|
264
|
+
You can create Point, Line, Circle, Box and Polygon on your models:
|
265
|
+
|
266
|
+
|
267
|
+
class CrazyGeom
|
268
|
+
include Mongoid::Document
|
269
|
+
include Mongoid::Geospatial
|
270
|
+
|
271
|
+
field :location, type: Point, spatial: true, delegate: true
|
272
|
+
|
273
|
+
field :route, type: Line
|
274
|
+
field :area, type: Polygon
|
275
|
+
|
276
|
+
field :square, type: Box
|
277
|
+
field :around, type: Circle
|
278
|
+
|
279
|
+
# default mongodb options
|
280
|
+
spatial_index :location, {bit: 24, min: -180, max: 180}
|
281
|
+
|
282
|
+
# query by location
|
283
|
+
spatial_scope :location
|
284
|
+
end
|
285
|
+
|
286
|
+
|
287
|
+
Helpers
|
288
|
+
-------
|
289
|
+
|
290
|
+
You can use `spatial: true` to add an '2d' index automatically,
|
291
|
+
No need for `spatial_index :location`:
|
292
|
+
|
293
|
+
|
294
|
+
field :location, type: Point, spatial: true
|
295
|
+
|
296
|
+
|
297
|
+
You can delegate some point methods to the instance itself:
|
298
|
+
|
299
|
+
|
300
|
+
field :location, type: Point, delegate: true
|
301
|
+
|
302
|
+
|
303
|
+
Now instead of `instance.location.x` you may call `instance.x`.
|
304
|
+
|
305
|
+
|
306
|
+
Nearby
|
307
|
+
------
|
308
|
+
|
309
|
+
You can add a `spatial_scope` on your models. So you can query:
|
310
|
+
|
311
|
+
Bar.nearby(my.location)
|
312
|
+
|
313
|
+
instead of
|
314
|
+
|
315
|
+
Bar.near(location: my.location)
|
316
|
+
|
317
|
+
Good when you're drunk. Just add to your model:
|
318
|
+
|
319
|
+
spatial_scope :<field>
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
Geometry
|
324
|
+
--------
|
325
|
+
|
326
|
+
You can also store Circle, Box, Line (LineString) and Polygons.
|
327
|
+
Some helper methods are available to them:
|
328
|
+
|
329
|
+
|
330
|
+
# Returns a geometry bounding box
|
331
|
+
# Useful to query #within_box
|
332
|
+
polygon.bbox
|
333
|
+
polygon.bounding_box
|
334
|
+
|
335
|
+
# Returns a geometry calculated middle point
|
336
|
+
# Useful to query for #near
|
337
|
+
polygon.center
|
338
|
+
|
339
|
+
# Returns middle point + radius
|
340
|
+
# Useful to search #within_circle
|
341
|
+
polygon.radius(5) # [[1.0, 1.0], 5]
|
342
|
+
polygon.radius_sphere(5) # [[1.0, 1.0], 0.00048..]
|
343
|
+
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
Mongo DB 1.9+ New Geo features
|
348
|
+
---------
|
349
|
+
|
350
|
+
Multi-location Documents v.1.9+
|
351
|
+
|
352
|
+
MongoDB now also supports indexing documents by multiple locations. These locations can be specified in arrays of sub-objects, for example:
|
353
|
+
|
354
|
+
```
|
355
|
+
> db.places.insert({ addresses : [ { name : "Home", loc : [55.5, 42.3] }, { name : "Work", loc : [32.3, 44.2] } ] })
|
356
|
+
> db.places.ensureIndex({ "addresses.loc" : "2d" })
|
357
|
+
```
|
358
|
+
|
359
|
+
Multiple locations may also be specified in a single field:
|
360
|
+
|
361
|
+
```
|
362
|
+
> db.places.insert({ lastSeenAt : [ { x : 45.3, y : 32.2 }, [54.2, 32.3], { lon : 44.2, lat : 38.2 } ] })
|
363
|
+
> db.places.ensureIndex({ "lastSeenAt" : "2d" })
|
364
|
+
```
|
365
|
+
|
366
|
+
By default, when performing geoNear or $near-type queries on collections containing multi-location documents, the same document may be returned multiple times, since $near queries return ordered results by distance. Queries using the $within operator by default do not return duplicate documents.
|
367
|
+
|
368
|
+
v2.0
|
369
|
+
In v2.0, this default can be overridden by the use of a $uniqueDocs parameter for geoNear and $within queries, like so:
|
370
|
+
|
371
|
+
```
|
372
|
+
> db.runCommand( { geoNear : "places" , near : [50,50], num : 10, uniqueDocs : false } )
|
373
|
+
> db.places.find( { loc : { $within : { $center : [[0.5, 0.5], 20], $uniqueDocs : true } } } )
|
374
|
+
```
|
375
|
+
|
376
|
+
Currently it is not possible to specify $uniqueDocs for $near queries
|
377
|
+
Whether or not uniqueDocs is true, when using a limit the limit is applied (as is normally the case) to the number of results returned (and not to the docs or locations). If running a geoNear query with uniqueDocs : true, the closest location in a document to the center of the search region will always be returned - this is not true for $within queries.
|
378
|
+
|
379
|
+
In addition, when using geoNear queries and multi-location documents, often it is useful to return not only distances, but also the location in the document which was used to generate the distance. In v2.0, to return the location alongside the distance in the geoNear results (in the field loc), specify includeLocs : true in the geoNear query. The location returned will be a copy of the location in the document used.
|
380
|
+
|
381
|
+
If the location was an array, the location returned will be an object with "0" and "1" fields in v2.0.0 and v2.0.1.
|
382
|
+
|
383
|
+
```
|
384
|
+
> db.runCommand({ geoNear : "places", near : [ 0, 0 ], maxDistance : 20, includeLocs : true })
|
385
|
+
{
|
386
|
+
"ns" : "test.places",
|
387
|
+
"near" : "1100000000000000000000000000000000000000000000000000",
|
388
|
+
"results" : [
|
389
|
+
{
|
390
|
+
"dis" : 5.830951894845301,
|
391
|
+
"loc" : {
|
392
|
+
"x" : 3,
|
393
|
+
"y" : 5
|
394
|
+
},
|
395
|
+
"obj" : {
|
396
|
+
"_id" : ObjectId("4e52672c15f59224bdb2544d"),
|
397
|
+
"name" : "Final Place",
|
398
|
+
"loc" : {
|
399
|
+
"x" : 3,
|
400
|
+
"y" : 5
|
401
|
+
}
|
402
|
+
}
|
403
|
+
},
|
404
|
+
{
|
405
|
+
"dis" : 14.142135623730951,
|
406
|
+
"loc" : {
|
407
|
+
"0" : 10,
|
408
|
+
"1" : 10
|
409
|
+
},
|
410
|
+
"obj" : {
|
411
|
+
"_id" : ObjectId("4e5266a915f59224bdb2544b"),
|
412
|
+
"name" : "Some Place",
|
413
|
+
"loc" : [
|
414
|
+
[
|
415
|
+
10,
|
416
|
+
10
|
417
|
+
],
|
418
|
+
[
|
419
|
+
50,
|
420
|
+
50
|
421
|
+
]
|
422
|
+
]
|
423
|
+
}
|
424
|
+
},
|
425
|
+
{
|
426
|
+
"dis" : 14.142135623730951,
|
427
|
+
"loc" : {
|
428
|
+
"0" : -10,
|
429
|
+
"1" : -10
|
430
|
+
},
|
431
|
+
"obj" : {
|
432
|
+
"_id" : ObjectId("4e5266ba15f59224bdb2544c"),
|
433
|
+
"name" : "Another Place",
|
434
|
+
"loc" : [
|
435
|
+
[
|
436
|
+
-10,
|
437
|
+
-10
|
438
|
+
],
|
439
|
+
[
|
440
|
+
-50,
|
441
|
+
-50
|
442
|
+
]
|
443
|
+
]
|
444
|
+
}
|
445
|
+
}
|
446
|
+
],
|
447
|
+
"stats" : {
|
448
|
+
"time" : 0,
|
449
|
+
"btreelocs" : 0,
|
450
|
+
"nscanned" : 5,
|
451
|
+
"objectsLoaded" : 3,
|
452
|
+
"avgDistance" : 11.371741047435734,
|
453
|
+
"maxDistance" : 14.142157540259815
|
454
|
+
},
|
455
|
+
"ok" : 1
|
456
|
+
}
|
457
|
+
```
|
458
|
+
|
459
|
+
The plan is to include this functionality in a future release. Please help out ;)
|
460
|
+
|
461
|
+
This Fork
|
462
|
+
---------
|
463
|
+
|
464
|
+
This fork is not backwards compatible with 'mongoid_spatial'.
|
465
|
+
This fork delegates calculations to the external libs and use Moped.
|
466
|
+
|
467
|
+
Change in your models:
|
468
|
+
|
469
|
+
include Mongoid::Spacial::Document
|
470
|
+
|
471
|
+
to
|
472
|
+
|
473
|
+
include Mongoid::Geospatial
|
474
|
+
|
475
|
+
|
476
|
+
And for the fields:
|
477
|
+
|
478
|
+
|
479
|
+
field :source, type: Array, spacial: true
|
480
|
+
|
481
|
+
to
|
482
|
+
|
483
|
+
field :source, type: Point, spatial: true
|
484
|
+
|
485
|
+
|
486
|
+
Beware the 't' and 'c' issue. It's spaTial.
|
487
|
+
|
488
|
+
|
489
|
+
|
490
|
+
Troubleshooting
|
491
|
+
---------------
|
492
|
+
|
493
|
+
**Mongo::OperationFailure: can't find special index: 2d**
|
494
|
+
|
495
|
+
Indexes need to be created. Execute command:
|
496
|
+
|
497
|
+
rake db:mongoid:create_indexes
|
498
|
+
|
499
|
+
Using mongoid-geospatial (or mongoid for that matter) without rails?
|
500
|
+
|
501
|
+
https://github.com/kristianmandrup/mongoid_indexing
|
502
|
+
|
503
|
+
You need a way to create indexes easily, check out the gem above.
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
Contributing
|
508
|
+
------------
|
509
|
+
|
510
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
511
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
512
|
+
* Fork the project
|
513
|
+
* Start a feature/bugfix branch
|
514
|
+
* Commit and push until you are happy with your contribution
|
515
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
516
|
+
* 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.
|
517
|
+
|
518
|
+
|
519
|
+
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/nofxx/mongoid-geospatial/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
|