mongoid-geospatial 4.0.1 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3610388899a7280b932823c0541ac28e1590bc4a
4
- data.tar.gz: 884000ab8ac5263b1fde24db7d966e823f9fa554
3
+ metadata.gz: e35bbf5de066ee3347846c61de3a32f8f034705b
4
+ data.tar.gz: ff4828b80b77ef40b8f3e3a91c3831dcab387e03
5
5
  SHA512:
6
- metadata.gz: cab14e04b4353673b88bafa569743546809c8f7d179f0272ccec68390dd03f6a98923a062ff749043d8ef66a810ad5db20d21d8acc07f10570d0b1b6b92abb5f
7
- data.tar.gz: 519caf325d7896a2db2ef706c60e166f67b793a827c0fb2b56965527c6c388b6ffd1ae6364d9602c12fbebe984e74444fcfc60c19bcff0870796a66b65f8a837
6
+ metadata.gz: e5c121ff1e4869322186eebc97362209e08b704957cbd96d2b52d3588005e6f560bced4a287e2251a6e9e16f04d02765f4140222c65b042ac39d7ec20506c987
7
+ data.tar.gz: 7576483685a5c3969413e7584ebb82676261ad8b4ffe9758226ce2df17485e16f3e901c6488c35275ab8f92c50f75c4b915d37d4b4878040350e1799646b32cf
@@ -23,3 +23,5 @@ matrix:
23
23
 
24
24
  services:
25
25
  - mongodb
26
+
27
+ sudo: false
data/README.md CHANGED
@@ -4,24 +4,19 @@ Mongoid Geospatial
4
4
 
5
5
  A Mongoid Extension that simplifies the use of MongoDB spatial features.
6
6
 
7
- ** On beta again **
7
+ * Version 5.x -> Mongoid 5
8
+ * Version 4.x -> Mongoid 4
8
9
 
9
- Removing some trash, improving and adding support for RGeo and GeoRuby.
10
- Version 3+ is going to be beta testing, when it's ready I'll release v4,
11
- So the major version stays the same as mongoid.
12
-
13
- ** Gem name: 'mongoid-geospatial' (notice the hyphen) **
14
-
15
- The name of this gem has changed.
10
+ ** Gem name has changed: 'mongoid-geospatial' (notice the hyphen) **
16
11
  Please change the underscore to a hyphen.
17
- If you need to: require 'mongoid/geospatial'
18
-
12
+ That will require it correctly within bundler:
13
+ `require 'mongoid/geospatial'`
19
14
 
20
- [![Gem Version](https://badge.fury.io/rb/mongoid-geospatial.png)](http://badge.fury.io/rb/mongoid-geospatial)
21
- [![Code Climate](https://codeclimate.com/github/nofxx/mongoid-geospatial.png)](https://codeclimate.com/github/nofxx/mongoid-geospatial)
22
- [![Coverage Status](https://coveralls.io/repos/nofxx/mongoid-geospatial/badge.png)](https://coveralls.io/r/nofxx/mongoid-geospatial)
15
+ [![Gem Version](https://badge.fury.io/rb/mongoid-geospatial.svg)](http://badge.fury.io/rb/mongoid-geospatial)
16
+ [![Code Climate](https://codeclimate.com/github/nofxx/mongoid-geospatial.svg)](https://codeclimate.com/github/nofxx/mongoid-geospatial)
17
+ [![Coverage Status](https://coveralls.io/repos/nofxx/mongoid-geospatial/badge.svg?branch=master)](https://coveralls.io/r/nofxx/mongoid-geospatial?branch=master)
23
18
  [![Dependency Status](https://gemnasium.com/nofxx/mongoid-geospatial.svg)](https://gemnasium.com/nofxx/mongoid-geospatial)
24
- [![Build Status](https://travis-ci.org/nofxx/mongoid-geospatial.png?branch=master)](https://travis-ci.org/nofxx/mongoid-geospatial)
19
+ [![Build Status](https://travis-ci.org/nofxx/mongoid-geospatial.svg?branch=master)](https://travis-ci.org/nofxx/mongoid-geospatial)
25
20
 
26
21
 
27
22
  Quick Start
@@ -34,7 +29,7 @@ But you may also use an external Geometric/Spatial gem alongside.
34
29
  gem 'mongoid-geospatial'
35
30
 
36
31
 
37
- A place to illustrate Point, Line and Polygon
32
+ A `Place` to illustrate `Point`, `Line` and `Polygon`
38
33
 
39
34
  class Place
40
35
  include Mongoid::Document
@@ -50,23 +45,19 @@ A place to illustrate Point, Line and Polygon
50
45
  field :route, type: Linestring
51
46
  field :area, type: Polygon
52
47
 
53
- # If your are going to query on your points, don't forget to index:
54
- spatial_index :location
48
+ # To query on your points, don't forget to index:
49
+ # You may use a method:
50
+ sphere_index :location # 2d
51
+ # or
52
+ spatial_index :location # 2dsphere
55
53
 
56
- # Or you can index points with 'spatial: true' option too:
57
- field :location, type: Point, spatial: true
54
+ # Or use a helper directly on the `field`:
55
+ field :location, type: Point, spatial: true # 2d
56
+ # or
57
+ field :location, type: Point, sphere: true # 2dsphere
58
58
  end
59
59
 
60
60
 
61
- For geo points, an extra macro `geo_field` is available
62
-
63
- geo_field :location
64
-
65
- Will generate:
66
-
67
- field :location, type: Point, spatial: true
68
-
69
-
70
61
 
71
62
  Generate indexes on MongoDB via rake:
72
63
 
@@ -81,7 +72,6 @@ Or programatically:
81
72
 
82
73
 
83
74
 
84
-
85
75
  Points
86
76
  ------
87
77
 
@@ -149,119 +139,6 @@ And for polygons and lines:
149
139
  house.area.center # Returns calculate middle point
150
140
 
151
141
 
152
- Query
153
- -----
154
-
155
- Before you read about this gem have sure you read this:
156
-
157
- http://mongoid.org/en/origin/docs/selection.html#standard
158
-
159
- All MongoDB queries are handled by Mongoid.
160
-
161
-
162
- You can use Geometry instance directly on any query:
163
-
164
- * near
165
- * Bar.near(location: person.house)
166
- * Bar.where(:location.near => person.house)
167
-
168
-
169
- * near_sphere
170
- * Bar.near_sphere(location: person.house)
171
- * Bar.where(:location.near_sphere => person.house)
172
-
173
-
174
- ### Not supported (until Mongoid 5.0.0)
175
- Those have been dropped from moped, should return on mongoid 5.
176
- Sad story.
177
-
178
- * within_box
179
- * Bar.within_box(location: hood.area)
180
-
181
-
182
- * within_circle
183
- * Bar.within_circle(location: hood.area)
184
-
185
-
186
- * within_circle_sphere
187
- * Bar.within_circle_sphere(location: hood.area)
188
-
189
-
190
- * within_polygon
191
- * Bar.within_polygon(location: city.area)
192
-
193
-
194
-
195
- External Libraries
196
- ------------------
197
-
198
- Use RGeo?
199
- https://github.com/dazuma/rgeo
200
-
201
- RGeo is a Ruby wrapper for Proj/GEOS.
202
- It's perfect when you need to work with complex calculations and projections.
203
- It'll require more stuff installed to compile/work.
204
-
205
-
206
- Use GeoRuby?
207
- https://github.com/nofxx/georuby
208
-
209
- GeoRuby is a pure Ruby Geometry Library.
210
- It's perfect if you want simple calculations and/or keep your stack in pure ruby.
211
- Albeit not full featured in maths it has a handful of methods and good import/export helpers.
212
-
213
-
214
-
215
- Geometry Helpers
216
- ----------------
217
-
218
- We currently support GeoRuby and RGeo.
219
- If you require one of those, a #to_geo and #to_rgeo, respectivelly,
220
- method(s) will be available to all spatial fields, returning the
221
- external library corresponding object.
222
-
223
- To illustrate:
224
-
225
- class Person
226
- include Mongoid::Document
227
- include Mongoid::Geospatial
228
-
229
- field :location, type: Point
230
- end
231
-
232
- me = Person.new(location: [8, 8])
233
-
234
- # Example with GeoRuby
235
- point.class # Mongoid::Geospatial::Point
236
- point.to_geo.class # GeoRuby::SimpleFeatures::Point
237
-
238
- # Example with RGeo
239
- point.class # Mongoid::Geospatial::Point
240
- point.to_rgeo.class # RGeo::Geographic::SphericalPointImpl
241
-
242
-
243
- Configure
244
- ---------
245
-
246
- Assemble it as you need (use a initializer file):
247
-
248
- With RGeo
249
-
250
- Mongoid::Geospatial.with_rgeo!
251
- # Optional
252
- # Mongoid::Geospatial.factory = RGeo::Geographic.spherical_factory
253
-
254
-
255
- With GeoRuby
256
-
257
- Mongoid::Geospatial.with_georuby!
258
-
259
-
260
- Defaults (change if you know what you're doing)
261
-
262
- Mongoid::Geospatial.lng_symbol = :x
263
- Mongoid::Geospatial.lat_symbol = :y
264
- Mongoid::Geospatial.earth_radius = EARTH_RADIUS
265
142
 
266
143
 
267
144
 
@@ -294,13 +171,20 @@ You can create Point, Line, Circle, Box and Polygon on your models:
294
171
  Helpers
295
172
  -------
296
173
 
297
- You can use `spatial: true` to add an '2d' index automatically,
174
+ You can use `spatial: true` to add a '2d' index automatically,
298
175
  No need for `spatial_index :location`:
299
176
 
300
177
 
301
178
  field :location, type: Point, spatial: true
302
179
 
303
180
 
181
+ And you can use `sphere: true` to add a '2dsphere' index automatically,
182
+ No need for `spatial_sphere :location`:
183
+
184
+
185
+ field :location, type: Point, sphere: true
186
+
187
+
304
188
  You can delegate some point methods to the instance itself:
305
189
 
306
190
 
@@ -335,7 +219,7 @@ Some helper methods are available to them:
335
219
 
336
220
 
337
221
  # Returns a geometry bounding box
338
- # Useful to query #within_box
222
+ # Useful to query #within_geometry
339
223
  polygon.bbox
340
224
  polygon.bounding_box
341
225
 
@@ -349,127 +233,123 @@ Some helper methods are available to them:
349
233
  polygon.radius_sphere(5) # [[1.0, 1.0], 0.00048..]
350
234
 
351
235
 
236
+ Query
237
+ -----
352
238
 
239
+ Before you read about this gem have sure you read this:
353
240
 
354
- Mongo DB 1.9+ New Geo features
355
- ---------
241
+ http://mongoid.org/en/origin/docs/selection.html#standard
356
242
 
357
- Multi-location Documents v.1.9+
243
+ All MongoDB queries are handled by Mongoid/Origin.
244
+
245
+ http://www.rubydoc.info/github/mongoid/origin/Origin/Selectable
246
+
247
+ You can use Geometry instance directly on any query:
248
+
249
+ * near
358
250
 
359
- MongoDB now also supports indexing documents by multiple locations. These locations can be specified in arrays of sub-objects, for example:
360
251
 
361
252
  ```
362
- > db.places.insert({ addresses : [ { name : "Home", loc : [55.5, 42.3] }, { name : "Work", loc : [32.3, 44.2] } ] })
363
- > db.places.ensureIndex({ "addresses.loc" : "2d" })
253
+ Bar.near(location: person.house)
254
+ Bar.where(:location.near => person.house)
364
255
  ```
365
256
 
366
- Multiple locations may also be specified in a single field:
257
+ * near_sphere
367
258
 
368
259
  ```
369
- > db.places.insert({ lastSeenAt : [ { x : 45.3, y : 32.2 }, [54.2, 32.3], { lon : 44.2, lat : 38.2 } ] })
370
- > db.places.ensureIndex({ "lastSeenAt" : "2d" })
260
+ Bar.near_sphere(location: person.house)
261
+ Bar.where(:location.near_sphere => person.house)
371
262
  ```
372
263
 
373
- 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.
374
-
375
- v2.0
376
- In v2.0, this default can be overridden by the use of a $uniqueDocs parameter for geoNear and $within queries, like so:
264
+ * within_polygon
377
265
 
378
266
  ```
379
- > db.runCommand( { geoNear : "places" , near : [50,50], num : 10, uniqueDocs : false } )
380
- > db.places.find( { loc : { $within : { $center : [[0.5, 0.5], 20], $uniqueDocs : true } } } )
267
+ Bar.within_polygon(location: [[[x,y],...[x,y]]])
268
+ # or with a bbox
269
+ Bar.within_polygon(location: street.bbox)
381
270
  ```
382
271
 
383
- Currently it is not possible to specify $uniqueDocs for $near queries
384
- 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.
385
272
 
386
- 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.
273
+ * intersects_line
274
+ * intersects_point
275
+ * intersects_polygon
387
276
 
388
- 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.
389
277
 
390
- ```
391
- > db.runCommand({ geoNear : "places", near : [ 0, 0 ], maxDistance : 20, includeLocs : true })
392
- {
393
- "ns" : "test.places",
394
- "near" : "1100000000000000000000000000000000000000000000000000",
395
- "results" : [
396
- {
397
- "dis" : 5.830951894845301,
398
- "loc" : {
399
- "x" : 3,
400
- "y" : 5
401
- },
402
- "obj" : {
403
- "_id" : ObjectId("4e52672c15f59224bdb2544d"),
404
- "name" : "Final Place",
405
- "loc" : {
406
- "x" : 3,
407
- "y" : 5
408
- }
409
- }
410
- },
411
- {
412
- "dis" : 14.142135623730951,
413
- "loc" : {
414
- "0" : 10,
415
- "1" : 10
416
- },
417
- "obj" : {
418
- "_id" : ObjectId("4e5266a915f59224bdb2544b"),
419
- "name" : "Some Place",
420
- "loc" : [
421
- [
422
- 10,
423
- 10
424
- ],
425
- [
426
- 50,
427
- 50
428
- ]
429
- ]
430
- }
431
- },
432
- {
433
- "dis" : 14.142135623730951,
434
- "loc" : {
435
- "0" : -10,
436
- "1" : -10
437
- },
438
- "obj" : {
439
- "_id" : ObjectId("4e5266ba15f59224bdb2544c"),
440
- "name" : "Another Place",
441
- "loc" : [
442
- [
443
- -10,
444
- -10
445
- ],
446
- [
447
- -50,
448
- -50
449
- ]
450
- ]
451
- }
452
- }
453
- ],
454
- "stats" : {
455
- "time" : 0,
456
- "btreelocs" : 0,
457
- "nscanned" : 5,
458
- "objectsLoaded" : 3,
459
- "avgDistance" : 11.371741047435734,
460
- "maxDistance" : 14.142157540259815
461
- },
462
- "ok" : 1
463
- }
464
- ```
465
278
 
466
- The plan is to include this functionality in a future release. Please help out ;)
279
+ External Libraries
280
+ ------------------
281
+
282
+ We currently support GeoRuby and RGeo.
283
+ If you require one of those, a #to_geo and #to_rgeo, respectivelly,
284
+ method(s) will be available to all spatial fields, returning the
285
+ external library corresponding object.
286
+
287
+
288
+ ### Use RGeo?
289
+ https://github.com/dazuma/rgeo
290
+
291
+ RGeo is a Ruby wrapper for Proj/GEOS.
292
+ It's perfect when you need to work with complex calculations and projections.
293
+ It'll require more stuff installed to compile/work.
294
+
295
+
296
+ ### Use GeoRuby?
297
+ https://github.com/nofxx/georuby
298
+
299
+ GeoRuby is a pure Ruby Geometry Library.
300
+ It's perfect if you want simple calculations and/or keep your stack in pure ruby.
301
+ Albeit not full featured in maths it has a handful of methods and good import/export helpers.
302
+
303
+
304
+ ### Example
305
+
306
+ class Person
307
+ include Mongoid::Document
308
+ include Mongoid::Geospatial
309
+
310
+ field :location, type: Point
311
+ end
312
+
313
+ me = Person.new(location: [8, 8])
314
+
315
+ # Example with GeoRuby
316
+ point.class # Mongoid::Geospatial::Point
317
+ point.to_geo.class # GeoRuby::SimpleFeatures::Point
318
+
319
+ # Example with RGeo
320
+ point.class # Mongoid::Geospatial::Point
321
+ point.to_rgeo.class # RGeo::Geographic::SphericalPointImpl
322
+
323
+
324
+ ## Configure
325
+
326
+ Assemble it as you need (use a initializer file):
327
+
328
+ With RGeo
329
+
330
+ Mongoid::Geospatial.with_rgeo!
331
+ # Optional
332
+ # Mongoid::Geospatial.factory = RGeo::Geographic.spherical_factory
333
+
334
+
335
+ With GeoRuby
336
+
337
+ Mongoid::Geospatial.with_georuby!
338
+
339
+
340
+ Defaults (change if you know what you're doing)
341
+
342
+ Mongoid::Geospatial.lng_symbol = :x
343
+ Mongoid::Geospatial.lat_symbol = :y
344
+ Mongoid::Geospatial.earth_radius = EARTH_RADIUS
345
+
346
+
467
347
 
468
348
  This Fork
469
349
  ---------
470
350
 
471
- This fork is not backwards compatible with 'mongoid_spatial'.
472
- This fork delegates calculations to the external libs and use Moped.
351
+ This fork is not backwards compatible with 'mongoid_spacial'.
352
+ This fork delegates calculations to external libs.
473
353
 
474
354
  Change in your models:
475
355
 
@@ -487,7 +367,7 @@ And for the fields:
487
367
 
488
368
  to
489
369
 
490
- field :source, type: Point, spatial: true
370
+ field :source, type: Point, spatial: true # or sphere: true
491
371
 
492
372
 
493
373
  Beware the 't' and 'c' issue. It's spaTial.
@@ -503,6 +383,10 @@ Indexes need to be created. Execute command:
503
383
 
504
384
  rake db:mongoid:create_indexes
505
385
 
386
+ Programatically
387
+
388
+ Model.create_indexes
389
+
506
390
 
507
391
  Contributing
508
392
  ------------