mongoid_geo 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -4,13 +4,16 @@ A Geo extension for Mongoid.
4
4
 
5
5
  "MongoDB Geospatial Indexing":http://www.mongodb.org/display/DOCS/Geospatial+Indexing
6
6
 
7
- * Supports Mongoid 1.7 sphere distance calculations and
7
+ * Supports Mongo DB 1.7+ sphere distance calculations
8
8
  * Adds nearSphere inclusion method
9
9
  * Adds a set of geo related inflections
10
10
  * Adds an exta option for defining a "geo" field, to have the generated attr_writer parse and convert strings etc. to float arrays.
11
+ * Calculate locations near a given point (geoNear)
11
12
 
12
13
  h2. Mongoid 2 geo features
13
14
 
15
+ The following summarized what geo functionality is already provided by Mongoid 2.0 (as far as I could tell, May 9th, 2011)
16
+
14
17
  h3. Find addresses near a point
15
18
 
16
19
  <pre>
@@ -38,26 +41,29 @@ h3. Create geo-spatial index
38
41
  autocreate_indexes: true
39
42
  </pre>
40
43
 
41
- These are the only geo features I could find are currently built-in for Mongoid 2.
42
- _Mongoid Geo_ implements the following extra features...
44
+ These are the only geo features I could find that are currently built-in for Mongoid 2.
45
+
46
+ _Mongoid Geo_ implements some nice extra geo features:
43
47
 
44
48
  h1. Mongoid Geo features
45
49
 
46
- The following briefly demonstrates all the features that Mongoid Geo currently provides
50
+ The following briefly demonstrates all the features that *mongoid-geo* provides:
47
51
 
48
52
  h2. Geo index
49
53
 
50
- Old/Manual way: @index [[ :location, Mongo::GEO2D ]], :min => -180, :max => 180@
54
+ A new _geo_index_ class method
51
55
 
52
- Using new _geo_index_ class method : @geo_index :location@
56
+ Usage example:
57
+
58
+ @geo_index :location@
53
59
 
54
60
  Note: For embedded documents, you must define the index in the root collection class. (davemitchell)
55
61
 
56
- h2. Special geo-array attribute writer
62
+ h2. Geo option for Array GPS location field
57
63
 
58
- When setting a geo-location array, the setter should try to convert the value to an array of floats
64
+ Objective: When setting a geo GPS location array, the setter should try to convert the value to an array of floats
59
65
 
60
- Old/Manual way:
66
+ The "old" manual way:
61
67
 
62
68
  <pre>
63
69
  class Person
@@ -69,7 +75,9 @@ Old/Manual way:
69
75
  end
70
76
  </pre>
71
77
 
72
- With the new @:geo@ option supplied by _mongoid-geo_ :
78
+ *mongoid-geo* provides a new @:geo@ option that can be used with any Array field:
79
+
80
+ Usage example:
73
81
 
74
82
  <pre>
75
83
  class Person
@@ -121,11 +129,12 @@ With the new @:geo@ option supplied by _mongoid-geo_ :
121
129
 
122
130
  # or alternatively
123
131
  Mongoid::Geo.spherical = true
132
+
133
+ address.location = "23.5, -47"
134
+ address.location.should == [23.5, -47].reverse
124
135
  </pre>
125
136
 
126
- h2. News update (May 9, 2011)
127
-
128
- The geoNear queries can now convert the result into a Mongoid::Criteria, where distance from a given location is sorted in either descending or ascending (default) order
137
+ h2. geoNear
129
138
 
130
139
  <pre>
131
140
  class Address
@@ -148,46 +157,42 @@ end
148
157
  </pre>
149
158
 
150
159
  Find all positions sorted nearest to the address loation
160
+
151
161
  @nearest_positions = Position.geoNear(another_address.location, :pos)@
152
162
 
153
163
  Perform distance locations in Speherical mode inside Mongo DB (default is :plane)
164
+
154
165
  @nearest_positions = Position.geoNear(another_address.location, :pos, :mode => :sphere)@
155
166
 
156
167
  Other options supported are: @:num, :maxDistance, :distanceMultiplier, :query@
157
168
 
158
169
  GeoNear returns each distance calculated in degrees. Use the :distanceMultiplier or :unit option to return in the unit of your choice (see unit.rb).
159
170
 
160
- Set :distanceMultiplier to 6371 to get distance in KM
161
- Set :distanceMultiplier to 3963.19 to get distance in Miles
171
+ Set @:distanceMultiplier = 6371@ to get distance in KMs
172
+ Set @@:distanceMultiplier = @3963.19@ to get distance in Miles
162
173
 
163
- You can also use the :unit option instead like this (supports :feet, :meters, :kms, :miles)
174
+ You can also use the :unit option instead like this (supports :feet, :meters, :kms, :miles):
164
175
 
165
- @results = Address.geoNear @center.location, :location, :unit => :km, :dist_order => :desc@
176
+ @results = Address.geoNear @center.location, :location, :unit => :feet, :dist_order => :desc@
166
177
 
167
- The geoNear query result is returned as Mongoid::Criteria
178
+ The _geoNear_ query result is returned as a _Mongoid::Criteria_
168
179
 
169
180
  @results.desc(:distance).map(&:distance)@
170
181
 
171
- Note that the :fromLocation field, stores the location the distance was last calculated as a hash of the Point it was calculated from
182
+ Note that the @:fromLocation@ field, stores the location the distance was last calculated as a Hash of the GPS location point it was calculated from:
172
183
 
173
184
  @[23.5, -47].hash@
174
185
 
175
- </pre>
176
-
177
- If you need to operate on the Mongoid models referenced by the query result, simply call #to_models on it
178
- Fx to get the city of the first model instance from the query result:
186
+ This hash can be retrieved (and used for comparison?) using the @fromHash@ field
179
187
 
180
- @nearest_city = Position.geoNear(another_address.location, :pos).to_models.first.city@
188
+ @from = results.first.fromHash@
181
189
 
182
- You can also use a #to_model method on an individual query result like this:
190
+ You can also at any time get the GPS location point which the distance of any result instance was calculated from, using the @fromPoint field
183
191
 
184
- @nearest_city = Position.geoNear(another_address.location, :pos).first.to_model.city@
185
-
186
- # the model returned also has a distance accessor populated with the distance calculated by running the geoNear query
187
-
188
- @nearest_distance = Position.geoNear(another_address.location, :pos).first.to_model.distance@
192
+ @from = results.first.fromPoint@
193
+ </pre>
189
194
 
190
- You can now explicitly set/configure the Mongo DB version used. This will affect whether built-in Mongo DB distance calculation will be used or using standalone Ruby Haversine algorithm. By default the version is set to 1.5. See _geo_near_ specs for more details/info on this.
195
+ You can now explicitly set/configure the Mongo DB version used. This will affect whether built-in Mongo DB distance calculation will be used or using standalone Ruby Haversine algorithm. By default the Mongo DB version is set to 1.8 (as of May 9, 2011) . See _geo_near_ specs for more details/info on this.
191
196
 
192
197
  @Mongoid::Geo.mongo_db_version = 1.7@
193
198
 
@@ -28,7 +28,8 @@ module Mongoid #:nodoc
28
28
  define_method("#{meth}=") do |value|
29
29
  if options[:geo]
30
30
  self.class.send :field, :distance, :type => Float
31
- self.class.send :field, :fromLocation, :type => String
31
+ self.class.send :field, :fromHash, :type => String
32
+ self.class.send :field, :fromPoint, :type => Array
32
33
  end
33
34
 
34
35
  if options[:type] == Array && options[:geo]
@@ -36,11 +36,12 @@ module Mongoid
36
36
  module Models
37
37
  def to_models mode = nil
38
38
  distance_hash = Hash[ self.map {|item| [item._id, item.distance] } ]
39
- from_hash = Hash[ self.map { |item| [item._id, item.fromLocation] } ]
39
+ from_hash = Hash[ self.map { |item| [item._id, item.fromPoint] } ]
40
40
 
41
41
  ret = to_criteria.to_a.map do |m|
42
42
  m.distance = distance_hash[m._id.to_s]
43
- m.fromLocation = from_hash[m._id.to_s]
43
+ m.fromPoint = from_hash[m._id.to_s]
44
+ m.fromHash = from_hash[m._id.to_s].hash
44
45
  m.save if mode == :save
45
46
  m
46
47
  end
@@ -50,7 +51,7 @@ module Mongoid
50
51
  def as_criteria direction = nil
51
52
  to_models(:save)
52
53
  ids = first.klass.all.map(&:_id)
53
- crit = Mongoid::Criteria.new(first.klass).where(:_id.in => ids, :fromLocation => first.fromLocation)
54
+ crit = Mongoid::Criteria.new(first.klass).where(:_id.in => ids, :fromHash => first.fromPoint.hash)
54
55
  crit = crit.send(direction, :distance) if direction
55
56
  crit
56
57
  end
@@ -98,7 +99,8 @@ module Mongoid
98
99
  # Calculate distance in KM or Miles if mongodb < 1.7
99
100
  r[distance_meth] ||= calc_distance(r, center, location_attribute, options) if Mongoid::Geo.mongo_db_version < 1.7
100
101
  r['klass'] = klass
101
- r['from'] = center.hash
102
+ r['fromPoint'] = center
103
+ # r['fromHash'] = center.hash
102
104
  end
103
105
  query_result
104
106
  end
@@ -107,7 +109,8 @@ module Mongoid
107
109
  qres.map do |qr|
108
110
  res = Hashie::Mash.new(qr['obj'].to_hash).extend(Mongoid::Geo::Model)
109
111
  res.klass = qr['klass']
110
- res.fromLocation = qr['from']
112
+ res.fromPoint = qr['fromPoint']
113
+ # res.fromHash = qr['fromHash']
111
114
  res.distance = qr[distance_meth]
112
115
  res._id = qr['obj']['_id'].to_s
113
116
  res
data/lib/mongoid_geo.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  require 'mongoid/geo'
2
2
 
3
- Mongoid::Geo.mongo_db_version = 1.5
3
+ Mongoid::Geo.mongo_db_version = 1.8
4
4
  Mongoid::Geo.spherical = false
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_geo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-05-09 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &2157028960 !ruby/object:Gem::Requirement
16
+ requirement: &2152549140 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2.4'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2157028960
24
+ version_requirements: *2152549140
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mongoid
27
- requirement: &2157044860 !ruby/object:Gem::Requirement
27
+ requirement: &2152548680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.0.1
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2157044860
35
+ version_requirements: *2152548680
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bson
38
- requirement: &2157044400 !ruby/object:Gem::Requirement
38
+ requirement: &2152548220 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '1.3'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2157044400
46
+ version_requirements: *2152548220
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: activesupport
49
- requirement: &2157043940 !ruby/object:Gem::Requirement
49
+ requirement: &2152547760 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 3.0.4
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *2157043940
57
+ version_requirements: *2152547760
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: hashie
60
- requirement: &2157043480 !ruby/object:Gem::Requirement
60
+ requirement: &2152547300 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 0.4.0
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *2157043480
68
+ version_requirements: *2152547300
69
69
  description: Geo spatial extension on Mongoid 2, to add more geo-spatial capabilities
70
70
  email:
71
71
  - kmandrup@gmail.com