mongoid_geospatial 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,7 +26,7 @@ You can also use an external Geometric/Spatial alongside.
26
26
  gem 'mongoid_geospatial'
27
27
 
28
28
 
29
- # A place to illustrate Point, LineString and Polygon
29
+ # A place to illustrate Point, Line and Polygon
30
30
  class Place
31
31
  include Mongoid::Document
32
32
  include Mongoid::Geospatial
@@ -67,7 +67,7 @@ To illustrate:
67
67
  Configure
68
68
  ----------------
69
69
 
70
- Assemble it as you need:
70
+ Assemble it as you need (use a initializer file):
71
71
 
72
72
  With RGeo
73
73
 
@@ -90,105 +90,143 @@ Defaults (change if you know what you're doing)
90
90
  Model Setup
91
91
  -----------
92
92
 
93
- You can create Point, LineString and Polygon on your models:
93
+ You can create Point, Line, Circle, Box and Polygon on your models:
94
94
 
95
95
 
96
- ```ruby
97
- class River
98
- include Mongoid::Document
99
- include Mongoid::Geospatial
96
+ class River
97
+ include Mongoid::Document
98
+ include Mongoid::Geospatial
100
99
 
101
- field :name, type: String
102
- field :length, type: Integer
103
- field :average_discharge, type: Integer
104
- field :source, type: Point, spatial: true
100
+ field :name, type: String
101
+ field :length, type: Integer
102
+ field :discharge, type: Integer
105
103
 
106
- # set return_array to true if you do not want a hash returned all the time
107
- field :mouth, type: Point, spatial: {lat: :latitude, lng: :longitude, return_array: true }
108
- field :course, type: Polygon
104
+ field :source, type: Point, spatial: true
105
+ field :mouth, type: Point, spatial: true
106
+ field :course, type: Line
107
+ field :boundings, type: Box
109
108
 
110
- # simplified spatial indexing
111
- # you can only index one point in mongodb version below 1.9
112
- # if you want something besides the defaults {bit: 24, min: -180, max: 180} just set index to the options on the index
113
- spatial_index :source
109
+ # spatial indexing
110
+ spatial_index :mouth
111
+
112
+ # default mongodb options
113
+ spatial_index :mouth, {bit: 24, min: -180, max: 180}
114
+ end
114
115
 
115
- end
116
- ```
117
116
 
117
+ Use
118
+ ---
118
119
 
119
120
  Generate indexes on MongoDB:
120
121
 
121
- ```
122
- rake db:mongoid:create_indexes
123
- ```
124
122
 
123
+ rake db:mongoid:create_indexes
125
124
 
126
- Before we manipulate the data mongoid_spatial handles is what we call points.
127
125
 
128
- Points can be:
126
+ Points
127
+ ------
129
128
 
130
129
  * an unordered hash with the lat long string keys defined when setting the field (only applies for setting the field)
131
- * longitude latitude array in that order - [long,lat]
130
+ * longitude latitude array in that order - [long,lat] ([x, y])
132
131
  * an unordered hash with latitude key(:lat, :latitude) and a longitude key(:lon, :long, :lng, :longitude)
133
132
  * an ordered hash with longitude as the first item and latitude as the second item
134
133
  This hash does not have include the latitude and longitude keys
135
134
  \*only works in ruby 1.9 and up because hashes below ruby 1.9 because they are not ordered
136
135
  * anything with the method to_lng_lat that converts it to a [long,lat]
136
+
137
137
  We store data in the DB as a [lng,lat] array then reformat when it is returned to you
138
138
 
139
- ```ruby
140
- hudson = River.create(
141
- name: 'Hudson',
142
- length: 315,
143
- average_discharge: 21_400,
144
- # when setting array LONGITUDE MUST BE FIRST LATITUDE MUST BE SECOND
145
- # source: [-73.935833,44.106667],
146
- # but we can use hash in any order,
147
- # the default keys for latitude and longitude are :lat and :lng respectively
148
- source: {:lat => 44.106667, :lng => -73.935833},
149
- mouth: {:latitude => 40.703056, :longitude => -74.026667}
150
- )
151
-
152
- # now to access this spatial information we can now do this
153
- hudson.source #=> {:lng => -73.935833, :lat => 44.106667}
154
- hudson.mouth #=> [-74.026667, 40.703056] # notice how this returned as a lng,lat array because return_array was true
155
- # notice how the order of lng and lat were switched. it will always come out like this when using spatial.
156
- # Also adds a handy distance function
157
- hudson.distance_from(:source, [-74,40], {:unit=>:mi})
158
139
 
159
- ```
160
- Mongoid Geo has extended all built in spatial symbol extensions
140
+ hudson = River.create(
141
+ name: 'Hudson',
142
+ length: 315,
143
+ discharge: 21_400,
144
+ # when setting array LNG (x) MUST BE FIRST LAT (y) MUST BE SECOND
145
+ # source: [-73.935833,44.106667],
146
+ # but we can use hash in any order
147
+ source: {:lat => 44.106667, :lng => -73.935833},
148
+ mouth: {:latitude => 40.703056, :longitude => -74.026667}
149
+
150
+ Now to access this spatial information we can do this
151
+
152
+ hudson.mouth # => [-74.026667, 40.703056]
153
+
154
+ Distance and other geometrical calculations are delegated to the external
155
+ library you choosed. More info about using RGeo or GeoRuby below.
156
+ Some built in helpers:
157
+
158
+ # Returns middle point + radius
159
+ # Useful to search #within_circle
160
+ hudson.mouth.radius(5) # [[-74.., 40..], 5]
161
+ hudson.mouth.radius_sphere(5) # [[-74.., 40..], 0.00048..]
162
+
163
+ # Returns hash if needed
164
+ hudson.mounth.to_hsh # {:x => -74.., :y => 40..}
165
+ hudson.mounth.to_hsh(:lon, :lat) # {:lon => -74.., :lat => 40..}
166
+
167
+
168
+ Query
169
+ --------
170
+
171
+ Before you read about mongoid_spatial have sure you read this:
172
+
173
+ http://mongoid.org/en/origin/docs/selection.html#standard
174
+
175
+ All MongoDB queries are handled by Mongoid.
176
+
177
+
178
+ You can use Geometry instance directly on any query:
161
179
 
162
180
  * near
163
- * River.where(:source.near => [-73.98, 40.77])
164
- * River.where(:source.near => [[-73.98, 40.77],5]) # sets max distance of 5
165
- * River.where(:source.near => {:point => [-73.98, 40.77], :max => 5}) # sets max distance of 5
166
- * River.where(:source.near(:sphere) => [[-73.98, 40.77],5]) # sets max distance of 5 radians
167
- * River.where(:source.near(:sphere) => {:point => [-73.98, 40.77], :max => 5, :unit => :km}) # sets max distance of 5 km
168
- * River.where(:source.near(:sphere) => [-73.98, 40.77])
169
- * within
170
- * River.where(:source.within(:box) => [[-73.99756,40.73083], [-73.988135,40.741404]])
171
- * River.where(:source.within(:box) => [ {:lat => 40.73083, :lng => -73.99756}, [-73.988135,40.741404]])
172
- * River.where(:source.within(:polygon) => [ [ 10, 20 ], [ 10, 40 ], [ 30, 40 ], [ 30, 20 ] ]
173
- * River.where(:source.within(:polygon) => { a : { x : 10, y : 20 }, b : { x : 15, y : 25 }, c : { x : 20, y : 20 } })
174
- * River.where(:source.within(:center) => [[-73.98, 40.77],5]) # same format as near
175
- * River.where(:source.within(:center_sphere) => [[-73.98, 40.77],5]) # same format as near(:sphere)
176
-
177
- One of the most handy features we have added is geo_near finder
178
-
179
- ```ruby
180
- # accepts all criteria chains except without, only, asc, desc, order\_by
181
- River.where(:name=>'hudson').geo_near({:lat => 40.73083, :lng => -73.99756})
182
-
183
- # geo\_near accepts a few parameters besides a point
184
- # :num = limit
185
- # :query = where
186
- # :unit - [:km, :m, :mi, :ft] - converts :max\_distance to appropriate values and automatically sets :distance\_multiplier. accepts
187
- # :max\_distance - Integer
188
- # :distance\_multiplier - Integer
189
- # :spherical - true - To enable spherical calculations
190
- River.geo_near([-73.99756,40.73083], :max_distance => 4, :unit => :mi, :spherical => true)
191
- ```
181
+ * Bar.where(:location.near => person.house)
182
+
183
+ * near_sphere
184
+ * Bar.where(:location.near_sphere => person.house)
185
+
186
+ * within_box
187
+ * Bar.where(:location.within_box => hood.area)
188
+
189
+ * within_circle
190
+ * Bar.where(:location.within_box => hood.area)
191
+
192
+ * within_circle_sphere
193
+ * Bar.where(:location.within_circle_sphere => hood.area)
194
+
195
+ * within_polygon
196
+ * Bar.where(:location.within_polygon => city.area)
197
+
198
+
199
+ Class Methods
200
+ -------------
201
+
202
+ Some method are added to your class when you define a field as spatial.
203
+
204
+ field :location, type: Point, spatial: true
205
+
206
+
207
+
208
+ Geometry
209
+ --------
210
+
211
+ You can also store Circle, Box, Line (LineString) and Polygons.
212
+ Some helper methods are available to them:
213
+
214
+
215
+ # Returns a geometry bounding box
216
+ # Useful to query #within_box
217
+ polygon.bbox
218
+ polygon.bounding_box
219
+
220
+ # Returns a geometry calculated middle point
221
+ # Useful to query for #near
222
+ polygon.center
223
+
224
+ # Returns middle point + radius
225
+ # Useful to search #within_circle
226
+ polygon.radius(5) # [[1.0, 1.0], 5]
227
+ polygon.radius_sphere(5) # [[1.0, 1.0], 0.00048..]
228
+
229
+
192
230
 
193
231
 
194
232
  Mongo DB 1.9+ New Geo features
@@ -350,6 +388,7 @@ Thanks
350
388
  * Thanks to Kristian Mandrup for creating the base of the gem and a few of the tests
351
389
  * Thanks to CarZen LLC. for letting me release the code we are using
352
390
 
391
+
353
392
  Contributing
354
393
  ------------
355
394
 
@@ -361,8 +400,8 @@ Contributing
361
400
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
362
401
  * 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.
363
402
 
403
+
364
404
  Copyright
365
405
  -----------
366
406
 
367
- Copyright (c) 2011 Ryan Ong. See LICENSE.txt for
368
- further details.
407
+ Copyright (c) 2011 Ryan Ong. See LICENSE.txt for further details.
@@ -4,12 +4,18 @@ Mongoid::Fields.option :spatial do |model,field,options|
4
4
  options = {} unless options.kind_of?(Hash)
5
5
  # x_meth = options[:x] || :x
6
6
  # y_meth = options[:y] || :y
7
- model.class_eval do
8
- (self.spatial_fields ||= []) << field.name.to_sym
9
7
 
10
- define_method "distance_from_#{field.name}" do |*args|
11
- self.distance_from(field.name, *args)
12
- end
8
+ # model.instance_eval do # wont work
9
+ # # define_method "near_#{field.name}" do |*args|
10
+ # # self.where(field.name => args)
11
+ # # end
12
+ # end
13
13
 
14
+ model.class_eval do
15
+ (self.spatial_fields ||= []) << field.name.to_sym
16
+ # define_method "distance_from_#{field.name}" do |*args|
17
+ # self.distance_from(field.name, *args)
18
+ # end
14
19
  end
20
+
15
21
  end
@@ -0,0 +1,7 @@
1
+ module Mongoid
2
+ module Geospatial
3
+ class Box < GeometryField
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ module Mongoid
2
+ module Geospatial
3
+ class Circle < GeometryField
4
+ attr_accessor :center, :radius
5
+
6
+ def point
7
+ Point.new(self[0])
8
+ end
9
+ alias :point :center
10
+
11
+ def radius
12
+ self[1]
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -29,6 +29,16 @@ module Mongoid
29
29
  [center, r.to_f/Mongoid::Geospatial.earth_radius[:km]]
30
30
  end
31
31
 
32
+
33
+ class << self
34
+
35
+ # Database -> Object
36
+ def demongoize(o)
37
+ self.new(o)
38
+ end
39
+
40
+ end
41
+
32
42
  end
33
43
  end
34
44
  end
@@ -0,0 +1,7 @@
1
+ module Mongoid
2
+ module Geospatial
3
+ class Line < GeometryField
4
+
5
+ end
6
+ end
7
+ end
@@ -3,7 +3,7 @@ module Mongoid
3
3
  class Point
4
4
  attr_accessor :x, :y
5
5
 
6
- def initialize(x, y)
6
+ def initialize(x=nil, y=nil)
7
7
  @x, @y = x, y
8
8
  end
9
9
 
@@ -18,12 +18,48 @@ module Mongoid
18
18
  mongoize[args]
19
19
  end
20
20
 
21
+ def to_hsh xl = :x, yl = :y
22
+ {xl => x, yl => y}
23
+ end
24
+ alias :to_hash :to_hsh
25
+
26
+ def radius r = 1
27
+ [mongoize, r]
28
+ end
29
+
30
+ def radius_sphere r = 1, unit = :km
31
+ radius r.to_f/Mongoid::Geospatial.earth_radius[unit]
32
+ end
33
+
34
+ #
35
+ # Distance calculation methods. Thinking about not using it
36
+ # One needs to choose and external lib. GeoRuby or RGeo
37
+ #
38
+ # #Return the distance between the 2D points (ie taking care only of the x and y coordinates), assuming
39
+ # #the points are in projected coordinates. Euclidian distance in whatever unit the x and y ordinates are.
40
+ # def euclidian_distance(point)
41
+ # Math.sqrt((point.x - x)**2 + (point.y - y)**2)
42
+ # end
43
+
44
+ # # Spherical distance in meters, using 'Haversine' formula.
45
+ # # with a radius of 6471000m
46
+ # # Assumes x is the lon and y the lat, in degrees (Changed in version 1.1).
47
+ # # The user has to make sure using this distance makes sense (ie she should be in latlon coordinates)
48
+ # def spherical_distance(point,r=6370997.0)
49
+ # dlat = (point.lat - lat) * DEG2RAD / 2
50
+ # dlon = (point.lon - lon) * DEG2RAD / 2
51
+
52
+ # a = Math.sin(dlat)**2 + Math.cos(lat * DEG2RAD) * Math.cos(point.lat * DEG2RAD) * Math.sin(dlon)**2
53
+ # c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
54
+ # r * c
55
+ # end
56
+
21
57
  class << self
22
58
 
23
59
  # Database -> Object
24
60
  def demongoize(object)
25
61
  # return unless object && !object.empty?
26
- Point.new(object[0], object[1])
62
+ Point.new(*object)
27
63
  end
28
64
 
29
65
  def mongoize(object)
@@ -2,15 +2,6 @@ module Mongoid
2
2
  module Geospatial
3
3
  class Polygon < GeometryField
4
4
 
5
- class << self
6
-
7
- # Database -> Object
8
- def demongoize(o)
9
- Polygon.new(o)
10
- end
11
-
12
- end
13
-
14
5
  end
15
6
  end
16
7
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Geospatial
3
- VERSION = "2.2.0"
3
+ VERSION = "2.3.0"
4
4
  end
5
5
  end
@@ -11,7 +11,7 @@ module Mongoid
11
11
  end
12
12
 
13
13
 
14
- class LineString < Array
14
+ class Line < Array
15
15
  def to_geo
16
16
  GeoRuby::SimpleFeatures::LineString.from_array(self)
17
17
  end
@@ -5,13 +5,25 @@ module Mongoid
5
5
  module Geospatial
6
6
 
7
7
  class Point
8
+ delegate :distance, :to => :to_geo
9
+
8
10
  def to_geo
9
11
  RGeo::Geographic.spherical_factory.point x, y
10
12
  end
13
+
14
+ def self.mongoize(obj)
15
+ case obj
16
+ when RGeo::Geographic::SphericalPointImpl then [obj.x, obj.y]
17
+ when Point then obj.mongoize
18
+ when Array then obj.to_xy
19
+ when Hash then obj.to_xy
20
+ else obj
21
+ end
22
+ end
11
23
  end
12
24
 
13
25
 
14
- class LineString < GeometryField
26
+ class Line < GeometryField
15
27
  def to_geo
16
28
  RGeo::Geographic.spherical_factory.line_string self
17
29
  end
@@ -1,14 +1,13 @@
1
1
  require 'mongoid'
2
2
  require 'active_support/core_ext/string/inflections'
3
3
  require 'active_support/concern'
4
+ require 'mongoid_geospatial/geospatial'
4
5
  require 'mongoid_geospatial/extensions/core_ext'
5
6
  require 'mongoid_geospatial/extensions/rgeo_spherical_point_impl'
6
7
  require 'mongoid_geospatial/field_option'
7
8
 
8
9
  require 'mongoid_geospatial/fields/geometry_field'
9
10
 
10
- %w{point polygon line_string}.each do |type|
11
+ %w{point circle box line polygon}.each do |type|
11
12
  require "mongoid_geospatial/fields/#{type}"
12
13
  end
13
-
14
- require 'mongoid_geospatial/geospatial'
@@ -0,0 +1,9 @@
1
+ class Alarm
2
+ include Mongoid::Document
3
+ include Mongoid::Geospatial
4
+
5
+ field :radius, type: Circle
6
+ field :area, type: Box
7
+
8
+
9
+ end
data/spec/models/bar.rb CHANGED
@@ -2,7 +2,7 @@ class Bar
2
2
  include Mongoid::Document
3
3
  include Mongoid::Geospatial
4
4
 
5
- field :name, :type => String
5
+ field :name, :type => String
6
6
  field :location, :type => Point, :spatial => true
7
7
 
8
8
  has_one :rating, :as => :ratable
data/spec/models/river.rb CHANGED
@@ -5,7 +5,7 @@ class River
5
5
  field :name, type: String
6
6
  field :length, type: Integer
7
7
  field :average_discharge, type: Integer
8
- field :source, type: LineString, spatial: true
8
+ field :source, type: Line, spatial: true
9
9
  # set return_array to true if you do not want a hash returned all the time
10
10
  field :mouth, type: Point, spatial: {lat: 'latitude', lng: 'longitude'}
11
11
  field :mouth_array, type: Array, spatial: {return_array: true}
@@ -3,6 +3,11 @@ require "spec_helper"
3
3
  describe Mongoid::Fields do
4
4
 
5
5
  context "spatial" do
6
+
7
+ it "should set some class methods" do
8
+ Bar.near_location([1,1]).should eq([])
9
+ end
10
+
6
11
  end
7
12
 
8
13
  context "geom" do
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Geospatial::Circle do
4
+
5
+ it "should work" do
6
+ alarm = Alarm.new(radius: [[1,2], 3])
7
+ alarm.radius.should be_a Mongoid::Geospatial::Circle
8
+ end
9
+
10
+
11
+ end
@@ -1,12 +1,12 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Mongoid::Geospatial::LineString do
3
+ describe Mongoid::Geospatial::Line do
4
4
 
5
5
  describe "(de)mongoize" do
6
6
 
7
7
  it "should support a field mapped as linestring" do
8
8
  river = River.new(source: [[5,5],[6,5],[6,6],[5,6]])
9
- river.source.should be_a Mongoid::Geospatial::LineString
9
+ river.source.should be_a Mongoid::Geospatial::Line
10
10
  river.source.should eq([[5,5],[6,5],[6,6],[5,6]])
11
11
  end
12
12
 
@@ -16,22 +16,22 @@ describe Mongoid::Geospatial::LineString do
16
16
  end
17
17
 
18
18
  it "should have a bounding box" do
19
- geom = Mongoid::Geospatial::LineString.new [[1,5],[6,5],[6,6],[5,6]]
19
+ geom = Mongoid::Geospatial::Line.new [[1,5],[6,5],[6,6],[5,6]]
20
20
  geom.bbox.should eq([[1,5], [6,6]])
21
21
  end
22
22
 
23
23
  it "should have a center point" do
24
- geom = Mongoid::Geospatial::LineString.new [[1,1],[1,1],[9,9],[9,9]]
24
+ geom = Mongoid::Geospatial::Line.new [[1,1],[1,1],[9,9],[9,9]]
25
25
  geom.center.should eq([5.5,5.5])
26
26
  end
27
27
 
28
28
  it "should have a radius helper" do
29
- geom = Mongoid::Geospatial::LineString.new [[1,1],[1,1],[9,9],[9,9]]
29
+ geom = Mongoid::Geospatial::Line.new [[1,1],[1,1],[9,9],[9,9]]
30
30
  geom.radius(10).should eq([[5.5,5.5], 10])
31
31
  end
32
32
 
33
33
  it "should have a radius sphere" do
34
- geom = Mongoid::Geospatial::LineString.new [[1,1],[1,1],[9,9],[9,9]]
34
+ geom = Mongoid::Geospatial::Line.new [[1,1],[1,1],[9,9],[9,9]]
35
35
  geom.radius_sphere(10)[1].should be_within(0.001).of(0.001569)
36
36
  end
37
37
 
@@ -7,6 +7,37 @@ describe Mongoid::Geospatial::Point do
7
7
  Bar.count.should eql(1)
8
8
  end
9
9
 
10
+ it "should not fail if point is nil" do
11
+ bar = Bar.create!(name: "Moe's")
12
+ bar.location.x.should be_nil
13
+ end
14
+
15
+ describe "methods" do
16
+
17
+ let(:bar) { Bar.create!(location: [3,2]) }
18
+
19
+ it "should have a .to_a" do
20
+ bar.location.to_a[0..1].should == [3.0, 2.0]
21
+ end
22
+
23
+ it "should have an array [] accessor" do
24
+ bar.location[0].should == 3.0
25
+ end
26
+
27
+ it "should have an ActiveModel symbol accessor" do
28
+ bar[:location].should == [3,2]
29
+ end
30
+
31
+ it "should have a radius helper" do
32
+ bar.location.radius.should eql([[3.0, 2.0], 1])
33
+ end
34
+
35
+ it "should have a radius sphere helper" do
36
+ bar.location.radius_sphere[1].should be_within(0.0001).of(0.00015)
37
+ end
38
+
39
+ end
40
+
10
41
  describe "queryable" do
11
42
 
12
43
  before do
@@ -103,38 +134,6 @@ describe Mongoid::Geospatial::Point do
103
134
  end
104
135
 
105
136
 
106
- describe "methods" do
107
-
108
- it "should have a .to_a" do
109
- bar = Bar.create!(location: [3,2])
110
- bar.location.to_a[0..1].should == [3.0, 2.0]
111
- end
112
-
113
- it "should have an array [] accessor" do
114
- bar = Bar.create!(location: [3,2])
115
- bar.location[0].should == 3.0
116
- end
117
-
118
- it "should have an ActiveModel symbol accessor" do
119
- bar = Bar.create!(location: [3,2])
120
- bar[:location].should == [3,2]
121
- end
122
-
123
- it "should calculate distance between points" do
124
- pending
125
- bar = Bar.create!(location: [5,5])
126
- bar2 = Bar.create!(location: [15,15])
127
- bar.location.distance(bar2.location).should be_within(1).of(1561283.8)
128
- end
129
-
130
- it "should calculate 3d distances by default" do
131
- pending
132
- bar = Bar.create! location: [-73.77694444, 40.63861111 ]
133
- bar2 = Bar.create! location: [-118.40, 33.94] #,:unit=>:mi, :spherical => true)
134
- bar.location.distance(bar2.location).to_i.should be_within(1).of(2469)
135
- end
136
-
137
- end
138
137
 
139
138
  # should raise
140
139
  # geom.to_geo
@@ -6,8 +6,8 @@ describe Mongoid::Geospatial::Polygon do
6
6
 
7
7
  it "should support a field mapped as polygon" do
8
8
  farm = Farm.new(area: [[5,5],[6,5],[6,6],[5,6]])
9
- farm.area.should eq([[5,5],[6,5],[6,6],[5,6]])
10
9
  farm.area.should be_a Mongoid::Geospatial::Polygon
10
+ farm.area.should eq([[5,5],[6,5],[6,6],[5,6]])
11
11
  end
12
12
 
13
13
  it "should store as array on mongo" do
@@ -7,6 +7,12 @@ describe Mongoid::Geospatial::Point do
7
7
  Bar.count.should eql(1)
8
8
  end
9
9
 
10
+ it "should not respond to distance before loading external" do
11
+ bar = Bar.create!(location: [5,5])
12
+ bar.location.should_not respond_to(:distance)
13
+ end
14
+
15
+
10
16
  describe "queryable" do
11
17
 
12
18
  before do
@@ -31,13 +37,18 @@ describe Mongoid::Geospatial::Point do
31
37
  end
32
38
 
33
39
  it "should accept an RGeo object" do
34
- pending
35
40
  point = RGeo::Geographic.spherical_factory.point 1, 2
36
41
  bar = Bar.create!(location: point)
37
42
  bar.location.x.should be_within(0.1).of(1)
38
43
  bar.location.y.should be_within(0.1).of(2)
39
44
  end
40
45
 
46
+ it "should calculate 3d distances by default" do
47
+ bar = Bar.create! location: [-73.77694444, 40.63861111 ]
48
+ bar2 = Bar.create! location: [-118.40, 33.94] #,:unit=>:mi, :spherical => true)
49
+ bar.location.distance(bar2.location.to_geo).to_i.should be_within(1).of(3978262)
50
+ end
51
+
41
52
  end
42
53
  end
43
54
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_geospatial
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -160,8 +160,10 @@ files:
160
160
  - lib/mongoid_geospatial/extensions/core_ext.rb
161
161
  - lib/mongoid_geospatial/extensions/rgeo_spherical_point_impl.rb
162
162
  - lib/mongoid_geospatial/field_option.rb
163
+ - lib/mongoid_geospatial/fields/box.rb
164
+ - lib/mongoid_geospatial/fields/circle.rb
163
165
  - lib/mongoid_geospatial/fields/geometry_field.rb
164
- - lib/mongoid_geospatial/fields/line_string.rb
166
+ - lib/mongoid_geospatial/fields/line.rb
165
167
  - lib/mongoid_geospatial/fields/point.rb
166
168
  - lib/mongoid_geospatial/fields/polygon.rb
167
169
  - lib/mongoid_geospatial/geospatial.rb
@@ -170,6 +172,7 @@ files:
170
172
  - lib/mongoid_geospatial/wrappers/rgeo.rb
171
173
  - mongoid_geospatial.gemspec
172
174
  - spec/models/address.rb
175
+ - spec/models/alarm.rb
173
176
  - spec/models/bar.rb
174
177
  - spec/models/event.rb
175
178
  - spec/models/farm.rb
@@ -178,7 +181,8 @@ files:
178
181
  - spec/models/river.rb
179
182
  - spec/mongoid_geospatial/extensions/core_ext_spec.rb
180
183
  - spec/mongoid_geospatial/field_option_spec.rb
181
- - spec/mongoid_geospatial/fields/line_string_spec.rb
184
+ - spec/mongoid_geospatial/fields/circle_spec.rb
185
+ - spec/mongoid_geospatial/fields/line_spec.rb
182
186
  - spec/mongoid_geospatial/fields/point_spec.rb
183
187
  - spec/mongoid_geospatial/fields/polygon_spec.rb
184
188
  - spec/mongoid_geospatial/geospatial_spec.rb
@@ -214,6 +218,7 @@ summary: A Mongoid Extension that simplifies and adds support for MongoDB Geo Sp
214
218
  Calculations.
215
219
  test_files:
216
220
  - spec/models/address.rb
221
+ - spec/models/alarm.rb
217
222
  - spec/models/bar.rb
218
223
  - spec/models/event.rb
219
224
  - spec/models/farm.rb
@@ -222,7 +227,8 @@ test_files:
222
227
  - spec/models/river.rb
223
228
  - spec/mongoid_geospatial/extensions/core_ext_spec.rb
224
229
  - spec/mongoid_geospatial/field_option_spec.rb
225
- - spec/mongoid_geospatial/fields/line_string_spec.rb
230
+ - spec/mongoid_geospatial/fields/circle_spec.rb
231
+ - spec/mongoid_geospatial/fields/line_spec.rb
226
232
  - spec/mongoid_geospatial/fields/point_spec.rb
227
233
  - spec/mongoid_geospatial/fields/polygon_spec.rb
228
234
  - spec/mongoid_geospatial/geospatial_spec.rb
@@ -1,15 +0,0 @@
1
- module Mongoid
2
- module Geospatial
3
- class LineString < GeometryField
4
-
5
- class << self
6
-
7
- # Database -> Object
8
- def demongoize(o)
9
- LineString.new(o)
10
- end
11
-
12
- end
13
- end
14
- end
15
- end