Permalink
Browse files

Merge branch 'master' of git://github.com/kristianmandrup/mongoid_geo…

…spatial into geospatial

Conflicts:
	lib/mongoid_geospatial/geospatial/geo_near_results.rb
	spec/functional/geospatial/geo_near_results_spec.rb
  • Loading branch information...
2 parents 972e90b + ed72cf0 commit 7c45933f0333c5196298499daa01ddc09bc8e4a3 @niedhui committed Jul 25, 2012
Showing with 1,108 additions and 618 deletions.
  1. +12 −1 Gemfile
  2. +0 −20 LICENSE.txt
  3. +175 −17 README.md
  4. +24 −0 lib/mongoid_geospatial.rb
  5. +22 −20 lib/{mongoid_spacial → mongoid_geospatial}/contexts/mongo.rb
  6. +117 −0 lib/mongoid_geospatial/contextual/mongo.rb
  7. +10 −0 lib/mongoid_geospatial/criteria.rb
  8. +1 −1 lib/{mongoid_spacial → mongoid_geospatial}/criterion/complex.rb
  9. +14 −0 lib/mongoid_geospatial/criterion/inclusion.rb
  10. +11 −9 lib/{mongoid_spacial/criterion/near_spacial.rb → mongoid_geospatial/criterion/near_spatial.rb}
  11. +11 −9 lib/{mongoid_spacial/criterion/within_spacial.rb → mongoid_geospatial/criterion/within_spatial.rb}
  12. +13 −0 lib/mongoid_geospatial/extensions/rgeo_spherical_point_impl.rb
  13. +46 −0 lib/mongoid_geospatial/extensions/symbol.rb
  14. +16 −0 lib/mongoid_geospatial/field_option.rb
  15. +21 −0 lib/mongoid_geospatial/fields/line_string.rb
  16. +20 −0 lib/mongoid_geospatial/fields/mongoid2/line_string.rb
  17. +20 −0 lib/mongoid_geospatial/fields/mongoid2/point.rb
  18. +23 −0 lib/mongoid_geospatial/fields/mongoid2/polygon.rb
  19. +57 −0 lib/mongoid_geospatial/fields/point.rb
  20. +25 −0 lib/mongoid_geospatial/fields/polygon.rb
  21. 0 lib/{mongoid_spacial → mongoid_geospatial}/finders.rb
  22. +90 −0 lib/mongoid_geospatial/geospatial.rb
  23. +29 −0 lib/mongoid_geospatial/geospatial/core_ext.rb
  24. +8 −8 lib/{mongoid_spacial/spacial → mongoid_geospatial/geospatial}/geo_near_results.rb
  25. +5 −0 lib/mongoid_geospatial/version.rb
  26. +0 −11 lib/mongoid_spacial.rb
  27. +0 −5 lib/mongoid_spacial/criteria.rb
  28. +0 −3 lib/mongoid_spacial/criterion.rb
  29. +0 −14 lib/mongoid_spacial/criterion/inclusion.rb
  30. +0 −22 lib/mongoid_spacial/extentions/hash/criteria_helpers.rb
  31. +0 −46 lib/mongoid_spacial/extentions/symbol/inflections.rb
  32. +0 −38 lib/mongoid_spacial/field_option.rb
  33. +0 −56 lib/mongoid_spacial/spacial.rb
  34. +0 −27 lib/mongoid_spacial/spacial/core_ext.rb
  35. +0 −29 lib/mongoid_spacial/spacial/document.rb
  36. +0 −52 lib/mongoid_spacial/spacial/formulas.rb
  37. +0 −5 lib/mongoid_spacial/spacial/version.rb
  38. +27 −0 mongoid_geospatial.gemspec
  39. +0 −27 mongoid_spacial.gemspec
  40. +0 −18 spec/config/mongoid.yml
  41. +25 −15 spec/functional/{mongoid → }/contexts/mongo_spec.rb
  42. +17 −13 spec/functional/{mongoid → }/criterion/inclusion_spec.rb
  43. +6 −8 spec/functional/{mongoid/spacial → geospatial}/geo_near_results_spec.rb
  44. +0 −18 spec/functional/mongoid/spacial_spec.rb
  45. +65 −0 spec/functional/mongoid_geospatial_spec.rb
  46. +7 −5 spec/models/bar.rb
  47. +10 −0 spec/models/farm.rb
  48. +9 −9 spec/models/river.rb
  49. 0 spec/{unit/mongoid → mongoid_geospatial}/criterion/complex_spec.rb
  50. 0 spec/{unit/mongoid → mongoid_geospatial}/criterion/inclusion_spec.rb
  51. +3 −3 ...{unit/mongoid/criterion/near_spacial_spec.rb → mongoid_geospatial/criterion/near_spatial_spec.rb}
  52. +54 −0 spec/mongoid_geospatial/criterion/within_spatial_spec.rb
  53. +25 −0 spec/mongoid_geospatial/geospatial_spec.rb
  54. +34 −0 spec/mongoid_setup.rb
  55. +35 −12 spec/spec_helper.rb
  56. +2 −2 spec/support/authentication.rb
  57. 0 spec/{config → support}/mongod.conf
  58. +19 −0 spec/support/mongoid.yml
  59. +0 −52 spec/unit/mongoid/criterion/within_spacial_spec.rb
  60. +0 −37 spec/unit/mongoid/spacial/formulas_spec.rb
  61. +0 −6 spec/unit/mongoid/spacial_spec.rb
View
13 Gemfile
@@ -1,4 +1,15 @@
source 'http://rubygems.org'
-# Specify your gem's dependencies in mongoid_spacial.gemspec
+# Specify gem's dependencies in mongoid_geospatial.gemspec
gemspec
+
+gem 'rgeo'
+
+gem 'mongoid', '~> 2.4' # all specs pass on 2.4.10 :)
+# gem 'mongoid', :git => 'git://github.com/mongoid/mongoid'
+
+group :development do
+ gem 'rspec'
+ gem 'pry'
+# gem 'fuubar'
+end
View
@@ -1,20 +0,0 @@
-Copyright (c) 2011 Ryan Ong
-
-Permission is hereby granted, free of charge, to any person obtaining
-a copy of this software and associated documentation files (the
-"Software"), to deal in the Software without restriction, including
-without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to
-permit persons to whom the Software is furnished to do so, subject to
-the following conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
192 README.md
@@ -1,47 +1,61 @@
-Mongoid Spacial
-============
+Mongoid Geospatial
+==================
+
+A Mongoid Extension that simplifies and adds support for MongoDB and
+RGeo Spatial Calculations.
+
+*WARNING* There are no plans to support MongoDB < 2.0.
+
+*WARNING* Experimental Mongoid 3.0 support.
-A Mongoid Extention that simplifies and adds support for MongoDB Geo Spacial Calculations.
Quick Start
-----------
-Add mongoid_spacial to your Gemfile:
+Add mongoid_geospatial to your Gemfile:
```ruby
-gem 'mongoid_spacial'
+gem 'mongoid_geospatial'
```
Set up some slugs:
```ruby
class River
include Mongoid::Document
- include Mongoid::Spacial::Document
+ include Mongoid::Geospatial
field :name, type: String
field :length, type: Integer
field :average_discharge, type: Integer
- field :source, type: Array, spacial: true
+ field :source, type: Point, spatial: 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 }
+ field :mouth, type: Point, spatial: {lat: :latitude, lng: :longitude, return_array: true }
+ field :course, type: Polygon
- # simplified spacial indexing
+ # simplified spatial 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
+ spatial_index :source
end
```
+Avaiable data types:
+
+* Point
+* LineString
+* Polygon
+
+
Generate indexes on MongoDB:
```
rake db:mongoid:create_indexes
```
-Before we manipulate the data mongoid_spacial handles is what we call points.
+Before we manipulate the data mongoid_spatial handles is what we call points.
Points can be:
@@ -67,15 +81,15 @@ hudson = River.create(
mouth: {:latitude => 40.703056, :longitude => -74.026667}
)
-# now to access this spacial information we can now do this
+# now to access this spatial 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.
+# notice how the order of lng and lat were switched. it will always come out like this when using spatial.
# Also adds a handy distance function
hudson.distance_from(:source, [-74,40], {:unit=>:mi})
```
-Mongoid Geo has extended all built in spacial symbol extentions
+Mongoid Geo has extended all built in spatial symbol extensions
* near
* River.where(:source.near => [-73.98, 40.77])
@@ -123,7 +137,7 @@ Post-Result has some advantages that are listed below.
# :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
+# Prefered method to set is Mongoid::Geospatial.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)
@@ -150,8 +164,152 @@ rivers = rivers.per(25).page(1)
rivers.reset! # resets the object to it is original state right after query.
```
+Mongo DB 1.9+ New Geo features
+---------
+
+Multi-location Documents v.1.9+
+
+MongoDB now also supports indexing documents by multiple locations. These locations can be specified in arrays of sub-objects, for example:
+
+```
+> db.places.insert({ addresses : [ { name : "Home", loc : [55.5, 42.3] }, { name : "Work", loc : [32.3, 44.2] } ] })
+> db.places.ensureIndex({ "addresses.loc" : "2d" })
+```
+
+Multiple locations may also be specified in a single field:
+
+```
+> db.places.insert({ lastSeenAt : [ { x : 45.3, y : 32.2 }, [54.2, 32.3], { lon : 44.2, lat : 38.2 } ] })
+> db.places.ensureIndex({ "lastSeenAt" : "2d" })
+```
+
+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.
+
+ v2.0
+In v2.0, this default can be overridden by the use of a $uniqueDocs parameter for geoNear and $within queries, like so:
+
+```
+> db.runCommand( { geoNear : "places" , near : [50,50], num : 10, uniqueDocs : false } )
+> db.places.find( { loc : { $within : { $center : [[0.5, 0.5], 20], $uniqueDocs : true } } } )
+```
+
+ Currently it is not possible to specify $uniqueDocs for $near queries
+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.
+
+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.
+
+ 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.
+
+```
+> db.runCommand({ geoNear : "places", near : [ 0, 0 ], maxDistance : 20, includeLocs : true })
+{
+ "ns" : "test.places",
+ "near" : "1100000000000000000000000000000000000000000000000000",
+ "results" : [
+ {
+ "dis" : 5.830951894845301,
+ "loc" : {
+ "x" : 3,
+ "y" : 5
+ },
+ "obj" : {
+ "_id" : ObjectId("4e52672c15f59224bdb2544d"),
+ "name" : "Final Place",
+ "loc" : {
+ "x" : 3,
+ "y" : 5
+ }
+ }
+ },
+ {
+ "dis" : 14.142135623730951,
+ "loc" : {
+ "0" : 10,
+ "1" : 10
+ },
+ "obj" : {
+ "_id" : ObjectId("4e5266a915f59224bdb2544b"),
+ "name" : "Some Place",
+ "loc" : [
+ [
+ 10,
+ 10
+ ],
+ [
+ 50,
+ 50
+ ]
+ ]
+ }
+ },
+ {
+ "dis" : 14.142135623730951,
+ "loc" : {
+ "0" : -10,
+ "1" : -10
+ },
+ "obj" : {
+ "_id" : ObjectId("4e5266ba15f59224bdb2544c"),
+ "name" : "Another Place",
+ "loc" : [
+ [
+ -10,
+ -10
+ ],
+ [
+ -50,
+ -50
+ ]
+ ]
+ }
+ }
+ ],
+ "stats" : {
+ "time" : 0,
+ "btreelocs" : 0,
+ "nscanned" : 5,
+ "objectsLoaded" : 3,
+ "avgDistance" : 11.371741047435734,
+ "maxDistance" : 14.142157540259815
+ },
+ "ok" : 1
+}
+```
+
+The plan is to include this functionality in a future release. Please help out ;)
+
+This Fork
+---------
+
+This fork is not backwards compatible with 'mongoid_spatial'.
+This fork delegates all the calculation to the nice RGeo.
+As a result, all the GEOS/Proj features are available in Ruby/Mongoid.
+
+Change in your models:
+
+ include Mongoid::Spacial::Document
+
+to
+
+ include Mongoid::Geospatial
+
+
+And for the fields:
+
+
+ field :source, type: Array, spacial: true
+
+to
+
+ field :source, type: Point, spatial: true
+
+
+Beware the 't' and 'c' issue. It's spaTial.
+
+
+
Troubleshooting
--------------
+---------------
**Mongo::OperationFailure: can't find special index: 2d**
@@ -163,7 +321,7 @@ Thanks
* Thanks to Kristian Mandrup for creating the base of the gem and a few of the tests
* Thanks to CarZen LLC. for letting me release the code we are using
-Contributing to mongoid_spacial
+Contributing to mongoid_spatial
-----------
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
View
@@ -0,0 +1,24 @@
+require 'rgeo'
+require 'mongoid'
+require 'active_support/core_ext/string/inflections'
+require 'active_support/concern'
+if Mongoid::VERSION > '3'
+ require 'mongoid_geospatial/contextual/mongo'
+else
+ require 'mongoid_geospatial/contexts/mongo'
+end
+require 'mongoid_geospatial/contextual/mongo'
+require 'mongoid_geospatial/criteria'
+require 'mongoid_geospatial/extensions/symbol'
+require 'mongoid_geospatial/extensions/rgeo_spherical_point_impl'
+require 'mongoid_geospatial/field_option'
+
+fields_path = 'mongoid_geospatial/fields' + (Mongoid::VERSION > '3' ? '' : '/mongoid2')
+
+%w{point polygon line_string}.each do |type|
+ require "#{fields_path}/#{type}"
+end
+
+require 'mongoid_geospatial/finders'
+require 'mongoid_geospatial/geospatial'
+
Oops, something went wrong.

0 comments on commit 7c45933

Please sign in to comment.