geo_query 0.0.1
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 +13 -0
- data/config/routes.rb +2 -0
- data/lib/generators/geo_query/resource/resource_generator.rb +34 -0
- data/lib/generators/geo_query/resource/templates/add_coordinates_to.rb.erb +6 -0
- data/lib/geo_query/engine.rb +14 -0
- data/lib/geo_query/geo_queryable.rb +45 -0
- data/lib/geo_query/version.rb +3 -0
- data/lib/geo_query.rb +14 -0
- data/lib/tasks/geo_query_tasks.rake +4 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 067870f16295abd5335d979eaff90afe4fc43193
|
4
|
+
data.tar.gz: f0b5a9c5ae41d5fc49b9cc548968b0356d652257
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8d0efcf1edb11f5276814a205e8787592bfc04d2ffb5b677c3afbaa53a554ca0a58c6fd69d480580021e1b8de584bfd45f862230bded6bcf9da469135b85a600
|
7
|
+
data.tar.gz: 440a3a9ab8c43cd7ff3270af33f9f7899d9166a173b7ec9bc1bd33600dc2c7bac9e0988a7e3748023dc01479d8db50a5ff618403bada00442d29817627dc0cac
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 William Porter
|
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
data/config/routes.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
|
3
|
+
class GeoQuery::ResourceGenerator < Rails::Generators::NamedBase
|
4
|
+
include Rails::Generators::Migration
|
5
|
+
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
def copy_migrations
|
9
|
+
copy_migration("add_coordinates_to", name.underscore)
|
10
|
+
|
11
|
+
puts "added coordinates to #{name}"
|
12
|
+
puts "rake db:migrate"
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.next_migration_number(dirname)
|
16
|
+
if ActiveRecord::Base.timestamped_migrations
|
17
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
18
|
+
else
|
19
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def copy_migration(file, name)
|
26
|
+
filename = "#{file}_#{name}"
|
27
|
+
if self.class.migration_exists?("db/migrate", "#{filename}")
|
28
|
+
say_status("skipped", "Migration #{filename}.rb already exists")
|
29
|
+
else
|
30
|
+
migration_template "#{file}.rb.erb", "db/migrate/#{filename}.rb"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -0,0 +1,6 @@
|
|
1
|
+
class AddCoordinatesTo<%= name.camelize %> < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
add_column <%= ":#{name.underscore.pluralize.to_sym}" %>, :coordinates, :st_point, geographic: true
|
4
|
+
add_index <%= ":#{name.underscore.pluralize.to_sym}" %>, :coordinates, using: :gist
|
5
|
+
end
|
6
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'activerecord-postgis-adapter'
|
2
|
+
|
3
|
+
module GeoQuery
|
4
|
+
class Engine < ::Rails::Engine
|
5
|
+
isolate_namespace GeoQuery
|
6
|
+
|
7
|
+
config.generators do |g|
|
8
|
+
g.test_framework :rspec, :fixture => false
|
9
|
+
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
|
10
|
+
g.assets false
|
11
|
+
g.helper false
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module GeoQuery
|
2
|
+
module GeoQueryable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
# Class vars
|
7
|
+
mattr_accessor :point_column
|
8
|
+
@@point_column = nil
|
9
|
+
|
10
|
+
# Validations
|
11
|
+
before_validation :save_coordinates
|
12
|
+
|
13
|
+
attr_accessor :lat, :lng
|
14
|
+
|
15
|
+
# Instance Methods
|
16
|
+
def save_coordinates
|
17
|
+
if lat.present? && lng.present?
|
18
|
+
self.coordinates = "POINT(#{lng} #{lat})"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Class methods
|
23
|
+
## Returns other objects within the given radius of a point
|
24
|
+
def self.near_lat_lng(lat, lng, radius=500) #radius in metres
|
25
|
+
select("#{self.base_class.table_name}.*, ST_Distance(#{self.base_class.point_column}, ST_GeographyFromText('POINT(#{lng} #{lat})')) AS distance")
|
26
|
+
.where("ST_DWithin(#{self.base_class.point_column} , ST_GeographyFromText('POINT(#{lng} #{lat})'), #{radius})")
|
27
|
+
.order("distance ASC")
|
28
|
+
end
|
29
|
+
|
30
|
+
## Returns all objects within a bounding box
|
31
|
+
def self.within_bounding_box(min_lat, min_lng, max_lat, max_lng)
|
32
|
+
select("#{self.base_class.table_name}.*").
|
33
|
+
where("#{self.base_class.point_column} && ST_MakeEnvelope(?, ?, ?, ?, 4326)",
|
34
|
+
min_lng, min_lat, max_lng, max_lat)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module ClassMethods
|
39
|
+
# Init method
|
40
|
+
def geo_queryable(options = {})
|
41
|
+
self.base_class.point_column = options[:coordinates] || :coordinates
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/geo_query.rb
ADDED
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geo_query
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- William Porter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord-postgis-adapter
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.0.0.beta5
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.0.beta5
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pg
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: factory_girl_rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: awesome_print
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Saves you the hassle of writing your own location queries and uses pre-built
|
98
|
+
and reusable methods.
|
99
|
+
email:
|
100
|
+
- wp@papercloud.com.au
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- MIT-LICENSE
|
106
|
+
- Rakefile
|
107
|
+
- config/routes.rb
|
108
|
+
- lib/generators/geo_query/resource/resource_generator.rb
|
109
|
+
- lib/generators/geo_query/resource/templates/add_coordinates_to.rb.erb
|
110
|
+
- lib/geo_query.rb
|
111
|
+
- lib/geo_query/engine.rb
|
112
|
+
- lib/geo_query/geo_queryable.rb
|
113
|
+
- lib/geo_query/version.rb
|
114
|
+
- lib/tasks/geo_query_tasks.rake
|
115
|
+
homepage: http://papercloud.com.au
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata: {}
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 2.4.5
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: Query PostGIS coordinate objects on any model
|
139
|
+
test_files: []
|