mongoid_geo 0.5.4.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/Changelog.textile +18 -0
- data/Gemfile +17 -0
- data/README.textile +45 -4
- data/Rakefile +41 -11
- data/VERSION +1 -0
- data/lib/mongoid/geo/index.rb +14 -2
- data/sandbox/haversine.rb +17 -0
- data/sandbox/location.rb +38 -0
- data/sandbox/test.rb +12 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +44 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +26 -0
- data/spec/dummy/config/environments/production.rb +49 -0
- data/spec/dummy/config/environments/test.rb +35 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/mongoid.yml +23 -0
- data/spec/dummy/config/routes.rb +58 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/javascripts/application.js +2 -0
- data/spec/dummy/public/javascripts/controls.js +965 -0
- data/spec/dummy/public/javascripts/dragdrop.js +974 -0
- data/spec/dummy/public/javascripts/effects.js +1123 -0
- data/spec/dummy/public/javascripts/prototype.js +6001 -0
- data/spec/dummy/public/javascripts/rails.js +175 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/integration/navigation_spec.rb +9 -0
- data/spec/models/address.rb +23 -0
- data/spec/models/person.rb +9 -0
- data/spec/mongoid/db_v1_8/geo_near_places_spec.rb +30 -0
- data/spec/mongoid/db_v1_8/geo_near_spec.rb +55 -0
- data/spec/mongoid/db_v1_8/geonear_benchmark_spec.rb +48 -0
- data/spec/mongoid/db_v1_8/spherical_calc_spec.rb +149 -0
- data/spec/mongoid/geo/geo_fields_spec.rb +140 -0
- data/spec/mongoid/geo/geo_inclusions_spec.rb +20 -0
- data/spec/mongoid/geo/geo_inflections_spec.rb +237 -0
- data/spec/mongoid/geo/geo_near_spec.rb +58 -0
- data/spec/mongoid/geo/geo_near_to_model_spec.rb +74 -0
- data/spec/mongoid/geo/geo_spherical_mode_spec.rb +137 -0
- data/spec/mongoid/spec_helper.rb +23 -0
- data/spec/spec_helper.rb +31 -0
- metadata +174 -29
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format nested
|
data/Changelog.textile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
h3. May 19, 2011
|
2
|
+
|
3
|
+
- removed date field from gemspec
|
4
|
+
|
5
|
+
h3. May 17, 2011
|
6
|
+
|
7
|
+
Released version 0.5.4
|
8
|
+
|
9
|
+
- alias methods using underscore '_' for methods such as #near_sphere
|
10
|
+
|
11
|
+
h3. May 10, 2011
|
12
|
+
|
13
|
+
Released version 0.5.3 (improvements by davemitchell)
|
14
|
+
|
15
|
+
- Accept hash with 0, 1 for lat,lng (support auto conversion from a multipart post as an array)
|
16
|
+
- updated fields to support embedded location document
|
17
|
+
- allow lat/lng to be assigned independently when location has not been initialized
|
18
|
+
|
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
|
4
|
+
# gem 'ruby-debug'
|
5
|
+
# gem 'ruby-debug19'
|
6
|
+
|
7
|
+
gem 'mongoid', '> 2'
|
8
|
+
gem "bson", '>= 1.3' # for non jruby apps, require bson_ext in your Gemfile to boost performance
|
9
|
+
gem 'activesupport', '> 3'
|
10
|
+
gem 'hashie', '>= 0.4.0' # https://github.com/okiess/mongo-hashie ???
|
11
|
+
|
12
|
+
group :test, :development do
|
13
|
+
gem 'rspec', '> 2.4'
|
14
|
+
gem 'bundler', '> 1'
|
15
|
+
gem 'jeweler', '> 1.5'
|
16
|
+
gem 'rdoc'
|
17
|
+
end
|
data/README.textile
CHANGED
@@ -7,11 +7,23 @@ A Geo extension for Mongoid.
|
|
7
7
|
* Supports Mongo DB 1.7+ sphere distance calculations
|
8
8
|
* Extra geo calculation methods such as #near_sphere etc.
|
9
9
|
* Add concept of a special "geo" field (for setting the location point)
|
10
|
+
* Configure and create geo indexes
|
10
11
|
* Calculate locations near a given point and return as criteria, sorted by distance
|
11
12
|
|
12
|
-
h2. Status update (May
|
13
|
+
h2. Status update (May 21, 2011)
|
13
14
|
|
14
|
-
Created alias methods such as _#near_sphere_ for _#nearSphere_ etc. to make the DSL much more 'rubyish'!
|
15
|
+
* Created alias methods such as _#near_sphere_ for _#nearSphere_ etc. to make the DSL much more 'rubyish'!
|
16
|
+
* Added #create_index! method (see 'Geo index' section below).
|
17
|
+
|
18
|
+
h2. You need help?
|
19
|
+
|
20
|
+
Post questions in the "mongoid-geo":http://groups.google.com/group/mongoid-geo group. Here I (and other uses of mongoid-geo) will
|
21
|
+
try to help out and answer any questions.
|
22
|
+
|
23
|
+
h2. Suggestions, improvements and bugs?
|
24
|
+
|
25
|
+
Please raise issues or suggestions for improvements in the "Issues" section on github.
|
26
|
+
I would recommend that you try to branch the project, try to fix it yourself and make a pull request.
|
15
27
|
|
16
28
|
h2. Mongoid 2 geo features
|
17
29
|
|
@@ -62,6 +74,23 @@ Usage example:
|
|
62
74
|
|
63
75
|
Note: For embedded documents, you must define the index in the root collection class. (davemitchell)
|
64
76
|
|
77
|
+
Calling geo_index also adds a #create_index! method to the class to enable construction of the index (on the instances/data in the DB).
|
78
|
+
|
79
|
+
<pre>
|
80
|
+
class Address
|
81
|
+
...
|
82
|
+
geo_index :location
|
83
|
+
end
|
84
|
+
|
85
|
+
Address.create_index!
|
86
|
+
</pre>
|
87
|
+
|
88
|
+
I also added a nice little Array macro so you can do this:
|
89
|
+
|
90
|
+
<pre>
|
91
|
+
[Address, Location].create_geo_indexes!
|
92
|
+
</pre>
|
93
|
+
|
65
94
|
h2. Geo option for Array GPS location field
|
66
95
|
|
67
96
|
Objective: When setting a geo GPS location array, the setter should try to convert the value to an array of floats
|
@@ -317,8 +346,8 @@ You can also use a hash to define the circle, with @:center@ and @:radius@ keys
|
|
317
346
|
<pre>
|
318
347
|
places.where(:location.within_center => {:center => [50, 40], :radius => 4})
|
319
348
|
</pre>
|
320
|
-
|
321
|
-
Or use an object (which must have the methods #
|
349
|
+
|
350
|
+
Or use an object (which must have the methods #center and #radius that return the center and radius of the circle))
|
322
351
|
|
323
352
|
<pre>
|
324
353
|
circle = (Struct.new :center, :radius).new
|
@@ -327,3 +356,15 @@ Or use an object (which must have the methods #lower_left and #upper_right that
|
|
327
356
|
|
328
357
|
places.where(:location.within_center => circle)
|
329
358
|
</pre>
|
359
|
+
|
360
|
+
h2. Note on the specs
|
361
|
+
|
362
|
+
The specs still use the old "Javascript" like method convention, such as _#nearSphere_
|
363
|
+
Don't let that fool you ;)
|
364
|
+
|
365
|
+
h2. Contribute
|
366
|
+
|
367
|
+
*Please feel free to contribute to the project!*
|
368
|
+
|
369
|
+
I aim to deliver a complete *geo* package for use with _Mongoid_. This gem should work nicely with _geo_calc_ and _geo_vectors_ that I'm also working on.
|
370
|
+
Please help me with these gems also and help make sure they all play nice and work together!
|
data/Rakefile
CHANGED
@@ -1,25 +1,55 @@
|
|
1
|
-
# encoding:
|
1
|
+
# encoding: utf-8
|
2
|
+
|
2
3
|
require 'rubygems'
|
4
|
+
require 'bundler'
|
3
5
|
begin
|
4
|
-
|
5
|
-
rescue
|
6
|
-
puts
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
7
11
|
end
|
8
|
-
|
9
12
|
require 'rake'
|
10
|
-
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "mongoid_geo"
|
18
|
+
gem.homepage = "http://github.com/kristianmandrup/mongoid_geo"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Mongoid geo extensions with support for native Mongo DB calculations}
|
21
|
+
gem.description = %Q{Makes it easy to use geo calculations with Mongoid}
|
22
|
+
gem.email = "kmandrup@gmail.com"
|
23
|
+
gem.authors = ["Kristian Mandrup"]
|
24
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
25
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
26
|
+
gem.add_runtime_dependency 'mongoid', '> 2'
|
27
|
+
gem.add_runtime_dependency 'bson', '>= 1.3'
|
28
|
+
gem.add_runtime_dependency 'activesupport', '> 3'
|
29
|
+
gem.add_runtime_dependency 'hashie', '>= 0.4.0' # https://github.com/okiess/mongo-hashie ???
|
30
|
+
gem.add_development_dependency 'rspec', '> 2.4'
|
31
|
+
end
|
32
|
+
Jeweler::RubygemsDotOrgTasks.new
|
11
33
|
|
12
34
|
require 'rspec/core'
|
13
35
|
require 'rspec/core/rake_task'
|
36
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
37
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
38
|
+
end
|
14
39
|
|
15
|
-
RSpec::Core::RakeTask.new(:spec
|
40
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
41
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
42
|
+
spec.rcov = true
|
43
|
+
end
|
16
44
|
|
17
45
|
task :default => :spec
|
18
46
|
|
19
|
-
|
47
|
+
require 'rake/rdoctask'
|
48
|
+
Rake::RDocTask.new do |rdoc|
|
49
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
50
|
+
|
20
51
|
rdoc.rdoc_dir = 'rdoc'
|
21
|
-
rdoc.title
|
22
|
-
rdoc.
|
23
|
-
rdoc.rdoc_files.include('README.rdoc')
|
52
|
+
rdoc.title = "mongoid_geo #{version}"
|
53
|
+
rdoc.rdoc_files.include('README*')
|
24
54
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
55
|
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.6.0
|
data/lib/mongoid/geo/index.rb
CHANGED
@@ -1,9 +1,21 @@
|
|
1
1
|
module Mongoid #:nodoc
|
2
2
|
module Indexes #:nodoc
|
3
3
|
module ClassMethods #:nodoc
|
4
|
-
def geo_index name
|
5
|
-
|
4
|
+
def geo_index name, options = {}
|
5
|
+
min = options[:min] || -180
|
6
|
+
max = options[:max] || 180
|
7
|
+
index [[ name, Mongo::GEO2D ]], :min => min, :max => max
|
8
|
+
|
9
|
+
define_singleton_method :create_index! do
|
10
|
+
collection.create_index([[name, Mongo::GEO2D]], :min => min, :max => max)
|
11
|
+
end
|
6
12
|
end
|
7
13
|
end
|
8
14
|
end
|
9
15
|
end
|
16
|
+
|
17
|
+
class Array
|
18
|
+
def create_geo_indexes!
|
19
|
+
self.each {|clazz| clazz.create_index! }
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class Haversine
|
2
|
+
#EARTH_RADIUS = 3963.19 # miles
|
3
|
+
EARTH_RADIUS = 6371 # kilometers
|
4
|
+
RADIAN_PER_DEGREE = Math::PI / 180.0
|
5
|
+
|
6
|
+
def self.distance(lat1, lng1, lat2, lng2)
|
7
|
+
lat1_radians = lat1 * RADIAN_PER_DEGREE
|
8
|
+
lat2_radians = lat2 * RADIAN_PER_DEGREE
|
9
|
+
|
10
|
+
distance_lat = (lat2-lat1) * RADIAN_PER_DEGREE
|
11
|
+
distance_lng = (lng2-lng1) * RADIAN_PER_DEGREE
|
12
|
+
|
13
|
+
a = Math.sin(distance_lat/2)**2 + Math.cos(lat1_radians) * Math.cos(lat2_radians) * Math.sin(distance_lng/2) ** 2
|
14
|
+
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
|
15
|
+
EARTH_RADIUS * c
|
16
|
+
end
|
17
|
+
end
|
data/sandbox/location.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
require 'mongoid_geo'
|
3
|
+
require 'net/http'
|
4
|
+
require 'active_support'
|
5
|
+
require 'haversine'
|
6
|
+
require 'rack'
|
7
|
+
require 'hashie'
|
8
|
+
|
9
|
+
Mongoid.configure.master = Mongo::Connection.new.db('mongoid-geo')
|
10
|
+
|
11
|
+
Mongoid.database.collections.each do |coll|
|
12
|
+
coll.remove
|
13
|
+
end
|
14
|
+
|
15
|
+
class Location
|
16
|
+
include Mongoid::Document
|
17
|
+
|
18
|
+
field :lon_lat, :type => Array
|
19
|
+
|
20
|
+
extend Mongoid::Geo::Near
|
21
|
+
|
22
|
+
geo_index :lon_lat, :create
|
23
|
+
|
24
|
+
def self.search(loc)
|
25
|
+
country = "The Netherlands"
|
26
|
+
location = [loc, country].compact.join(', ')
|
27
|
+
response = ::Net::HTTP.get_response(URI.parse("http://maps.googleapis.com/maps/api/geocode/json?address=#{Rack::Utils.escape(location)}&sensor=false"))
|
28
|
+
json = ActiveSupport::JSON.decode(response.body)
|
29
|
+
|
30
|
+
if json["status"] == "OK"
|
31
|
+
lon_lat = json["results"][0]["geometry"]["location"]["lng"], json["results"][0]["geometry"]["location"]["lat"]
|
32
|
+
else
|
33
|
+
return false
|
34
|
+
end
|
35
|
+
geoNear(lon_lat, :lon_lat)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
data/sandbox/test.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'location'
|
4
|
+
|
5
|
+
Location.create(:lon_lat => [45, 11])
|
6
|
+
Location.create(:lon_lat => [46, 12])
|
7
|
+
|
8
|
+
# Location.collection.create_index([['lon_lat', Mongo::GEO2D]], :min => -180, :max => 180)
|
9
|
+
|
10
|
+
Location.create_index!
|
11
|
+
|
12
|
+
puts Location.search('Amsterdam')
|
data/spec/dummy/Rakefile
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
2
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
3
|
+
|
4
|
+
require File.expand_path('../config/application', __FILE__)
|
5
|
+
require 'rake'
|
6
|
+
|
7
|
+
Dummy::Application.load_tasks
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
require "active_model/railtie"
|
4
|
+
require "action_controller/railtie"
|
5
|
+
require "action_view/railtie"
|
6
|
+
require "action_mailer/railtie"
|
7
|
+
|
8
|
+
Bundler.require
|
9
|
+
require "mongoid_geo"
|
10
|
+
|
11
|
+
module Dummy
|
12
|
+
class Application < Rails::Application
|
13
|
+
# Settings in config/environments/* take precedence over those specified here.
|
14
|
+
# Application configuration should go into files in config/initializers
|
15
|
+
# -- all .rb files in that directory are automatically loaded.
|
16
|
+
|
17
|
+
# Custom directories with classes and modules you want to be autoloadable.
|
18
|
+
# config.autoload_paths += %W(#{config.root}/extras)
|
19
|
+
|
20
|
+
# Only load the plugins named here, in the order given (default is alphabetical).
|
21
|
+
# :all can be used as a placeholder for all plugins not explicitly named.
|
22
|
+
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
23
|
+
|
24
|
+
# Activate observers that should always be running.
|
25
|
+
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
|
26
|
+
|
27
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
28
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
29
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
30
|
+
|
31
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
32
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
33
|
+
# config.i18n.default_locale = :de
|
34
|
+
|
35
|
+
# JavaScript files you want as :defaults (application.js is always included).
|
36
|
+
# config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
|
37
|
+
|
38
|
+
# Configure the default encoding used in templates for Ruby 1.9.
|
39
|
+
config.encoding = "utf-8"
|
40
|
+
|
41
|
+
# Configure sensitive parameters which will be filtered from the log file.
|
42
|
+
config.filter_parameters += [:password]
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Dummy::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# In the development environment your application's code is reloaded on
|
5
|
+
# every request. This slows down response time but is perfect for development
|
6
|
+
# since you don't have to restart the webserver when you make code changes.
|
7
|
+
config.cache_classes = false
|
8
|
+
|
9
|
+
# Log error messages when you accidentally call methods on nil.
|
10
|
+
config.whiny_nils = true
|
11
|
+
|
12
|
+
# Show full error reports and disable caching
|
13
|
+
config.consider_all_requests_local = true
|
14
|
+
config.action_view.debug_rjs = true
|
15
|
+
config.action_controller.perform_caching = false
|
16
|
+
|
17
|
+
# Don't care if the mailer can't send
|
18
|
+
config.action_mailer.raise_delivery_errors = false
|
19
|
+
|
20
|
+
# Print deprecation notices to the Rails logger
|
21
|
+
config.active_support.deprecation = :log
|
22
|
+
|
23
|
+
# Only use best-standards-support built into browsers
|
24
|
+
config.action_dispatch.best_standards_support = :builtin
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
Dummy::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# The production environment is meant for finished, "live" apps.
|
5
|
+
# Code is not reloaded between requests
|
6
|
+
config.cache_classes = true
|
7
|
+
|
8
|
+
# Full error reports are disabled and caching is turned on
|
9
|
+
config.consider_all_requests_local = false
|
10
|
+
config.action_controller.perform_caching = true
|
11
|
+
|
12
|
+
# Specifies the header that your server uses for sending files
|
13
|
+
config.action_dispatch.x_sendfile_header = "X-Sendfile"
|
14
|
+
|
15
|
+
# For nginx:
|
16
|
+
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
|
17
|
+
|
18
|
+
# If you have no front-end server that supports something like X-Sendfile,
|
19
|
+
# just comment this out and Rails will serve the files
|
20
|
+
|
21
|
+
# See everything in the log (default is :info)
|
22
|
+
# config.log_level = :debug
|
23
|
+
|
24
|
+
# Use a different logger for distributed setups
|
25
|
+
# config.logger = SyslogLogger.new
|
26
|
+
|
27
|
+
# Use a different cache store in production
|
28
|
+
# config.cache_store = :mem_cache_store
|
29
|
+
|
30
|
+
# Disable Rails's static asset server
|
31
|
+
# In production, Apache or nginx will already do this
|
32
|
+
config.serve_static_assets = false
|
33
|
+
|
34
|
+
# Enable serving of images, stylesheets, and javascripts from an asset server
|
35
|
+
# config.action_controller.asset_host = "http://assets.example.com"
|
36
|
+
|
37
|
+
# Disable delivery errors, bad email addresses will be ignored
|
38
|
+
# config.action_mailer.raise_delivery_errors = false
|
39
|
+
|
40
|
+
# Enable threaded mode
|
41
|
+
# config.threadsafe!
|
42
|
+
|
43
|
+
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
|
44
|
+
# the I18n.default_locale when a translation can not be found)
|
45
|
+
config.i18n.fallbacks = true
|
46
|
+
|
47
|
+
# Send deprecation notices to registered listeners
|
48
|
+
config.active_support.deprecation = :notify
|
49
|
+
end
|