Permalink
Browse files

some more fields and fixes

  • Loading branch information...
1 parent 95f63c4 commit 2717d88b5d975e9e2eb1ecded4161d4feabbb77b @nofxx nofxx committed Sep 13, 2012
View
@@ -26,7 +26,7 @@ You can also use an external Geometric/Spatial alongside.
gem 'mongoid_geospatial'
- # A place to illustrate Point, LineString and Polygon
+ # A place to illustrate Point, Line and Polygon
class Place
include Mongoid::Document
include Mongoid::Geospatial
@@ -90,7 +90,7 @@ Defaults (change if you know what you're doing)
Model Setup
-----------
-You can create Point, LineString and Polygon on your models:
+You can create Point, Line and Polygon on your models:
```ruby
@@ -115,6 +115,8 @@ class River
end
```
+Use
+---
Generate indexes on MongoDB:
@@ -7,7 +7,7 @@
require 'mongoid_geospatial/fields/geometry_field'
-%w{point polygon line_string}.each do |type|
+%w{point circle box line polygon}.each do |type|
require "mongoid_geospatial/fields/#{type}"
end
@@ -0,0 +1,7 @@
+module Mongoid
+ module Geospatial
+ class Box < GeometryField
+
+ end
+ end
+end
@@ -0,0 +1,17 @@
+module Mongoid
+ module Geospatial
+ class Circle < GeometryField
+ attr_accessor :center, :radius
+
+ def point
+ Point.new(self[0])
+ end
+ alias :point :center
+
+ def radius
+ self[1]
+ end
+
+ end
+ end
+end
@@ -29,6 +29,16 @@ def radius_sphere r = 1
[center, r.to_f/Mongoid::Geospatial.earth_radius[:km]]
end
+
+ class << self
+
+ # Database -> Object
+ def demongoize(o)
+ self.new(o)
+ end
+
+ end
+
end
end
end
@@ -0,0 +1,7 @@
+module Mongoid
+ module Geospatial
+ class Line < GeometryField
+
+ end
+ end
+end
@@ -1,15 +0,0 @@
-module Mongoid
- module Geospatial
- class LineString < GeometryField
-
- class << self
-
- # Database -> Object
- def demongoize(o)
- LineString.new(o)
- end
-
- end
- end
- end
-end
@@ -2,15 +2,6 @@ module Mongoid
module Geospatial
class Polygon < GeometryField
- class << self
-
- # Database -> Object
- def demongoize(o)
- Polygon.new(o)
- end
-
- end
-
end
end
end
@@ -11,7 +11,7 @@ def to_geo
end
- class LineString < Array
+ class Line < Array
def to_geo
GeoRuby::SimpleFeatures::LineString.from_array(self)
end
@@ -11,7 +11,7 @@ def to_geo
end
- class LineString < GeometryField
+ class Line < GeometryField
def to_geo
RGeo::Geographic.spherical_factory.line_string self
end
View
@@ -0,0 +1,9 @@
+class Alarm
+ include Mongoid::Document
+ include Mongoid::Geospatial
+
+ field :radius, type: Circle
+ field :area, type: Box
+
+
+end
View
@@ -5,7 +5,7 @@ class River
field :name, type: String
field :length, type: Integer
field :average_discharge, type: Integer
- field :source, type: LineString, spatial: true
+ field :source, type: Line, spatial: true
# set return_array to true if you do not want a hash returned all the time
field :mouth, type: Point, spatial: {lat: 'latitude', lng: 'longitude'}
field :mouth_array, type: Array, spatial: {return_array: true}
@@ -0,0 +1,11 @@
+require "spec_helper"
+
+describe Mongoid::Geospatial::Circle do
+
+ it "should work" do
+ alarm = Alarm.new(radius: [[1,2], 3])
+ alarm.radius.should be_a Mongoid::Geospatial::Circle
+ end
+
+
+end
@@ -1,12 +1,12 @@
require "spec_helper"
-describe Mongoid::Geospatial::LineString do
+describe Mongoid::Geospatial::Line do
describe "(de)mongoize" do
it "should support a field mapped as linestring" do
river = River.new(source: [[5,5],[6,5],[6,6],[5,6]])
- river.source.should be_a Mongoid::Geospatial::LineString
+ river.source.should be_a Mongoid::Geospatial::Line
river.source.should eq([[5,5],[6,5],[6,6],[5,6]])
end
@@ -16,22 +16,22 @@
end
it "should have a bounding box" do
- geom = Mongoid::Geospatial::LineString.new [[1,5],[6,5],[6,6],[5,6]]
+ geom = Mongoid::Geospatial::Line.new [[1,5],[6,5],[6,6],[5,6]]
geom.bbox.should eq([[1,5], [6,6]])
end
it "should have a center point" do
- geom = Mongoid::Geospatial::LineString.new [[1,1],[1,1],[9,9],[9,9]]
+ geom = Mongoid::Geospatial::Line.new [[1,1],[1,1],[9,9],[9,9]]
geom.center.should eq([5.5,5.5])
end
it "should have a radius helper" do
- geom = Mongoid::Geospatial::LineString.new [[1,1],[1,1],[9,9],[9,9]]
+ geom = Mongoid::Geospatial::Line.new [[1,1],[1,1],[9,9],[9,9]]
geom.radius(10).should eq([[5.5,5.5], 10])
end
it "should have a radius sphere" do
- geom = Mongoid::Geospatial::LineString.new [[1,1],[1,1],[9,9],[9,9]]
+ geom = Mongoid::Geospatial::Line.new [[1,1],[1,1],[9,9],[9,9]]
geom.radius_sphere(10)[1].should be_within(0.001).of(0.001569)
end
@@ -120,18 +120,9 @@
bar[:location].should == [3,2]
end
- it "should calculate distance between points" do
- pending
+ it "should not calculate distance between points w/o external" do
bar = Bar.create!(location: [5,5])
- bar2 = Bar.create!(location: [15,15])
- bar.location.distance(bar2.location).should be_within(1).of(1561283.8)
- end
-
- it "should calculate 3d distances by default" do
- pending
- bar = Bar.create! location: [-73.77694444, 40.63861111 ]
- bar2 = Bar.create! location: [-118.40, 33.94] #,:unit=>:mi, :spherical => true)
- bar.location.distance(bar2.location).to_i.should be_within(1).of(2469)
+ bar.location.should_not respond_to(:distance)
end
end
@@ -6,8 +6,8 @@
it "should support a field mapped as polygon" do
farm = Farm.new(area: [[5,5],[6,5],[6,6],[5,6]])
- farm.area.should eq([[5,5],[6,5],[6,6],[5,6]])
farm.area.should be_a Mongoid::Geospatial::Polygon
+ farm.area.should eq([[5,5],[6,5],[6,6],[5,6]])
end
it "should store as array on mongo" do
@@ -31,13 +31,18 @@
end
it "should accept an RGeo object" do
- pending
point = RGeo::Geographic.spherical_factory.point 1, 2
bar = Bar.create!(location: point)
bar.location.x.should be_within(0.1).of(1)
bar.location.y.should be_within(0.1).of(2)
end
+ it "should calculate 3d distances by default" do
+ bar = Bar.create! location: [-73.77694444, 40.63861111 ]
+ bar2 = Bar.create! location: [-118.40, 33.94] #,:unit=>:mi, :spherical => true)
+ bar.location.distance(bar2.location).to_i.should be_within(1).of(2469)
+ end
+
end
end
end

0 comments on commit 2717d88

Please sign in to comment.