Skip to content

Commit

Permalink
some more fields and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nofxx committed Sep 13, 2012
1 parent 95f63c4 commit 2717d88
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 49 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -115,6 +115,8 @@ class River
end
```

Use
---

Generate indexes on MongoDB:

Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid_geospatial.rb
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions lib/mongoid_geospatial/fields/box.rb
@@ -0,0 +1,7 @@
module Mongoid
module Geospatial
class Box < GeometryField

end
end
end
17 changes: 17 additions & 0 deletions lib/mongoid_geospatial/fields/circle.rb
@@ -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
10 changes: 10 additions & 0 deletions lib/mongoid_geospatial/fields/geometry_field.rb
Expand Up @@ -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
7 changes: 7 additions & 0 deletions lib/mongoid_geospatial/fields/line.rb
@@ -0,0 +1,7 @@
module Mongoid
module Geospatial
class Line < GeometryField

end
end
end
15 changes: 0 additions & 15 deletions lib/mongoid_geospatial/fields/line_string.rb

This file was deleted.

9 changes: 0 additions & 9 deletions lib/mongoid_geospatial/fields/polygon.rb
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/mongoid_geospatial/wrappers/georuby.rb
Expand Up @@ -11,7 +11,7 @@ def to_geo
end


class LineString < Array
class Line < Array
def to_geo
GeoRuby::SimpleFeatures::LineString.from_array(self)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid_geospatial/wrappers/rgeo.rb
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/models/alarm.rb
@@ -0,0 +1,9 @@
class Alarm
include Mongoid::Document
include Mongoid::Geospatial

field :radius, type: Circle
field :area, type: Box


end
2 changes: 1 addition & 1 deletion spec/models/river.rb
Expand Up @@ -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}
Expand Down
11 changes: 11 additions & 0 deletions spec/mongoid_geospatial/fields/circle_spec.rb
@@ -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

Expand All @@ -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

Expand Down
13 changes: 2 additions & 11 deletions spec/mongoid_geospatial/fields/point_spec.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/mongoid_geospatial/fields/polygon_spec.rb
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion spec/mongoid_geospatial/wrappers/rgeo_spec.rb
Expand Up @@ -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.