Browse files

clean up

  • Loading branch information...
1 parent be89469 commit 2e3c5e3cfa452cb2a663c7137af536e8db521f50 @nofxx nofxx committed Sep 12, 2012
View
141 README.md
@@ -21,45 +21,71 @@ Quick Start
This gem focus on (making helpers for) spatial features MongoDB has.
You can also use an external Geometric/Spatial alongside.
+
+ # Gemfile
+ gem 'mongoid_geospatial'
+
+
+ # A place to illustrate Point, LineString and Polygon
+ class Place
+ include Mongoid::Document
+ include Mongoid::Geospatial
+
+ field :name, type: String
+ field :location, type: Point, :spatial => true
+ field :route, type: Linestring
+ field :area, type: Polygon
+ end
+
+
+Geometry Helpers
+----------------
+
We currently support GeoRuby and RGeo.
If you require one of those, a #to_geo method will be available to all
spatial fields, returning the external library corresponding object.
To illustrate:
-```ruby
-point = Mongoid::Geospatial::Point.new(1,1)
-# Example with GeoRuby
-point.class # Mongoid::Geospatial::Point
-point.to_geo.class # GeoRuby::SimpleFeatures::Point
-# Example with RGeo
-point.class # Mongoid::Geospatial::Point
-point.to_geo.class # RGeo::Geographic::SphericalPointImpl
-```
+ class Person
+ include Mongoid::Document
+ include Mongoid::Geospatial
-Require it as you need:
+ field :location, type: Point
+ end
-```ruby
-# No External Support
-gem 'mongoid_geospatial'
+ me = Person.new(location: [8, 8])
-# Require with GeoRuby
-gem 'mongoid_geospatial', :require => 'mongoid_geospatial_georuby'
+ # Example with GeoRuby
+ point.class # Mongoid::Geospatial::Point
+ point.to_geo.class # GeoRuby::SimpleFeatures::Point
-# Require with RGeo
-gem 'mongoid_geospatial', :require => 'mongoid_geospatial_rgeo'
+ # Example with RGeo
+ point.class # Mongoid::Geospatial::Point
+ point.to_geo.class # RGeo::Geographic::SphericalPointImpl
-```
-Options (change if you know what you're doing)
+Configure
+----------------
+
+Assemble it as you need:
+
+With RGeo
+
+ Mongoid::Geospatial.use_rgeo
+ # Optional
+ # Mongoid::Geospatial.factory = RGeo::Geographic.spherical_factory
+
+With GeoRuby
+
+ Mongoid::Geospatial.use_georuby
+
+Defaults (change if you know what you're doing)
+
+ Mongoid::Geospatial.lng_symbol = :x
+ Mongoid::Geospatial.lat_symbol = :y
+ Mongoid::Geospatial.earth_radius = EARTH_RADIUS
-```ruby
-# Mongoid::Geospatial.lng_symbol = :x
-# Mongoid::Geospatial.lat_symbol = :y
-# Mongoid::Geospatial.earth_radius = EARTH_RADIUS
-# Only for RGeo:
-# Mongoid::Geospatial.geo_factory = RGeo::Geographic.spherical_factory
-```
Model Setup
-----------
@@ -89,12 +115,6 @@ class River
end
```
-Avaiable data types:
-
-* Point
-* LineString
-* Polygon
-
Generate indexes on MongoDB:
@@ -170,47 +190,6 @@ River.where(:name=>'hudson').geo_near({:lat => 40.73083, :lng => -73.99756})
River.geo_near([-73.99756,40.73083], :max_distance => 4, :unit => :mi, :spherical => true)
```
-There are two types of pagination!
-
-* MongoDB Pagination - Stores start of rows to page limit in memory
-* Post Query Pagination - Stores all rows in memory
-
-Post-Result is only minutely slower than MongoDB because MongoDB has to calculate distance for all of the rows anyway. The slow up is in the transfer of data from the database to ruby.
-
-Post-Result has some advantages that are listed below.
-
-```ruby
-# MongoDB pagination
-# overwrites #skip chain method
-# :page - pagination will be enabled if set to any variable including nil, pagination will not be enabled if either :per\_page or :paginator is set
-# :per\_page
-# :paginator - Choose which paginator to use. [default :arrary]
-# Prefered method to set is Mongoid::Geospatial.paginator=:array
-# Available Paginators [:kaminari, :will\_paginate, :array]
-# The only thing this does really is configure default per\_page so it is only kind of useful
-River.geo_near([-73.99756,40.73083], :page => 1)
-```
-
-```ruby
-# Post Query Pagination
-# At carzen we use Post Query Pagination because we need to re-sort our rows after fetching. Pagination is not friendly with re-sorting.
-# You can jump pages continously without querying the database again.
-# listens to #limit/:num & #skip before geo\_near
-# #page(page\_number, opts = {})
-# opts:
-# :per\_page
-# :paginator
-# #per(per\_page\_number, opts = {})
-# opts:
-# :page
-# :paginator
-#
-# both return a GeoNearResults, which is really just a modified Array
-# #per really just #page but just moves the options around
-rivers = River.geo_near([-73.99756,40.73083]).sort_by!{|r| r.geo[:distance] * r.multiplier }
-rivers = rivers.per(25).page(1)
-rivers.reset! # resets the object to it is original state right after query.
-```
Mongo DB 1.9+ New Geo features
---------
@@ -330,8 +309,7 @@ This Fork
---------
This fork is not backwards compatible with 'mongoid_spatial'.
-This fork delegates all the calculation to the nice RGeo.
-As a result, all the GEOS/Proj features are available in Ruby/Mongoid.
+This fork delegates calculations to the external libs and use Moped.
Change in your models:
@@ -361,16 +339,20 @@ Troubleshooting
**Mongo::OperationFailure: can't find special index: 2d**
-Indexes need to be created. Execute command: <code>rake db:mongoid:create_indexes</code>
+Indexes need to be created. Execute command:
+
+ rake db:mongoid:create_indexes
Thanks
------------
+------
+
* Thanks to Kristian Mandrup for creating the base of the gem and a few of the tests
* Thanks to CarZen LLC. for letting me release the code we are using
-Contributing to mongoid_spatial
------------
+Contributing
+------------
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
* Fork the project
@@ -381,5 +363,6 @@ Contributing to mongoid_spatial
Copyright
-----------
+
Copyright (c) 2011 Ryan Ong. See LICENSE.txt for
further details.
View
7 lib/mongoid_geospatial/field_option.rb
@@ -2,11 +2,10 @@
Mongoid::Fields.option :spatial do |model,field,options|
options = {} unless options.kind_of?(Hash)
- lat_meth = options[:lat] || :lat
- lng_meth = options[:lng] || :lng
+ # x_meth = options[:x] || :x
+ # y_meth = options[:y] || :y
model.class_eval do
- self.spatial_fields ||= []
- self.spatial_fields << field.name.to_sym if self.spatial_fields.kind_of? Array
+ (self.spatial_fields ||= []) << field.name.to_sym
define_method "distance_from_#{field.name}" do |*args|
self.distance_from(field.name, *args)
View
20 lib/mongoid_geospatial/geospatial.rb
@@ -1,6 +1,3 @@
-require 'mongoid_geospatial/geospatial/core_ext'
-require 'mongoid_geospatial/geospatial/geo_near_results'
-
module Mongoid
module Geospatial
extend ActiveSupport::Concern
@@ -9,29 +6,24 @@ module Geospatial
LAT_SYMBOLS = [:y, :lat, :latitude]
EARTH_RADIUS_KM = 6371 # taken directly from mongodb
- RAD_PER_DEG = Math::PI/180
+ RAD_PER_DEG = Math::PI / 180
EARTH_RADIUS = {
:km => EARTH_RADIUS_KM,
- :m => EARTH_RADIUS_KM*1000,
- :mi => EARTH_RADIUS_KM*0.621371192, # taken directly from mongodb
- :ft => EARTH_RADIUS_KM*5280*0.621371192,
- :sm => EARTH_RADIUS_KM*0.53995680345572 # sea mile
+ :m => EARTH_RADIUS_KM * 1000,
+ :mi => EARTH_RADIUS_KM * 0.621371192, # taken directly from mongodb
+ :ft => EARTH_RADIUS_KM * 5280*0.621371192,
+ :sm => EARTH_RADIUS_KM * 0.53995680345572 # sea mile
}
mattr_accessor :lng_symbols
mattr_accessor :lat_symbols
mattr_accessor :earth_radius
- mattr_accessor :geo_factory
-
- mattr_accessor :paginator
- mattr_accessor :default_per_page
+ mattr_accessor :factory
@@lng_symbols = LNG_SYMBOLS.dup
@@lat_symbols = LAT_SYMBOLS.dup
@@earth_radius = EARTH_RADIUS.dup
- @@paginator = :array
- @@default_per_page = 25
included do
attr_accessor :geo
View
11 spec/mongoid_geospatial/field_option_spec.rb
@@ -0,0 +1,11 @@
+require "spec_helper"
+
+describe Mongoid::Fields do
+
+ context "spatial" do
+ end
+
+ context "geom" do
+ end
+
+end
View
20 spec/mongoid_geospatial/fields/line_string_spec.rb
@@ -15,8 +15,24 @@
River.first.source.should eq([[5,5],[6,5],[6,6],[5,6]])
end
- describe "with rgeo" do
- # farm.area.should be_a RGeo::Geographic::SphericalPolygonImpl
+ it "should have a bounding box" do
+ geom = Mongoid::Geospatial::LineString.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.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.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.radius_sphere(10)[1].should be_within(0.001).of(0.001569)
end
end
View
6 spec/mongoid_geospatial/fields/polygon_spec.rb
@@ -30,6 +30,12 @@
geom.radius(10).should eq([[5.5,5.5], 10])
end
+ it "should have a radius sphere" do
+ geom = Mongoid::Geospatial::Polygon.new [[1,1],[1,1],[9,9],[9,9]]
+ geom.radius_sphere(10)[1].should be_within(0.001).of(0.001569)
+ end
+
+
describe "with rgeo" do
# farm.area.should be_a RGeo::Geographic::SphericalPolygonImpl
end

0 comments on commit 2e3c5e3

Please sign in to comment.