mongoid_geospatial 2.8.3 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/Guardfile +15 -3
- data/README.md +16 -14
- data/lib/mongoid_geospatial/extensions/georuby_point.rb +12 -0
- data/lib/mongoid_geospatial/extensions/rgeo_spherical_point_impl.rb +8 -1
- data/lib/mongoid_geospatial/fields/point.rb +13 -8
- data/lib/mongoid_geospatial/geospatial.rb +2 -2
- data/lib/mongoid_geospatial/helpers/core.rb +28 -0
- data/lib/mongoid_geospatial/helpers/spatial.rb +1 -0
- data/lib/mongoid_geospatial/version.rb +1 -1
- data/lib/mongoid_geospatial/wrappers/georuby.rb +4 -14
- data/lib/mongoid_geospatial/wrappers/rgeo.rb +7 -14
- data/lib/mongoid_geospatial.rb +1 -1
- data/mongoid_geospatial.gemspec +1 -1
- data/spec/models/alarm.rb +1 -0
- data/spec/models/bus.rb +1 -0
- data/spec/models/person.rb +3 -4
- data/spec/models/river.rb +1 -1
- data/spec/mongoid_geospatial/fields/point_spec.rb +26 -0
- data/spec/mongoid_geospatial/geospatial_spec.rb +2 -2
- data/spec/mongoid_geospatial/helpers/core_spec.rb +28 -0
- data/spec/mongoid_geospatial/helpers/spatial_spec.rb +1 -1
- data/spec/mongoid_geospatial/helpers/sphere_spec.rb +1 -1
- data/spec/mongoid_geospatial/wrappers/georuby_spec.rb +2 -2
- data/spec/mongoid_geospatial/wrappers/rgeo_spec.rb +9 -9
- metadata +8 -7
- data/lib/mongoid_geospatial/extensions/core_ext.rb +0 -29
- data/spec/mongoid_geospatial/extensions/core_ext_spec.rb +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f481eb153c311039d3c9ab431dd181481b51a97b
|
4
|
+
data.tar.gz: 3d1fa8021e6b79c79e85463a468ea865e53cdd99
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc40ac5a0f57a48a1d83c4c940b86ca381252aafa17b2eb90a1943fc27303007230e66a3ff1b98420735dadfc5b58f0d1e2478c58c1e58c7d73b66e3f5a8e433
|
7
|
+
data.tar.gz: d5264c6beec42461c03d803892b2b46270e17e9bdbdd6f9f160cdcabc9cd2230d162bfbf39f59889b4b809e6d52da67247ad7346a1ee12e3ceac874d62cd6dab
|
data/Gemfile
CHANGED
data/Guardfile
CHANGED
@@ -1,9 +1,21 @@
|
|
1
1
|
# A sample Guardfile
|
2
2
|
# More info at https://github.com/guard/guard#readme
|
3
|
-
|
3
|
+
ignore /\/.#.+/
|
4
4
|
|
5
|
-
guard
|
5
|
+
guard :rspec do
|
6
6
|
watch(%r{^spec/.+_spec\.rb$})
|
7
|
-
watch(%r{^lib/(.+)\.rb$}) {
|
7
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
8
8
|
watch('spec/spec_helper.rb') { "spec" }
|
9
9
|
end
|
10
|
+
|
11
|
+
|
12
|
+
# guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
|
13
|
+
# watch('config/application.rb')
|
14
|
+
# watch('config/environment.rb')
|
15
|
+
# watch('config/environments/test.rb')
|
16
|
+
# watch(%r{^config/initializers/.+\.rb$})
|
17
|
+
# watch('Gemfile.lock')
|
18
|
+
# watch('spec/spec_helper.rb') { :rspec }
|
19
|
+
# watch('test/test_helper.rb') { :test_unit }
|
20
|
+
# watch(%r{features/support/}) { :cucumber }
|
21
|
+
# end
|
data/README.md
CHANGED
@@ -19,7 +19,7 @@ Quick Start
|
|
19
19
|
-----------
|
20
20
|
|
21
21
|
This gem focus on (making helpers for) spatial features MongoDB has.
|
22
|
-
You
|
22
|
+
You may also use an external Geometric/Spatial gem alongside.
|
23
23
|
|
24
24
|
# Gemfile
|
25
25
|
gem 'mongoid_geospatial'
|
@@ -47,13 +47,10 @@ You can also use an external Geometric/Spatial alongside.
|
|
47
47
|
For geo points, an extra macro `geo_field` is available
|
48
48
|
|
49
49
|
|
50
|
-
class Place
|
51
|
-
include Mongoid::Document
|
52
|
-
include Mongoid::Geospatial
|
53
|
-
|
54
|
-
# field :location, type: Point, spatial: true
|
55
50
|
geo_field :location
|
56
|
-
|
51
|
+
# Will generate:
|
52
|
+
field :location, type: Point, spatial: true
|
53
|
+
|
57
54
|
|
58
55
|
|
59
56
|
Generate indexes on MongoDB:
|
@@ -104,11 +101,14 @@ Now to access this spatial information we can do this
|
|
104
101
|
|
105
102
|
If you need a hash
|
106
103
|
|
107
|
-
hudson.mouth.to_hsh
|
104
|
+
hudson.mouth.to_hsh # => { x: -74.026667, y: 40.703056 }
|
108
105
|
|
109
106
|
If you are using GeoRuby or RGeo
|
110
107
|
|
111
|
-
hudson.mouth.to_geo
|
108
|
+
hudson.mouth.to_geo # => GeoRuby::Point
|
109
|
+
|
110
|
+
hudson.mouth.to_rgeo # => RGeo::Point
|
111
|
+
|
112
112
|
|
113
113
|
Conventions:
|
114
114
|
|
@@ -205,8 +205,10 @@ Geometry Helpers
|
|
205
205
|
----------------
|
206
206
|
|
207
207
|
We currently support GeoRuby and RGeo.
|
208
|
-
If you require one of those, a #to_geo
|
209
|
-
spatial fields, returning the
|
208
|
+
If you require one of those, a #to_geo and #to_rgeo, respectivelly,
|
209
|
+
method(s) will be available to all spatial fields, returning the
|
210
|
+
external library corresponding object.
|
211
|
+
|
210
212
|
To illustrate:
|
211
213
|
|
212
214
|
class Person
|
@@ -224,7 +226,7 @@ To illustrate:
|
|
224
226
|
|
225
227
|
# Example with RGeo
|
226
228
|
point.class # Mongoid::Geospatial::Point
|
227
|
-
point.
|
229
|
+
point.to_rgeo.class # RGeo::Geographic::SphericalPointImpl
|
228
230
|
|
229
231
|
|
230
232
|
Configure
|
@@ -234,14 +236,14 @@ Assemble it as you need (use a initializer file):
|
|
234
236
|
|
235
237
|
With RGeo
|
236
238
|
|
237
|
-
Mongoid::Geospatial.
|
239
|
+
Mongoid::Geospatial.with_rgeo!
|
238
240
|
# Optional
|
239
241
|
# Mongoid::Geospatial.factory = RGeo::Geographic.spherical_factory
|
240
242
|
|
241
243
|
|
242
244
|
With GeoRuby
|
243
245
|
|
244
|
-
Mongoid::Geospatial.
|
246
|
+
Mongoid::Geospatial.with_georuby!
|
245
247
|
|
246
248
|
|
247
249
|
Defaults (change if you know what you're doing)
|
@@ -2,10 +2,12 @@ module Mongoid
|
|
2
2
|
module Geospatial
|
3
3
|
class Point
|
4
4
|
include Enumerable
|
5
|
-
|
5
|
+
attr_reader :x, :y
|
6
6
|
|
7
|
-
def initialize(x=nil, y=nil)
|
8
|
-
|
7
|
+
def initialize(x = nil, y = nil)
|
8
|
+
return unless x
|
9
|
+
ll = y ? [x, y] : x.split(/,|\s/).reject(&:empty?)
|
10
|
+
@x, @y = ll.map(&:to_f)
|
9
11
|
end
|
10
12
|
|
11
13
|
# Object -> Database
|
@@ -24,6 +26,10 @@ module Mongoid
|
|
24
26
|
yield y
|
25
27
|
end
|
26
28
|
|
29
|
+
def to_s
|
30
|
+
"#{x}, #{y}"
|
31
|
+
end
|
32
|
+
|
27
33
|
def to_hsh xl = :x, yl = :y
|
28
34
|
{xl => x, yl => y}
|
29
35
|
end
|
@@ -64,16 +70,15 @@ module Mongoid
|
|
64
70
|
|
65
71
|
# Database -> Object
|
66
72
|
def demongoize(object)
|
67
|
-
|
68
|
-
Point.new(*object)
|
73
|
+
Point.new(*object) if object
|
69
74
|
end
|
70
75
|
|
71
76
|
def mongoize(object)
|
72
77
|
case object
|
73
78
|
when Point then object.mongoize
|
74
|
-
when Array then object
|
75
|
-
when Hash then object
|
76
|
-
else object
|
79
|
+
when Array then Geospatial.from_array(object)
|
80
|
+
when Hash then Geospatial.from_hash(object)
|
81
|
+
else object.mongoize
|
77
82
|
end
|
78
83
|
end
|
79
84
|
|
@@ -32,11 +32,11 @@ module Mongoid
|
|
32
32
|
self.spatial_fields_indexed = []
|
33
33
|
end
|
34
34
|
|
35
|
-
def self.
|
35
|
+
def self.with_rgeo!
|
36
36
|
require 'mongoid_geospatial/wrappers/rgeo'
|
37
37
|
end
|
38
38
|
|
39
|
-
def self.
|
39
|
+
def self.with_georuby!
|
40
40
|
require 'mongoid_geospatial/wrappers/georuby'
|
41
41
|
end
|
42
42
|
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Geospatial
|
3
|
+
|
4
|
+
def self.from_array(ary)
|
5
|
+
ary[0..1].map(&:to_f)
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.from_hash(hsh)
|
9
|
+
raise "Hash must have at least 2 items" if hsh.size < 2
|
10
|
+
[from_hash_x(hsh), from_hash_y(hsh)]
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.from_hash_y(hsh)
|
14
|
+
v = (Mongoid::Geospatial.lat_symbols & hsh.keys).first
|
15
|
+
return hsh[v].to_f if !v.nil? && hsh[v]
|
16
|
+
fail "Hash must contain #{Mongoid::Geospatial.lat_symbols.inspect} if ruby version is less than 1.9" if RUBY_VERSION.to_f < 1.9
|
17
|
+
fail "Hash cannot contain #{Mongoid::Geospatial.lng_symbols.inspect} as the second item if there is no #{Mongoid::Geospatial.lat_symbols.inspect}" if Mongoid::Geospatial.lng_symbols.index(hsh.keys[1])
|
18
|
+
hsh.values[1].to_f
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.from_hash_x(hsh)
|
22
|
+
v = (Mongoid::Geospatial.lng_symbols & hsh.keys).first
|
23
|
+
return hsh[v].to_f if !v.nil? && hsh[v]
|
24
|
+
fail "Hash cannot contain #{Mongoid::Geospatial.lat_symbols.inspect} as the first item if there is no #{Mongoid::Geospatial.lng_symbols.inspect}" if Mongoid::Geospatial.lat_symbols.index(keys[0])
|
25
|
+
values[0].to_f
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'geo_ruby'
|
2
|
-
|
2
|
+
require 'mongoid_geospatial/extensions/georuby_point'
|
3
3
|
|
4
4
|
module Mongoid
|
5
5
|
module Geospatial
|
@@ -11,26 +11,16 @@ module Mongoid
|
|
11
11
|
GeoRuby::SimpleFeatures::Point.xy(x, y)
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
14
|
+
def geo_distance other
|
15
15
|
to_geo.spherical_distance(other.to_geo)
|
16
16
|
end
|
17
17
|
|
18
|
-
def self.mongoize(obj)
|
19
|
-
case obj
|
20
|
-
when GeoRuby::SimpleFeatures::Point then [obj.x, obj.y]
|
21
|
-
when Point then obj.mongoize
|
22
|
-
when Array then obj.to_xy
|
23
|
-
when Hash then obj.to_xy
|
24
|
-
else obj
|
25
|
-
end
|
26
|
-
end
|
27
18
|
end
|
28
19
|
|
29
|
-
|
30
20
|
class Line < GeometryField
|
31
21
|
|
32
22
|
def to_geo
|
33
|
-
GeoRuby::SimpleFeatures::LineString.from_coordinates(
|
23
|
+
GeoRuby::SimpleFeatures::LineString.from_coordinates(self)
|
34
24
|
end
|
35
25
|
|
36
26
|
end
|
@@ -38,7 +28,7 @@ module Mongoid
|
|
38
28
|
class Polygon < GeometryField
|
39
29
|
|
40
30
|
def to_geo
|
41
|
-
GeoRuby::SimpleFeatures::Polygon.from_coordinates(
|
31
|
+
GeoRuby::SimpleFeatures::Polygon.from_coordinates(self)
|
42
32
|
end
|
43
33
|
|
44
34
|
end
|
@@ -5,23 +5,14 @@ module Mongoid
|
|
5
5
|
module Geospatial
|
6
6
|
|
7
7
|
class Point
|
8
|
-
def
|
8
|
+
def to_rgeo
|
9
9
|
RGeo::Geographic.spherical_factory.point x, y
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
|
12
|
+
def rgeo_distance other
|
13
|
+
to_rgeo.distance other.to_rgeo
|
14
14
|
end
|
15
15
|
|
16
|
-
def self.mongoize(obj)
|
17
|
-
case obj
|
18
|
-
when RGeo::Geographic::SphericalPointImpl then [obj.x, obj.y]
|
19
|
-
when Point then obj.mongoize
|
20
|
-
when Array then obj.to_xy
|
21
|
-
when Hash then obj.to_xy
|
22
|
-
else obj
|
23
|
-
end
|
24
|
-
end
|
25
16
|
end
|
26
17
|
|
27
18
|
class GeometryField
|
@@ -34,17 +25,19 @@ module Mongoid
|
|
34
25
|
end
|
35
26
|
|
36
27
|
class Line < GeometryField
|
37
|
-
def
|
28
|
+
def to_rgeo
|
38
29
|
RGeo::Geographic.spherical_factory.line_string points
|
39
30
|
end
|
40
31
|
|
41
32
|
end
|
42
33
|
|
43
34
|
class Polygon < GeometryField
|
44
|
-
def
|
35
|
+
def to_rgeo
|
45
36
|
ring = RGeo::Geographic.spherical_factory.linear_ring points
|
46
37
|
RGeo::Geographic.spherical_factory.polygon ring
|
47
38
|
end
|
48
39
|
end
|
40
|
+
|
49
41
|
end
|
42
|
+
|
50
43
|
end
|
data/lib/mongoid_geospatial.rb
CHANGED
@@ -2,8 +2,8 @@ require 'mongoid'
|
|
2
2
|
require 'active_support/core_ext/string/inflections'
|
3
3
|
require 'active_support/concern'
|
4
4
|
require 'mongoid_geospatial/geospatial'
|
5
|
-
require 'mongoid_geospatial/extensions/core_ext'
|
6
5
|
require 'mongoid_geospatial/extensions/rgeo_spherical_point_impl'
|
6
|
+
require 'mongoid_geospatial/helpers/core'
|
7
7
|
require 'mongoid_geospatial/helpers/spatial'
|
8
8
|
require 'mongoid_geospatial/helpers/sphere'
|
9
9
|
require 'mongoid_geospatial/helpers/delegate'
|
data/mongoid_geospatial.gemspec
CHANGED
@@ -4,7 +4,7 @@ require File.expand_path('../lib/mongoid_geospatial/version', __FILE__)
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["Ryan Ong", "Marcos Piccinini"]
|
6
6
|
gem.email = ["use@git.hub.com"]
|
7
|
-
gem.description = "Mongoid Extension that simplifies MongoDB
|
7
|
+
gem.description = "Mongoid Extension that simplifies MongoDB casting and operations on spatial Ruby objects."
|
8
8
|
gem.summary = "Mongoid Extension that simplifies MongoDB Geospatial Operations."
|
9
9
|
gem.homepage = "https://github.com/nofxx/mongoid_geospatial"
|
10
10
|
|
data/spec/models/alarm.rb
CHANGED
data/spec/models/bus.rb
CHANGED
data/spec/models/person.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
class Person
|
2
2
|
include Mongoid::Document
|
3
|
-
include Mongoid::MultiParameterAttributes
|
4
3
|
include Mongoid::Timestamps
|
5
|
-
include Mongoid::Versioning
|
4
|
+
# include Mongoid::Versioning
|
6
5
|
include Mongoid::Geospatial
|
7
6
|
|
8
7
|
attr_accessor :mode
|
@@ -22,7 +21,7 @@ class Person
|
|
22
21
|
field :score, type: Integer
|
23
22
|
field :owner_id, type: Integer
|
24
23
|
field :reading, type: Object
|
25
|
-
field :bson_id, type: bson_object_id_class
|
24
|
+
# field :bson_id, type: bson_object_id_class
|
26
25
|
field :employer_id
|
27
26
|
field :security_code
|
28
27
|
field :blood_alcohol_content, type: Float, :default => lambda{ 0.0 }
|
@@ -42,7 +41,7 @@ class Person
|
|
42
41
|
|
43
42
|
attr_reader :rescored
|
44
43
|
|
45
|
-
attr_protected :security_code, :owner_id
|
44
|
+
# attr_protected :security_code, :owner_id
|
46
45
|
|
47
46
|
embeds_many :addresses, :as => :addressable do
|
48
47
|
def extension
|
data/spec/models/river.rb
CHANGED
@@ -4,7 +4,7 @@ class River
|
|
4
4
|
|
5
5
|
field :name, type: String
|
6
6
|
field :length, type: Integer
|
7
|
-
field :
|
7
|
+
field :discharge, type: Integer
|
8
8
|
field :course, 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 :source, type: Point, spatial: true
|
@@ -12,11 +12,37 @@ describe Mongoid::Geospatial::Point do
|
|
12
12
|
bar.location.should be_nil
|
13
13
|
end
|
14
14
|
|
15
|
+
it "should set point methodically" do
|
16
|
+
bar = Bar.create!(name: "Moe's", location: Mongoid::Geospatial::Point.new)
|
17
|
+
bar.location = Mongoid::Geospatial::Point.new(8,8)
|
18
|
+
bar.save.should be_true
|
19
|
+
Bar.first.location.x.should eq(8)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should set point with comma separated text" do
|
23
|
+
bar = Bar.create!(name: "Moe's", location: Mongoid::Geospatial::Point.new)
|
24
|
+
bar.location = Mongoid::Geospatial::Point.new("2.99,3.99")
|
25
|
+
bar.location.mongoize.should eq([2.99, 3.99])
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should set point with space separated text" do
|
29
|
+
bar = Bar.create!(name: "Moe's", location: Mongoid::Geospatial::Point.new)
|
30
|
+
bar.location = Mongoid::Geospatial::Point.new("2.99 3.99")
|
31
|
+
bar.location.mongoize.should eq([2.99, 3.99])
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should set point with space comma separated text" do
|
35
|
+
bar = Bar.create!(name: "Moe's", location: Mongoid::Geospatial::Point.new)
|
36
|
+
bar.location = Mongoid::Geospatial::Point.new("2.99 , 3.99")
|
37
|
+
bar.location.mongoize.should eq([2.99, 3.99])
|
38
|
+
end
|
39
|
+
|
15
40
|
it "should set point to nil" do
|
16
41
|
bar = Bar.create!(name: "Moe's", location: [1, 1])
|
17
42
|
bar.location = nil
|
18
43
|
bar.location.should be_nil
|
19
44
|
bar.save.should be_true
|
45
|
+
Bar.first.location.should be_nil
|
20
46
|
end
|
21
47
|
|
22
48
|
describe "methods" do
|
@@ -20,12 +20,12 @@ describe Mongoid::Geospatial do
|
|
20
20
|
|
21
21
|
it "should create a 2d index" do
|
22
22
|
Bar.create_indexes
|
23
|
-
Bar.
|
23
|
+
Bar.collection.indexes[:location => '2d'].should_not be_nil
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should create a 2dsphere index" do
|
27
27
|
Alarm.create_indexes
|
28
|
-
Alarm.
|
28
|
+
Alarm.collection.indexes[:spot => '2dsphere'].should_not be_nil
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Geospatial
|
3
|
+
|
4
|
+
def self.from_array(ary)
|
5
|
+
ary[0..1].map(&:to_f)
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.from_hash(hsh)
|
9
|
+
raise "Hash must have at least 2 items" if hsh.size < 2
|
10
|
+
[from_hash_x(hsh), from_hash_y(hsh)]
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.from_hash_y(hsh)
|
14
|
+
v = (Mongoid::Geospatial.lat_symbols & hsh.keys).first
|
15
|
+
return hsh[v].to_f if !v.nil? && hsh[v]
|
16
|
+
fail "Hash must contain #{Mongoid::Geospatial.lat_symbols.inspect} if ruby version is less than 1.9" if RUBY_VERSION.to_f < 1.9
|
17
|
+
fail "Hash cannot contain #{Mongoid::Geospatial.lng_symbols.inspect} as the second item if there is no #{Mongoid::Geospatial.lat_symbols.inspect}" if Mongoid::Geospatial.lng_symbols.index(hsh.keys[1])
|
18
|
+
hsh.values[1].to_f
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.from_hash_x(hsh)
|
22
|
+
v = (Mongoid::Geospatial.lng_symbols & hsh.keys).first
|
23
|
+
return hsh[v].to_f if !v.nil? && hsh[v]
|
24
|
+
fail "Hash cannot contain #{Mongoid::Geospatial.lat_symbols.inspect} as the first item if there is no #{Mongoid::Geospatial.lng_symbols.inspect}" if Mongoid::Geospatial.lat_symbols.index(keys[0])
|
25
|
+
values[0].to_f
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -24,7 +24,7 @@ describe Mongoid::Geospatial::Point do
|
|
24
24
|
describe "queryable" do
|
25
25
|
|
26
26
|
before do
|
27
|
-
Mongoid::Geospatial.
|
27
|
+
Mongoid::Geospatial.with_georuby!
|
28
28
|
Place.create_indexes
|
29
29
|
end
|
30
30
|
|
@@ -54,7 +54,7 @@ describe Mongoid::Geospatial::Point do
|
|
54
54
|
it "should calculate 3d distances by default" do
|
55
55
|
bar = Place.create! location: [-73.77694444, 40.63861111 ]
|
56
56
|
bar2 = Place.create! location: [-118.40, 33.94] #,:unit=>:mi, :spherical => true)
|
57
|
-
bar.location.
|
57
|
+
bar.location.geo_distance(bar2.location).to_i.should be_within(1).of(3973808)
|
58
58
|
end
|
59
59
|
|
60
60
|
describe "simple features" do
|
@@ -60,7 +60,7 @@ describe "RGeo Wrapper" do
|
|
60
60
|
describe "queryable" do
|
61
61
|
|
62
62
|
before do
|
63
|
-
Mongoid::Geospatial.
|
63
|
+
Mongoid::Geospatial.with_rgeo!
|
64
64
|
Bar.create_indexes
|
65
65
|
Farm.create_indexes
|
66
66
|
River.create_indexes
|
@@ -72,15 +72,15 @@ describe "RGeo Wrapper" do
|
|
72
72
|
it "should mongoize array" do
|
73
73
|
geom = Bar.new(location: [10, -9]).location
|
74
74
|
geom.class.should eql(Mongoid::Geospatial::Point)
|
75
|
-
geom.
|
75
|
+
geom.to_rgeo.class.should eql(RGeo::Geographic::SphericalPointImpl)
|
76
76
|
geom.x.should be_within(0.1).of(10)
|
77
|
-
geom.
|
77
|
+
geom.to_rgeo.y.should be_within(0.1).of(-9)
|
78
78
|
end
|
79
79
|
|
80
80
|
it "should mongoize hash" do
|
81
81
|
geom = Bar.new(location: {x: 10, y: -9}).location
|
82
82
|
geom.class.should eql(Mongoid::Geospatial::Point)
|
83
|
-
geom.
|
83
|
+
geom.to_rgeo.class.should eql(RGeo::Geographic::SphericalPointImpl)
|
84
84
|
end
|
85
85
|
|
86
86
|
it "should accept an RGeo object" do
|
@@ -93,7 +93,7 @@ describe "RGeo Wrapper" do
|
|
93
93
|
it "should calculate 3d distances by default" do
|
94
94
|
bar = Bar.create! location: [-73.77694444, 40.63861111 ]
|
95
95
|
bar2 = Bar.create! location: [-118.40, 33.94] #,:unit=>:mi, :spherical => true)
|
96
|
-
bar.location.
|
96
|
+
bar.location.rgeo_distance(bar2.location).to_i.should be_within(1).of(3978262)
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -101,8 +101,8 @@ describe "RGeo Wrapper" do
|
|
101
101
|
it "should mongoize array" do
|
102
102
|
geom = Farm.create!(area: [[5,5],[6,5],[6,6],[5,6]]).area
|
103
103
|
geom.class.should eql(Mongoid::Geospatial::Polygon)
|
104
|
-
geom.
|
105
|
-
geom.
|
104
|
+
geom.to_rgeo.class.should eql(RGeo::Geographic::SphericalPolygonImpl)
|
105
|
+
geom.to_rgeo.to_s.should eq "POLYGON ((5.0 5.0, 6.0 5.0, 6.0 6.0, 5.0 6.0, 5.0 5.0))"
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
@@ -110,8 +110,8 @@ describe "RGeo Wrapper" do
|
|
110
110
|
it "should mongoize array" do
|
111
111
|
geom = River.create!(course: [[5,5],[6,5],[6,6],[5,6]]).course
|
112
112
|
geom.class.should eql(Mongoid::Geospatial::Line)
|
113
|
-
geom.
|
114
|
-
geom.
|
113
|
+
geom.to_rgeo.class.should eql(RGeo::Geographic::SphericalLineStringImpl)
|
114
|
+
geom.to_rgeo.to_s.should eq "LINESTRING (5.0 5.0, 6.0 5.0, 6.0 6.0, 5.0 6.0)"
|
115
115
|
end
|
116
116
|
end
|
117
117
|
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:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Ong
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-11-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
@@ -53,8 +53,8 @@ dependencies:
|
|
53
53
|
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '3.2'
|
56
|
-
description: Mongoid Extension that simplifies MongoDB
|
57
|
-
|
56
|
+
description: Mongoid Extension that simplifies MongoDB casting and operations on spatial
|
57
|
+
Ruby objects.
|
58
58
|
email:
|
59
59
|
- use@git.hub.com
|
60
60
|
executables: []
|
@@ -70,7 +70,7 @@ files:
|
|
70
70
|
- README.md
|
71
71
|
- Rakefile
|
72
72
|
- lib/mongoid_geospatial.rb
|
73
|
-
- lib/mongoid_geospatial/extensions/
|
73
|
+
- lib/mongoid_geospatial/extensions/georuby_point.rb
|
74
74
|
- lib/mongoid_geospatial/extensions/rgeo_spherical_point_impl.rb
|
75
75
|
- lib/mongoid_geospatial/fields/box.rb
|
76
76
|
- lib/mongoid_geospatial/fields/circle.rb
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- lib/mongoid_geospatial/fields/point.rb
|
80
80
|
- lib/mongoid_geospatial/fields/polygon.rb
|
81
81
|
- lib/mongoid_geospatial/geospatial.rb
|
82
|
+
- lib/mongoid_geospatial/helpers/core.rb
|
82
83
|
- lib/mongoid_geospatial/helpers/delegate.rb
|
83
84
|
- lib/mongoid_geospatial/helpers/spatial.rb
|
84
85
|
- lib/mongoid_geospatial/helpers/sphere.rb
|
@@ -96,13 +97,13 @@ files:
|
|
96
97
|
- spec/models/phone.rb
|
97
98
|
- spec/models/place.rb
|
98
99
|
- spec/models/river.rb
|
99
|
-
- spec/mongoid_geospatial/extensions/core_ext_spec.rb
|
100
100
|
- spec/mongoid_geospatial/fields/box_spec.rb
|
101
101
|
- spec/mongoid_geospatial/fields/circle_spec.rb
|
102
102
|
- spec/mongoid_geospatial/fields/line_spec.rb
|
103
103
|
- spec/mongoid_geospatial/fields/point_spec.rb
|
104
104
|
- spec/mongoid_geospatial/fields/polygon_spec.rb
|
105
105
|
- spec/mongoid_geospatial/geospatial_spec.rb
|
106
|
+
- spec/mongoid_geospatial/helpers/core_spec.rb
|
106
107
|
- spec/mongoid_geospatial/helpers/delegate_spec.rb
|
107
108
|
- spec/mongoid_geospatial/helpers/spatial_spec.rb
|
108
109
|
- spec/mongoid_geospatial/helpers/sphere_spec.rb
|
@@ -147,13 +148,13 @@ test_files:
|
|
147
148
|
- spec/models/phone.rb
|
148
149
|
- spec/models/place.rb
|
149
150
|
- spec/models/river.rb
|
150
|
-
- spec/mongoid_geospatial/extensions/core_ext_spec.rb
|
151
151
|
- spec/mongoid_geospatial/fields/box_spec.rb
|
152
152
|
- spec/mongoid_geospatial/fields/circle_spec.rb
|
153
153
|
- spec/mongoid_geospatial/fields/line_spec.rb
|
154
154
|
- spec/mongoid_geospatial/fields/point_spec.rb
|
155
155
|
- spec/mongoid_geospatial/fields/polygon_spec.rb
|
156
156
|
- spec/mongoid_geospatial/geospatial_spec.rb
|
157
|
+
- spec/mongoid_geospatial/helpers/core_spec.rb
|
157
158
|
- spec/mongoid_geospatial/helpers/delegate_spec.rb
|
158
159
|
- spec/mongoid_geospatial/helpers/spatial_spec.rb
|
159
160
|
- spec/mongoid_geospatial/helpers/sphere_spec.rb
|
@@ -1,29 +0,0 @@
|
|
1
|
-
class Array
|
2
|
-
def to_xy
|
3
|
-
self[0..1].map(&:to_f)
|
4
|
-
end
|
5
|
-
alias :to_lng_lat :to_xy
|
6
|
-
end
|
7
|
-
|
8
|
-
class Hash
|
9
|
-
def to_xy
|
10
|
-
raise "Hash must have at least 2 items" if self.size < 2
|
11
|
-
[to_x, to_y]
|
12
|
-
end
|
13
|
-
alias :to_lng_lat :to_xy
|
14
|
-
|
15
|
-
def to_y
|
16
|
-
v = (Mongoid::Geospatial.lat_symbols & self.keys).first
|
17
|
-
return self[v].to_f if !v.nil? && self[v]
|
18
|
-
raise "Hash must contain #{Mongoid::Geospatial.lat_symbols.inspect} if ruby version is less than 1.9" if RUBY_VERSION.to_f < 1.9
|
19
|
-
raise "Hash cannot contain #{Mongoid::Geospatial.lng_symbols.inspect} as the second item if there is no #{Mongoid::Geospatial.lat_symbols.inspect}" if Mongoid::Geospatial.lng_symbols.index(self.keys[1])
|
20
|
-
self.values[1].to_f
|
21
|
-
end
|
22
|
-
|
23
|
-
def to_x
|
24
|
-
v = (Mongoid::Geospatial.lng_symbols & self.keys).first
|
25
|
-
return self[v].to_f if !v.nil? && self[v]
|
26
|
-
raise "Hash cannot contain #{Mongoid::Geospatial.lat_symbols.inspect} as the first item if there is no #{Mongoid::Geospatial.lng_symbols.inspect}" if Mongoid::Geospatial.lat_symbols.index(self.keys[0])
|
27
|
-
self.values[0].to_f
|
28
|
-
end
|
29
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe "Core Extensions" do
|
4
|
-
|
5
|
-
describe Array do
|
6
|
-
|
7
|
-
it "should have a #to_xy method" do
|
8
|
-
[1,2,3].to_xy.should eql([1.0, 2.0])
|
9
|
-
end
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
describe Hash do
|
14
|
-
|
15
|
-
it "converts hash with symbol keys x,y to array with x,y" do
|
16
|
-
{ x: 1.1, y: 2.1 }.to_xy.should eql([1.1, 2.1])
|
17
|
-
end
|
18
|
-
|
19
|
-
it "converts hash with symbol keys y,x to array with x,y" do
|
20
|
-
{ y: 2.1, x: 1.1 }.to_xy.should eql([1.1, 2.1])
|
21
|
-
end
|
22
|
-
|
23
|
-
it "converts hash with string keys x,y to array with x,y" do
|
24
|
-
{ 'x' => 1.1, 'y' => 2.1 }.to_xy.should eql([1.1, 2.1])
|
25
|
-
end
|
26
|
-
|
27
|
-
it "converts hash with string keys y,x to array with x,y" do
|
28
|
-
{ 'y' => 2.1, 'x' => 1.1 }.to_xy.should eql([1.1, 2.1])
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
end
|