active_record_nearest_neighbor 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +21 -0
- data/lib/active_record_nearest_neighbor.rb +36 -0
- data/lib/active_record_nearest_neighbor/close_to.rb +36 -0
- data/lib/active_record_nearest_neighbor/railtie.rb +17 -0
- data/lib/active_record_nearest_neighbor/scopes.rb +51 -0
- data/lib/active_record_nearest_neighbor/version.rb +3 -0
- data/lib/tasks/active_record_nearest_neighbor_tasks.rake +48 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4a6cebd11a459d5e5ac39cd785dad61430819e4a
|
4
|
+
data.tar.gz: de4ed564bed54ed3d4c9cb02db44fddc6eb9a70b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 53bf4f5226b2459420157f9aabbd59bf251c1f23d1c13c1efdbb98e8915ba5db9c8c9de7c9ddc103eced0339628364040915b0b1e152682ebd903ce51b0c1c91
|
7
|
+
data.tar.gz: 95de3884ee094bf09565b180c9ccb52b7dbaf34666473ce7692d420b59a834388d5f5d9e8f26da82d21f3ee6fd8be1b938ad2f69e5a3108937b0b6803fca0978
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 Andrew Hautau
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'ActiveRecordNearestNeighbor'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_record'
|
3
|
+
require 'active_record_nearest_neighbor/railtie' if defined?(Rails)
|
4
|
+
|
5
|
+
class ActiveRecord::Base
|
6
|
+
|
7
|
+
module NearestNeighbor
|
8
|
+
|
9
|
+
require 'active_record_nearest_neighbor/scopes'
|
10
|
+
require 'active_record_nearest_neighbor/close_to'
|
11
|
+
|
12
|
+
extend ActiveSupport::Concern
|
13
|
+
|
14
|
+
included do
|
15
|
+
|
16
|
+
# ensure the lonlat attribute
|
17
|
+
before_save :set_lonlat!
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
extend ActiveSupport::Concern
|
23
|
+
|
24
|
+
include Scopes
|
25
|
+
include CloseTo
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def set_lonlat!
|
32
|
+
self.lonlat = "POINT(#{self.longitude} #{self.latitude})"
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module ActiveRecord::Base::NearestNeighbor::CloseTo
|
2
|
+
|
3
|
+
def close_to(longitude_or_object, longitude_or_latitude_or_options={}, options={})
|
4
|
+
scope_method = :bounding_box
|
5
|
+
scope_params = {}
|
6
|
+
|
7
|
+
if longitude_or_object.class.ancestors.include?(ActiveRecord::Base)
|
8
|
+
# We are using an object as the point reference
|
9
|
+
object = longitude_or_object
|
10
|
+
options = longitude_or_latitude_or_options
|
11
|
+
|
12
|
+
scope_method = options[:method] || scope_method
|
13
|
+
scope_params = {id: options[:id], distance: options[:distance], limit: options[:limit]}.
|
14
|
+
merge(longitude: object.longitude, latitude: object.latitude, id: object.id)
|
15
|
+
else
|
16
|
+
# We are using longitude and latitude
|
17
|
+
longitude = longitude_or_object
|
18
|
+
latitude = longitude_or_latitude_or_options
|
19
|
+
|
20
|
+
scope_method = options[:method] || scope_method
|
21
|
+
|
22
|
+
scope_params = {id: options[:id], distance: options[:distance], limit: options[:limit]}.
|
23
|
+
merge(longitude: longitude, latitude: latitude)
|
24
|
+
end
|
25
|
+
|
26
|
+
close_to_with_scope(scope_method, scope_params)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def close_to_with_scope(scope_method, params={})
|
32
|
+
scope = "#{scope_method}_close_to".to_sym
|
33
|
+
self.send(scope,params)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'active_record/connection_adapters/postgis_adapter'
|
2
|
+
|
3
|
+
module ActiveRecordNearestNeighbor
|
4
|
+
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
|
7
|
+
rake_tasks do
|
8
|
+
load 'tasks/active_record_nearest_neighbor_tasks.rake'
|
9
|
+
load 'active_record/connection_adapters/postgis_adapter/databases.rake'
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module ActiveRecord::Base::NearestNeighbor::Scopes
|
2
|
+
|
3
|
+
def bounding_box_close_to(params)
|
4
|
+
latitude = params[:latitude]
|
5
|
+
longitude = params[:longitude]
|
6
|
+
limit = params[:limit]
|
7
|
+
|
8
|
+
# default of 10km
|
9
|
+
distance = params[:distance] || 10000
|
10
|
+
|
11
|
+
# If we have an object id, don't include the object in results
|
12
|
+
not_id_subclause = params[:id] ? "id != #{params[:id]}" : ''
|
13
|
+
|
14
|
+
where(%{ST_DWithin(points.lonlat, ST_GeographyFromText('SRID=4326;POINT(#{longitude} #{latitude})')::geometry, #{distance})}).
|
15
|
+
where(not_id_subclause).
|
16
|
+
order(%{ST_Distance(
|
17
|
+
#{table_name}.lonlat,
|
18
|
+
ST_GeographyFromText('SRID=4326;POINT(#{longitude} #{latitude})')::geometry
|
19
|
+
)}).
|
20
|
+
limit(params[:limit])
|
21
|
+
end
|
22
|
+
|
23
|
+
def k_nearest_neighbor_close_to(params)
|
24
|
+
longitude = params[:longitude]
|
25
|
+
latitude = params[:latitude]
|
26
|
+
limit = params[:limit] || 'NULL'
|
27
|
+
# If we have an object id, don't include the object in results
|
28
|
+
not_id_subclause = params[:id] ? "WHERE(id != #{params[:id]})" : ''
|
29
|
+
|
30
|
+
find_by_sql(
|
31
|
+
%{WITH closest_candidates AS (
|
32
|
+
SELECT "#{table_name}".* FROM "#{table_name}"
|
33
|
+
#{not_id_subclause}
|
34
|
+
ORDER BY
|
35
|
+
#{table_name}.lonlat::geometry <->
|
36
|
+
ST_GeographyFromText('SRID=4326;POINT(#{longitude} #{latitude})')::geometry
|
37
|
+
)
|
38
|
+
SELECT *
|
39
|
+
FROM closest_candidates
|
40
|
+
ORDER BY
|
41
|
+
ST_Distance(
|
42
|
+
closest_candidates.lonlat,
|
43
|
+
ST_GeographyFromText('SRID=4326;POINT(#{longitude} #{latitude})')::geometry
|
44
|
+
)
|
45
|
+
LIMIT #{limit};}
|
46
|
+
)
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
end
|
51
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'pry'
|
2
|
+
|
3
|
+
namespace :nearest_neighbor do
|
4
|
+
desc "Create a new table with latitude, longitude, lonlat (point type), and a spatial index for lonlat"
|
5
|
+
task :create, [:table_name] do |task, args|
|
6
|
+
timestamp = Time.now.strftime("%Y%m%d%H%M%S")
|
7
|
+
table_name = args[:table_name]
|
8
|
+
class_name = table_name.split("_").map(&:capitalize).join
|
9
|
+
migration =
|
10
|
+
%{class Create#{class_name} < ActiveRecord::Migration
|
11
|
+
def change
|
12
|
+
create_table :#{table_name} do |t|
|
13
|
+
t.decimal :latitude, precision: 9, scale: 6, null: false
|
14
|
+
t.decimal :longitude, precision: 9, scale: 6, null: false
|
15
|
+
t.point :lonlat, :geographic => true
|
16
|
+
t.index :lonlat, :spatial => true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end}
|
20
|
+
file_name = "./db/migrate/#{timestamp}_create_#{table_name}.rb"
|
21
|
+
file = File.open(file_name,"w+")
|
22
|
+
file.write(migration)
|
23
|
+
file.close
|
24
|
+
puts "generated migration: #{file_name}"
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "Add columns to existing table: latitude, longitude, lonlat (point type), and a spatial index for lonlat"
|
28
|
+
task :add_columns, [:table_name] do |task, args|
|
29
|
+
timestamp = Time.now.strftime("%Y%m%d%H%M%S")
|
30
|
+
table_name = args[:table_name]
|
31
|
+
class_name = table_name.split("_").map(&:capitalize).join
|
32
|
+
migration =
|
33
|
+
%{class AddGeospatialColumnsTo#{class_name} < ActiveRecord::Migration
|
34
|
+
def change
|
35
|
+
add_column :#{table_name}, :latitude, :decimal, precision: 9, scale: 6, null: false
|
36
|
+
add_column :#{table_name}, :longitude, :decimal, precision: 9, scale: 6, null: false
|
37
|
+
add_column :#{table_name}, :lonlat, :point, geographic: true
|
38
|
+
add_index :#{table_name}, :lonlat, spatial: true
|
39
|
+
end
|
40
|
+
end}
|
41
|
+
file_name = "db/migrate/#{timestamp}_add_geospatial_columns_to_#{table_name}.rb"
|
42
|
+
file = File.open(file_name,"w+")
|
43
|
+
file.write(migration)
|
44
|
+
file.close
|
45
|
+
puts "generated migration: #{file_name}"
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_nearest_neighbor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Hautau
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord-postgis-adapter
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: ActiveRecordNearestNeighbor adds methods to your ActiveRecord models
|
56
|
+
to perform nearest neighbor searches, using different algorithms. The plugin also
|
57
|
+
provides a rake task for generating migrations to add geospatial columns and indexes
|
58
|
+
to your models.
|
59
|
+
email:
|
60
|
+
- arhautau@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- MIT-LICENSE
|
66
|
+
- Rakefile
|
67
|
+
- lib/active_record_nearest_neighbor.rb
|
68
|
+
- lib/active_record_nearest_neighbor/close_to.rb
|
69
|
+
- lib/active_record_nearest_neighbor/railtie.rb
|
70
|
+
- lib/active_record_nearest_neighbor/scopes.rb
|
71
|
+
- lib/active_record_nearest_neighbor/version.rb
|
72
|
+
- lib/tasks/active_record_nearest_neighbor_tasks.rake
|
73
|
+
homepage: https://github.com/CrossTheStreams/active_record_nearest_neighbor
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.2.2
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: A Rails/ActiveRecord plugin to easily add nearest neighbor geospatial queries
|
97
|
+
with PostGIS and PostgreSQL.
|
98
|
+
test_files: []
|
99
|
+
has_rdoc:
|