mongoid_geo 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.textile
CHANGED
@@ -27,8 +27,8 @@ h3. Create geo-spatial index
|
|
27
27
|
|
28
28
|
<pre>
|
29
29
|
class Person
|
30
|
-
field :
|
31
|
-
index [[ :
|
30
|
+
field :location, :type => Array
|
31
|
+
index [[ :location, Mongo::GEO2D ]], :min => -180, :max => 180
|
32
32
|
end
|
33
33
|
|
34
34
|
# to ensure indexes are created, either:
|
@@ -47,9 +47,9 @@ The following briefly demonstrates all the features that Mongoid Geo currently p
|
|
47
47
|
|
48
48
|
h2. Geo index
|
49
49
|
|
50
|
-
Old/Manual way: @index [[ :
|
50
|
+
Old/Manual way: @index [[ :location, Mongo::GEO2D ]], :min => -180, :max => 180@
|
51
51
|
|
52
|
-
Using new _geo_index_ class method : @geo_index :
|
52
|
+
Using new _geo_index_ class method : @geo_index :location@
|
53
53
|
|
54
54
|
h2. Special geo-array attribute writer
|
55
55
|
|
@@ -71,17 +71,27 @@ With the new @:geo@ option supplied by _mongoid-geo_ :
|
|
71
71
|
|
72
72
|
<pre>
|
73
73
|
class Person
|
74
|
-
field :
|
74
|
+
field :location, :type => Array, :geo => true
|
75
75
|
|
76
|
-
geo_index :
|
76
|
+
geo_index :location
|
77
77
|
end
|
78
78
|
|
79
79
|
p = Person.new
|
80
|
-
# set via String or Strings
|
81
|
-
p.locations = "45.1, -3.4"
|
82
|
-
p.locations = "45.1", "-3.4"
|
83
80
|
|
84
|
-
|
81
|
+
# A Geo array can now be set via String or Strings, Hash or Object, here a few examples...
|
82
|
+
# Please see geo_fields_spec.rb for more options!
|
83
|
+
|
84
|
+
p.location = "45.1, -3.4"
|
85
|
+
p.location = "45.1", "-3.4"
|
86
|
+
p.location = {:lat => 45.1, :lng => -3.4}
|
87
|
+
p.location = [{:lat => 45.1, :lng => -3.4}]
|
88
|
+
p.location = {:latitude => 45.1, :longitude => -3.4}
|
89
|
+
|
90
|
+
my_location = Location.new :latitude => 45.1, :longitude => -3.4
|
91
|
+
p.location = my_location
|
92
|
+
|
93
|
+
# for each of the above, the following holds
|
94
|
+
assert([45.1, -3.4], p.location)
|
85
95
|
</pre>
|
86
96
|
|
87
97
|
|
@@ -90,7 +100,7 @@ h2. Mongoid Geo extra inclusions
|
|
90
100
|
Find addresses near a point using spherical distance calculation
|
91
101
|
|
92
102
|
<pre>
|
93
|
-
Address.nearSphere(:
|
103
|
+
Address.nearSphere(:location => [ 72, -44 ])
|
94
104
|
</pre>
|
95
105
|
|
96
106
|
|
@@ -99,8 +109,8 @@ h2. Mongoid Geo extra inflections
|
|
99
109
|
h3. nearSphere
|
100
110
|
|
101
111
|
<pre>
|
102
|
-
base.where(:
|
103
|
-
# => :
|
112
|
+
base.where(:location.nearSphere => [ 72, -44 ])
|
113
|
+
# => :location => { "$nearSphere" : [ 72, -44 ] }
|
104
114
|
</pre>
|
105
115
|
|
106
116
|
h3. nearMax
|
@@ -108,13 +118,13 @@ h3. nearMax
|
|
108
118
|
Find points near a given point within a maximum distance
|
109
119
|
|
110
120
|
<pre>
|
111
|
-
base.where(:
|
121
|
+
base.where(:location.nearMax => [[ 72, -44 ], 5])
|
112
122
|
# => { $near: [50, 40] , $maxDistance: 3 }
|
113
123
|
|
114
|
-
base.where(:
|
124
|
+
base.where(:location.nearMax(:sphere) => [[ 72, -44 ], 5])
|
115
125
|
# => { $nearSphere: [50, 40] , $maxDistanceSphere: 3 }
|
116
126
|
|
117
|
-
base.where(:
|
127
|
+
base.where(:location.nearMax(:sphere, :flat) => [[ 72, -44 ], 5])
|
118
128
|
# => { $nearSphere: [50, 40] , $maxDistance: 3 }
|
119
129
|
</pre>
|
120
130
|
|
@@ -124,7 +134,7 @@ You can also use a Hash to define the nearMax
|
|
124
134
|
places.where(:location.nearMax => {:point => [ 72, -44 ], :distance => 5})
|
125
135
|
</pre>
|
126
136
|
|
127
|
-
Or use an Object (which must have the methods
|
137
|
+
Or use an Object (which must have the methods @#point@ and @#distance@ that return the point and max distance from that point)
|
128
138
|
|
129
139
|
<pre>
|
130
140
|
near_max_ = (Struct.new :point, :distance).new
|
@@ -134,7 +144,7 @@ Or use an Object (which must have the methods #point and #distance that return t
|
|
134
144
|
places.where(:location.nearMax => near_max)
|
135
145
|
</pre>
|
136
146
|
|
137
|
-
Note: For the points, you can also use a hash or an object with the methods/keys, either
|
147
|
+
Note: For the points, you can also use a hash or an object with the methods/keys, either @:lat, :lng@ or @:latitude, :longitude@
|
138
148
|
|
139
149
|
Example:
|
140
150
|
|
@@ -153,10 +163,10 @@ h3. withinBox
|
|
153
163
|
|
154
164
|
<pre>
|
155
165
|
box = [[50, 40], [30,55]]
|
156
|
-
base.where(:
|
166
|
+
base.where(:location.withinBox => box)
|
157
167
|
# => locations: {"$within" : {"$box" : [[50, 40], [30,55]]}
|
158
168
|
|
159
|
-
base.where(:
|
169
|
+
base.where(:location.withinBox(:sphere) => box)
|
160
170
|
# => locations: {"$within" : {"$boxSphere" : [[50, 40], [30,55]]}
|
161
171
|
</pre>
|
162
172
|
|
@@ -170,7 +180,7 @@ You can also use a Hash to define the box
|
|
170
180
|
places.where(:location.withinBox => {:lower_left => {:lat => 50, :lng => 40}, :upper_right => [30,55] } )
|
171
181
|
</pre>
|
172
182
|
|
173
|
-
Or use an
|
183
|
+
Or use an object (which must have the methods @#lower_left@ and @#upper_right@ that return the points of the bounding box)
|
174
184
|
|
175
185
|
<pre>
|
176
186
|
box = (Struct.new :lower_left, :upper_right).new
|
@@ -193,13 +203,13 @@ h3. withinCenter
|
|
193
203
|
# => places: {"$within" : {"$centerSphere" : [[50, 40], 4]}
|
194
204
|
</pre>
|
195
205
|
|
196
|
-
You can also use a
|
206
|
+
You can also use a hash to define the circle, with @:center@ and @:radius@ keys
|
197
207
|
|
198
208
|
<pre>
|
199
209
|
places.where(:location.withinCenter => {:center => [50, 40], :radius => 4})
|
200
210
|
</pre>
|
201
211
|
|
202
|
-
Or use an
|
212
|
+
Or use an object (which must have the methods #lower_left and #upper_right that return the points of the bounding box)
|
203
213
|
|
204
214
|
<pre>
|
205
215
|
circle = (Struct.new :center, :radius).new
|
@@ -21,11 +21,11 @@ module Mongoid #:nodoc:
|
|
21
21
|
def make_hash v
|
22
22
|
if operator =~ /box/
|
23
23
|
v = extract_box(v) if !v.kind_of?(Array)
|
24
|
-
v = [
|
24
|
+
v = [to_point(v.first), to_point(v.last)]
|
25
25
|
end
|
26
26
|
|
27
27
|
v = extract_circle(v) if !v.kind_of?(Array) && operator =~ /center/
|
28
|
-
{"$#{outer_op}" => {"$#{operator}" =>
|
28
|
+
{"$#{outer_op}" => {"$#{operator}" => v } }
|
29
29
|
end
|
30
30
|
|
31
31
|
def hash
|
@@ -42,11 +42,9 @@ module Mongoid #:nodoc:
|
|
42
42
|
end
|
43
43
|
|
44
44
|
protected
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
49
|
-
|
45
|
+
|
46
|
+
include Mongoid::Geo::PointConversion
|
47
|
+
|
50
48
|
def extract_circle(v)
|
51
49
|
case v
|
52
50
|
when Hash
|
@@ -20,7 +20,7 @@ module Mongoid #:nodoc:
|
|
20
20
|
|
21
21
|
def make_hash v
|
22
22
|
v = extract_nearMax(v) if !v.kind_of?(Array) && op_b =~ /max/i
|
23
|
-
{"$#{op_a}" =>
|
23
|
+
{"$#{op_a}" => to_point(v.first), "$#{op_b}" => to_point(v.last) }
|
24
24
|
end
|
25
25
|
|
26
26
|
def hash
|
@@ -37,12 +37,9 @@ module Mongoid #:nodoc:
|
|
37
37
|
end
|
38
38
|
|
39
39
|
protected
|
40
|
-
|
41
|
-
def to_points v
|
42
|
-
return v if v.kind_of? Fixnum
|
43
|
-
v.extend(Mongoid::Geo::Point).to_points
|
44
|
-
end
|
45
40
|
|
41
|
+
include Mongoid::Geo::PointConversion
|
42
|
+
|
46
43
|
def extract_nearMax(v)
|
47
44
|
case v
|
48
45
|
when Hash
|
data/lib/mongoid/geo/fields.rb
CHANGED
@@ -8,11 +8,15 @@ module Mongoid #:nodoc
|
|
8
8
|
define_method("#{meth}=") do |value|
|
9
9
|
if options[:type] == Array && options[:geo]
|
10
10
|
value = case value
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
when String
|
12
|
+
value.split(",").map(&:to_f)
|
13
|
+
when Array
|
14
|
+
value.compact.extend(Mongoid::Geo::Point).to_points
|
15
|
+
else
|
16
|
+
!value.nil? ? value.extend(Mongoid::Geo::Point).to_point : value
|
14
17
|
end
|
15
18
|
end
|
19
|
+
value = value[0..1] if !value.nil?
|
16
20
|
write_attribute(name, value)
|
17
21
|
end
|
18
22
|
define_method("#{meth}?") do
|
data/lib/mongoid/geo/point.rb
CHANGED
@@ -2,17 +2,42 @@ module Mongoid::Geo
|
|
2
2
|
module Point
|
3
3
|
# convert hash or object to [x, y] of floats
|
4
4
|
def to_points
|
5
|
+
v = self.kind_of?(Array) ? self.map {|p| p.kind_of?(Fixnum) ? p.to_f : p.extend(Mongoid::Geo::Point).to_point } : self
|
6
|
+
v.flatten
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_point
|
5
10
|
case self
|
6
11
|
when Hash
|
7
12
|
return [self[:lat], self[:lng]] if self[:lat]
|
8
13
|
return [self[:latitude], self[:longitude]] if self[:latitude]
|
9
14
|
raise "Hash must contain either :lat, :lng or :latitude, :longitude keys to be converted to a geo point"
|
15
|
+
when nil
|
16
|
+
nil
|
17
|
+
when Array
|
18
|
+
self.map(&:to_f)
|
10
19
|
else
|
11
20
|
return [self.lat, self.lng] if self.respond_to? :lat
|
12
21
|
return [self.latitude, self.longitude] if self.respond_to? :latitude
|
13
|
-
self
|
22
|
+
self.to_f
|
14
23
|
# raise 'Object must contain either #lat, #lng or #latitude, #longitude methods to be converted to a geo point'
|
15
24
|
end
|
16
25
|
end
|
17
26
|
end
|
18
27
|
end
|
28
|
+
|
29
|
+
module Mongoid::Geo
|
30
|
+
module PointConversion
|
31
|
+
protected
|
32
|
+
|
33
|
+
def to_point v
|
34
|
+
return v if v.kind_of? Fixnum
|
35
|
+
v.extend(Mongoid::Geo::Point).to_point
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_points v
|
39
|
+
return v if v.kind_of? Fixnum
|
40
|
+
v.extend(Mongoid::Geo::Point).to_points
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
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.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease: !!null
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,12 @@ authors:
|
|
9
9
|
autorequire: !!null
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-02-
|
12
|
+
date: 2011-02-06 00:00:00.000000000 +01:00
|
13
13
|
default_executable: !!null
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
17
|
-
requirement: &
|
17
|
+
requirement: &2156349680 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2156349680
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: mongoid
|
28
|
-
requirement: &
|
28
|
+
requirement: &2156349140 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: 2.0.0.rc.6
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *2156349140
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: bson_ext
|
39
|
-
requirement: &
|
39
|
+
requirement: &2156348440 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,7 +44,7 @@ dependencies:
|
|
44
44
|
version: 1.1.6
|
45
45
|
type: :runtime
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *2156348440
|
48
48
|
description: Geo spatial extension on Mongoid 2, to add more geo-spatial capabilities
|
49
49
|
email:
|
50
50
|
- kmandrup@gmail.com
|