postgres_ext-postgis 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.travis.yml +19 -0
- data/CONTRIBUTING.md +24 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +79 -0
- data/Rakefile +71 -0
- data/lib/postgres_ext/postgis/active_record/connection_adapters/postgresql/schema_statements.rb +34 -0
- data/lib/postgres_ext/postgis/active_record/connection_adapters/postgresql.rb +1 -0
- data/lib/postgres_ext/postgis/active_record/connection_adapters/postgresql_adapter.rb +95 -0
- data/lib/postgres_ext/postgis/active_record/connection_adapters.rb +89 -0
- data/lib/postgres_ext/postgis/active_record.rb +4 -0
- data/lib/postgres_ext/postgis/arel/visitors/postgresql.rb +17 -0
- data/lib/postgres_ext/postgis/arel/visitors.rb +4 -0
- data/lib/postgres_ext/postgis/arel.rb +4 -0
- data/lib/postgres_ext/postgis/version.rb +5 -0
- data/lib/postgres_ext/postgis.rb +21 -0
- data/postgres_ext-postgis.gemspec +36 -0
- data/test/migrations/geometry_test.rb +33 -0
- data/test/querying/contains_test.rb +11 -0
- data/test/schema_dumper/geometry_test.rb +29 -0
- data/test/test_helper.rb +42 -0
- data/test/type_casts/geometry_test.rb +38 -0
- metadata +213 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d3ebfeda4eadd075cabe4ec92ff88ebe06673214
|
4
|
+
data.tar.gz: 06eac67b90a351b8b271a6e762734e0590c28e05
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 533b45da56945fd706b071f7aa213f64cc49f13168c41ad190c49daef4c36540e7ede36148279eea86213bf7f20f04223adedb95058aeb512293121bd5f10a5d
|
7
|
+
data.tar.gz: 2480b4bff28cc49ddedf4744036ffec76aa05cb3ace801e6121dbe089be16ca9e771926e82e0cbc9e707f621bfb2757b2240019240c9e543c398240f68577336
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
3
|
+
rvm:
|
4
|
+
- 2.0.0
|
5
|
+
- 2.1.0
|
6
|
+
before_install:
|
7
|
+
- sudo apt-get install libgeos-dev
|
8
|
+
- gem install bundler -v '= 1.5.1'
|
9
|
+
|
10
|
+
before_script:
|
11
|
+
- psql -c 'create database postgres_ext_postgis_test;' -U postgres -h localhost
|
12
|
+
- rake db:migrate
|
13
|
+
|
14
|
+
env: DATABASE_URL=postgres://postgres@localhost/postgres_ext_postgis_test
|
15
|
+
|
16
|
+
notifications:
|
17
|
+
hipchat:
|
18
|
+
rooms:
|
19
|
+
secure: YTnjwnQBCUm8/D+QbUlxKgH/LgKRXDgoed9nbnlW4LIiuguUAxhqrZGlusVvXs8b2IMuw3A4rHOpV/NEr6mwoA7eP6/Jn+KSWOIOIftRBW3p0INHSeKpHiPZ5pTVudgPAsfE5pojHTRXM19bf6RAiyOr02SAiafwsr6Nx1VCNTY=
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# Contribution Guidelines #
|
2
|
+
|
3
|
+
## Submitting a new issue ##
|
4
|
+
|
5
|
+
If you want to ensure that your issue gets fixed *fast* you should
|
6
|
+
attempt to reproduce the issue in an isolated example application that
|
7
|
+
you can share.
|
8
|
+
|
9
|
+
## Making a pull request ##
|
10
|
+
|
11
|
+
If you'd like to submit a pull request please adhere to the following:
|
12
|
+
|
13
|
+
1. Your code *must* be tested. Please TDD your code!
|
14
|
+
2. No single-character variables
|
15
|
+
3. Two-spaces instead of tabs
|
16
|
+
4. Single-quotes instead of double-quotes unless you are using string
|
17
|
+
interpolation or escapes.
|
18
|
+
5. General Rails/Ruby naming conventions for files and classes
|
19
|
+
|
20
|
+
Please note that you must adhere to each of the aforementioned rules.
|
21
|
+
Failure to do so will result in an immediate closing of the pull
|
22
|
+
request. If you update and rebase the pull request to follow the
|
23
|
+
guidelines your pull request will be re-opened and considered for
|
24
|
+
inclusion.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Dan McClain
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# PostgresExt::Postgis
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
gem 'postgres_ext-postgis'
|
8
|
+
|
9
|
+
And then execute:
|
10
|
+
|
11
|
+
$ bundle
|
12
|
+
|
13
|
+
Or install it yourself as:
|
14
|
+
|
15
|
+
$ gem install postgres_ext-postgis
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Just `require 'postgres_ext/postgis'` and use ActiveRecord as you normally would!
|
20
|
+
postgres\ext extends ActiveRecord's data type handling and query methods
|
21
|
+
in both Arel and ActiveRecord.
|
22
|
+
|
23
|
+
### Migrations
|
24
|
+
Adding a geomtry column to your table is simple! You can create a plain
|
25
|
+
geometry column, or specify your spatial type and projection:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
create_table :districts do |t|
|
29
|
+
t.geometry :location
|
30
|
+
t.geometry :district_boundries, spatial_type: :multipolygon, srid: 4326
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
### Type Casting
|
35
|
+
|
36
|
+
PostGIS geometry types are converted to [RGeo]() objects. You can set
|
37
|
+
your PostGIS types in ActiveRecord using either the Extended Well-Known Binary
|
38
|
+
(WKB) or Extended Well-Known Text (WKT) representation:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
user.location = 'SRID=4623;POINT(1 1)'
|
42
|
+
```
|
43
|
+
|
44
|
+
### Querying
|
45
|
+
|
46
|
+
#### Contains
|
47
|
+
Using the [contains querying interface from postgres\_ext](), we can
|
48
|
+
query if geometry column contains the specified object
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
District.where.contains(district_boundries: user.location)
|
52
|
+
```
|
53
|
+
## Authors ##
|
54
|
+
|
55
|
+
* [Dan McClain](http://twitter.com/_danmcclain)
|
56
|
+
|
57
|
+
[We are very thankful for the many
|
58
|
+
contributors](https://github.com/dockyard/postgres_ext-postgis/graphs/contributors)
|
59
|
+
|
60
|
+
## Versioning ##
|
61
|
+
|
62
|
+
This gem follows [Semantic Versioning](http://semver.org)
|
63
|
+
|
64
|
+
## Want to help? ##
|
65
|
+
|
66
|
+
Please do! We are always looking to improve this gem. Please see our
|
67
|
+
[Contribution
|
68
|
+
Guidelines](https://github.com/dockyard/postgre_ext-postgis/blob/master/CONTRIBUTING.md)
|
69
|
+
on how to properly submit issues and pull requests.
|
70
|
+
|
71
|
+
## Legal ##
|
72
|
+
|
73
|
+
[DockYard](http://dockyard.com), Inc. © 2014
|
74
|
+
|
75
|
+
[@dockyard](http://twitter.com/dockyard)
|
76
|
+
|
77
|
+
[Licensed under the MIT
|
78
|
+
license](http://www.opensource.org/licenses/mit-license.php)
|
79
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
Rake::TestTask.new(:test) do |t|
|
6
|
+
t.libs << 'lib'
|
7
|
+
t.libs << 'test'
|
8
|
+
t.pattern = 'test/**/*_test.rb'
|
9
|
+
t.verbose = false
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => :test
|
13
|
+
|
14
|
+
task :setup do
|
15
|
+
if File.exist?('.env')
|
16
|
+
puts 'This will overwrite your existing .env file'
|
17
|
+
end
|
18
|
+
print 'Enter your database name: [postgres_ext_postgis_test] '
|
19
|
+
db_name = STDIN.gets.chomp
|
20
|
+
print 'Enter your database user: [] '
|
21
|
+
db_user = STDIN.gets.chomp
|
22
|
+
print 'Enter your database password: [] '
|
23
|
+
db_password = STDIN.gets.chomp
|
24
|
+
print 'Enter your database server: [localhost] '
|
25
|
+
db_server = STDIN.gets.chomp
|
26
|
+
|
27
|
+
db_name = 'postgres_ext_postgis_test' if db_name.empty?
|
28
|
+
db_password = ":#{db_password}" unless db_password.empty?
|
29
|
+
db_server = 'localhost' if db_server.empty?
|
30
|
+
|
31
|
+
db_server = "@#{db_server}" unless db_user.empty?
|
32
|
+
|
33
|
+
env_path = File.expand_path('./.env')
|
34
|
+
File.open(env_path, 'w') do |file|
|
35
|
+
file.puts "DATABASE_NAME=#{db_name}"
|
36
|
+
file.puts "DATABASE_URL=\"postgres://#{db_user}#{db_password}#{db_server}/#{db_name}\""
|
37
|
+
end
|
38
|
+
|
39
|
+
puts '.env file saved'
|
40
|
+
end
|
41
|
+
|
42
|
+
namespace :db do
|
43
|
+
task :load_db_settings do
|
44
|
+
require 'active_record'
|
45
|
+
require 'postgres_ext/postgis'
|
46
|
+
unless ENV['DATABASE_URL']
|
47
|
+
require 'dotenv'
|
48
|
+
Dotenv.load
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
task :drop => :load_db_settings do
|
53
|
+
%x{ dropdb #{ENV['DATABASE_NAME']} }
|
54
|
+
end
|
55
|
+
|
56
|
+
task :create => :load_db_settings do
|
57
|
+
%x{ createdb #{ENV['DATABASE_NAME']} }
|
58
|
+
end
|
59
|
+
|
60
|
+
task :migrate => :load_db_settings do
|
61
|
+
ActiveRecord::Base.establish_connection
|
62
|
+
|
63
|
+
ActiveRecord::Base.connection.enable_extension 'postgis'
|
64
|
+
|
65
|
+
ActiveRecord::Base.connection.create_table :places, force: true do |t|
|
66
|
+
t.geometry :location
|
67
|
+
end
|
68
|
+
|
69
|
+
puts 'Database migrated'
|
70
|
+
end
|
71
|
+
end
|
data/lib/postgres_ext/postgis/active_record/connection_adapters/postgresql/schema_statements.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module PostgresExt::Postgis::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::SchemaCreation
|
2
|
+
private
|
3
|
+
|
4
|
+
def add_column_options!(sql, options)
|
5
|
+
if spatial_type_option = options[:spatial_type] || options[:column].try(:spatial_type)
|
6
|
+
srid = options[:srid] || options[:column].try(:srid)
|
7
|
+
sql << "(#{postgis_spatial_type spatial_type_option}, #{srid})"
|
8
|
+
end
|
9
|
+
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
def postgis_spatial_type(option)
|
14
|
+
POSTGIS_SPATIAL_TYPES[option]
|
15
|
+
end
|
16
|
+
|
17
|
+
POSTGIS_SPATIAL_TYPES = {
|
18
|
+
point: 'Point',
|
19
|
+
pointm: 'PointM',
|
20
|
+
linestring: 'LineString',
|
21
|
+
linestringm: 'LineStringM',
|
22
|
+
polygon: 'Polygon',
|
23
|
+
polygonm: 'PolygonM',
|
24
|
+
multipoint: 'MultiPoint',
|
25
|
+
multipointm: 'MultiPointM',
|
26
|
+
multilinestring: 'MultiLineString',
|
27
|
+
multilinestringm: 'MultiLineStringM',
|
28
|
+
multipolygon: 'MultiPolygon',
|
29
|
+
multipolygonm: 'MultiPolygonM',
|
30
|
+
geometrycollection: 'GeometryCollection',
|
31
|
+
geometrycollectionm: 'GeometryCollectionM',
|
32
|
+
geometry: 'Geometry'
|
33
|
+
}
|
34
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'postgres_ext/postgis/active_record/connection_adapters/postgresql/schema_statements'
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module PostgresExt::Postgis::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
|
2
|
+
POSTGIS_DATABASE_TYPES = {
|
3
|
+
geometry: { name: 'geometry' }
|
4
|
+
}
|
5
|
+
|
6
|
+
def self.prepended(klass)
|
7
|
+
klass::NATIVE_DATABASE_TYPES.merge! POSTGIS_DATABASE_TYPES
|
8
|
+
klass::TableDefinition.send :prepend, TableDefinition
|
9
|
+
klass::ColumnDefinition.send :prepend, ColumnDefinition
|
10
|
+
klass::SchemaCreation.send :prepend, SchemaCreation
|
11
|
+
klass::OID.send :prepend, OID
|
12
|
+
klass.send :prepend, Quoting
|
13
|
+
end
|
14
|
+
|
15
|
+
def prepare_column_options(column, types)
|
16
|
+
spec = super
|
17
|
+
|
18
|
+
if column.type == :geometry
|
19
|
+
if column.srid
|
20
|
+
spec[:srid] = column.srid
|
21
|
+
spec[:geometry_type] = column.geometry_type
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
spec
|
26
|
+
end
|
27
|
+
|
28
|
+
def migration_keys
|
29
|
+
super + [:srid, :geometry_type]
|
30
|
+
end
|
31
|
+
|
32
|
+
module ColumnMethods
|
33
|
+
def geometry(name, options = {})
|
34
|
+
column(name, 'geometry', options)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module ColumnDefinition
|
39
|
+
attr_accessor :spatial_type, :srid
|
40
|
+
end
|
41
|
+
|
42
|
+
module TableDefinition
|
43
|
+
include ColumnMethods
|
44
|
+
|
45
|
+
def column(name, type, options = {})
|
46
|
+
super
|
47
|
+
column = self[name]
|
48
|
+
|
49
|
+
if type.to_s == 'geometry'
|
50
|
+
column.srid = options[:srid]
|
51
|
+
column.spatial_type = options[:spatial_type]
|
52
|
+
end
|
53
|
+
|
54
|
+
self
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
module Quoting
|
59
|
+
def quote(value, column = nil)
|
60
|
+
if column && column.type == :geometry
|
61
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLColumn.geometry_to_string(value)
|
62
|
+
else
|
63
|
+
super
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def type_cast(value, column, array_member = false)
|
68
|
+
return super unless column
|
69
|
+
|
70
|
+
case value
|
71
|
+
when RGeo::Feature::Instance
|
72
|
+
return super unless /geometry/ =~ column.sql_type
|
73
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLColumn.geometry_to_string(value)
|
74
|
+
else
|
75
|
+
super
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
module OID
|
81
|
+
def self.prepended(klass)
|
82
|
+
klass.register_type 'geometry', OID::Geometry.new
|
83
|
+
end
|
84
|
+
|
85
|
+
class Geometry < ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Type
|
86
|
+
def type_cast(value)
|
87
|
+
if String === value
|
88
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLColumn.string_to_geometry value
|
89
|
+
else
|
90
|
+
value
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module PostgresExt::Postgis::ActiveRecord::ConnectionAdapters
|
2
|
+
module PostgreSQLColumn
|
3
|
+
def self.prepended(klass)
|
4
|
+
klass.class_eval do
|
5
|
+
attr_reader :geometry_type, :srid
|
6
|
+
class << klass
|
7
|
+
prepend ClassMethods
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def wkb_parser
|
14
|
+
@wkb_parser ||= RGeo::WKRep::WKBParser.new nil, support_ewkb: true
|
15
|
+
end
|
16
|
+
|
17
|
+
def wkb_generator
|
18
|
+
@wkb_generator ||= RGeo::WKRep::WKBGenerator.new hex_format: true, type_format: :ewkb, emit_ewkb_srid: true
|
19
|
+
end
|
20
|
+
|
21
|
+
def wkt_parser
|
22
|
+
@wkt_parser ||= RGeo::WKRep::WKTParser.new nil, support_ewkt: true
|
23
|
+
end
|
24
|
+
|
25
|
+
def string_to_geometry(value)
|
26
|
+
if value.index('(')
|
27
|
+
wkt_parser.parse value
|
28
|
+
else
|
29
|
+
wkb_parser.parse value
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def geometry_to_string(value)
|
34
|
+
if RGeo::Feature::Instance
|
35
|
+
wkb_generator.generate value
|
36
|
+
else
|
37
|
+
value
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def initialize(name, default, oid_type, sql_type = nil, null = true)
|
43
|
+
super
|
44
|
+
if type == :geometry
|
45
|
+
@srid = extract_srid sql_type
|
46
|
+
@geometry_type = extract_geometry_type sql_type
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def extract_srid(sql_type)
|
53
|
+
if match = geometry_type_regex.match(sql_type)
|
54
|
+
match[:srid]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def extract_geometry_type(sql_type)
|
59
|
+
if match = geometry_type_regex.match(sql_type)
|
60
|
+
match[:geom_type].downcase.to_sym.inspect
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def geometry_type_regex
|
65
|
+
/geometry\((?<geom_type>\w+),(?<srid>\d+)\)/i
|
66
|
+
end
|
67
|
+
|
68
|
+
def extract_limit(sql_type)
|
69
|
+
case sql_type
|
70
|
+
when /^geometry/i
|
71
|
+
nil
|
72
|
+
else
|
73
|
+
super
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def simplified_type(field_type)
|
78
|
+
case field_type
|
79
|
+
when /geometry(\(\w,\d\))?/
|
80
|
+
:geometry
|
81
|
+
else
|
82
|
+
super
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
require 'postgres_ext/postgis/active_record/connection_adapters/postgresql_adapter'
|
89
|
+
require 'postgres_ext/postgis/active_record/connection_adapters/postgresql'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module PostgresExt::Postgis::Arel::Visitors::PostgreSQL
|
2
|
+
private
|
3
|
+
|
4
|
+
def visit_Arel_Nodes_Contains(o, a = nil)
|
5
|
+
left_column = o.left.relation.engine.columns.find { |col| col.name == o.left.name.to_s }
|
6
|
+
|
7
|
+
if left_column.type == :geometry
|
8
|
+
"ST_CONTAINS(#{visit o.left, a}, #{visit o.right, o.left})"
|
9
|
+
else
|
10
|
+
super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def visit_RGeo_Feature_Instance(node, attribute = nil)
|
15
|
+
quote(node, column_for(attribute))
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module PostgresExt
|
2
|
+
module Postgis
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'rgeo'
|
7
|
+
require 'postgres_ext'
|
8
|
+
require 'postgres_ext/postgis/version'
|
9
|
+
require 'active_record/connection_adapters/postgresql_adapter'
|
10
|
+
require 'postgres_ext/postgis/active_record'
|
11
|
+
require 'postgres_ext/postgis/arel'
|
12
|
+
require 'active_record/schema_dumper'
|
13
|
+
|
14
|
+
ActiveRecord::SchemaDumper.ignore_tables << 'spatial_ref_sys'
|
15
|
+
|
16
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:prepend, PostgresExt::Postgis::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
|
17
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send(:prepend, PostgresExt::Postgis::ActiveRecord::ConnectionAdapters::PostgreSQLColumn)
|
18
|
+
|
19
|
+
require 'arel/visitors/postgresql'
|
20
|
+
|
21
|
+
Arel::Visitors::PostgreSQL.send(:prepend, PostgresExt::Postgis::Arel::Visitors::PostgreSQL)
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'postgres_ext/postgis/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'postgres_ext-postgis'
|
8
|
+
spec.version = PostgresExt::Postgis::VERSION
|
9
|
+
spec.authors = ['Dan McClain']
|
10
|
+
spec.email = ['rubygems@danmcclain.net']
|
11
|
+
spec.description = %q{Adding support for PostGIS types and clean PostGIS querying extensions to ActiveRecord}
|
12
|
+
spec.summary = %q{Extending ActiveRecord's geospatial awareness with PostGIS}
|
13
|
+
spec.homepage = 'https://github.com/dockyard/postgres_ext-postgis'
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'postgres_ext', '~> 2.1.0'
|
22
|
+
spec.add_runtime_dependency 'rgeo'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.1.0'
|
26
|
+
spec.add_development_dependency 'minitest'
|
27
|
+
spec.add_development_dependency 'm'
|
28
|
+
spec.add_development_dependency 'bourne', '~> 1.3.0'
|
29
|
+
spec.add_development_dependency 'database_cleaner'
|
30
|
+
spec.add_development_dependency 'dotenv'
|
31
|
+
if RUBY_PLATFORM =~ /java/
|
32
|
+
spec.add_development_dependency 'activerecord-jdbcpostgresql-adapter', '1.3.0.beta2'
|
33
|
+
else
|
34
|
+
spec.add_development_dependency 'pg', '> 0.13.2'
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe 'Geometry Migrations' do
|
4
|
+
after { connection.drop_table :data_types }
|
5
|
+
|
6
|
+
it 'creates geometry columns' do
|
7
|
+
connection.create_table :data_types do |t|
|
8
|
+
t.geometry :geo_1
|
9
|
+
t.column :geo_2, :geometry
|
10
|
+
end
|
11
|
+
|
12
|
+
columns = connection.columns(:data_types)
|
13
|
+
geo_1 = columns.find { |c| c.name == 'geo_1' }
|
14
|
+
geo_2 = columns.find { |c| c.name == 'geo_2' }
|
15
|
+
|
16
|
+
geo_1.sql_type.must_equal 'geometry'
|
17
|
+
geo_2.sql_type.must_equal 'geometry'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'creates geometry columns with type and SRID' do
|
21
|
+
connection.create_table :data_types do |t|
|
22
|
+
t.geometry :geo_1, spatial_type: :linestring, srid: 4326
|
23
|
+
t.column :geo_2, :geometry, spatial_type: :linestring, srid: 4326
|
24
|
+
end
|
25
|
+
|
26
|
+
columns = connection.columns(:data_types)
|
27
|
+
geo_1 = columns.find { |c| c.name == 'geo_1' }
|
28
|
+
geo_2 = columns.find { |c| c.name == 'geo_2' }
|
29
|
+
|
30
|
+
geo_1.sql_type.must_equal 'geometry(LineString,4326)'
|
31
|
+
geo_2.sql_type.must_equal 'geometry(LineString,4326)'
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe 'where.contains(geometry)' do
|
4
|
+
it 'genereates the appropriate where clause' do
|
5
|
+
point = Place.new(location: 'POINT(1 2)').location
|
6
|
+
|
7
|
+
query = Place.where.contains(location: point)
|
8
|
+
|
9
|
+
query.to_sql.must_match /ST_CONTAINS\(\"places\".\"location\", 0020000001000000003ff00000000000004000000000000000\)/
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe 'geometry schema dump' do
|
4
|
+
let(:stream) { StringIO.new }
|
5
|
+
let(:schema) { stream.string }
|
6
|
+
|
7
|
+
before do
|
8
|
+
connection.create_table :testings do |t|
|
9
|
+
t.geometry :geo
|
10
|
+
t.geometry :lines, spatial_type: :linestring, srid: 4326
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveRecord::SchemaDumper.dump(connection, stream)
|
14
|
+
end
|
15
|
+
|
16
|
+
after { connection.drop_table :testings }
|
17
|
+
|
18
|
+
it 'does not include the spatial_ref_sys table' do
|
19
|
+
schema.wont_match /create_table \"spatial_ref_sys\"/
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'generates the geometry column statements' do
|
23
|
+
schema.must_match /t\.geometry "geo"/
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'captures geometry settings in column statments' do
|
27
|
+
schema.must_match /t\.geometry "lines", srid: 4326, geometry_type: :linestring/
|
28
|
+
end
|
29
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'bourne'
|
3
|
+
require 'postgres_ext/postgis'
|
4
|
+
require 'rgeo'
|
5
|
+
require 'database_cleaner'
|
6
|
+
|
7
|
+
if defined?(M)
|
8
|
+
require 'minitest/spec'
|
9
|
+
else
|
10
|
+
require 'minitest/autorun'
|
11
|
+
end
|
12
|
+
|
13
|
+
unless ENV['CI'] || RUBY_PLATFORM =~ /java/
|
14
|
+
require 'byebug'
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'dotenv'
|
18
|
+
Dotenv.load
|
19
|
+
|
20
|
+
class Place < ActiveRecord::Base
|
21
|
+
end
|
22
|
+
|
23
|
+
ActiveRecord::Base.establish_connection
|
24
|
+
DatabaseCleaner.strategy = :deletion
|
25
|
+
|
26
|
+
class MiniTest::Spec
|
27
|
+
class << self
|
28
|
+
alias :context :describe
|
29
|
+
end
|
30
|
+
|
31
|
+
before do
|
32
|
+
DatabaseCleaner.clean
|
33
|
+
end
|
34
|
+
|
35
|
+
after do
|
36
|
+
DatabaseCleaner.clean
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def connection
|
41
|
+
ActiveRecord::Base.connection
|
42
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe 'geometry casts' do
|
4
|
+
it 'converts WKB from PostgreSQL into RGeo objects' do
|
5
|
+
connection.execute <<-SQL
|
6
|
+
INSERT INTO places (location)
|
7
|
+
VALUES (ST_GeomFromText('POINT(1 1)', 26918));
|
8
|
+
SQL
|
9
|
+
place = Place.first
|
10
|
+
|
11
|
+
(RGeo::Feature::Point === place.location).must_equal true
|
12
|
+
place.location.x.must_equal 1
|
13
|
+
place.location.y.must_equal 1
|
14
|
+
place.location.srid.must_equal 26918
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'parses WKT attributes into RGeo objects' do
|
18
|
+
place = Place.new
|
19
|
+
|
20
|
+
place.location = 'SRID=4623;POINT(1 1)'
|
21
|
+
(RGeo::Feature::Point === place.location).must_equal true
|
22
|
+
place.location.x.must_equal 1
|
23
|
+
place.location.y.must_equal 1
|
24
|
+
place.location.srid.must_equal 4623
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'enables roundtripping from the database' do
|
28
|
+
place = Place.new
|
29
|
+
|
30
|
+
place.location = 'SRID=1;POINT(2 2)'
|
31
|
+
place.save
|
32
|
+
place.reload
|
33
|
+
(RGeo::Feature::Point === place.location).must_equal true
|
34
|
+
place.location.x.must_equal 2
|
35
|
+
place.location.y.must_equal 2
|
36
|
+
place.location.srid.must_equal 1
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: postgres_ext-postgis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dan McClain
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: postgres_ext
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rgeo
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 10.1.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 10.1.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
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: m
|
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
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: bourne
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.3.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.3.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: database_cleaner
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: dotenv
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: pg
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 0.13.2
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 0.13.2
|
153
|
+
description: Adding support for PostGIS types and clean PostGIS querying extensions
|
154
|
+
to ActiveRecord
|
155
|
+
email:
|
156
|
+
- rubygems@danmcclain.net
|
157
|
+
executables: []
|
158
|
+
extensions: []
|
159
|
+
extra_rdoc_files: []
|
160
|
+
files:
|
161
|
+
- ".gitignore"
|
162
|
+
- ".travis.yml"
|
163
|
+
- CONTRIBUTING.md
|
164
|
+
- Gemfile
|
165
|
+
- LICENSE.txt
|
166
|
+
- README.md
|
167
|
+
- Rakefile
|
168
|
+
- lib/postgres_ext/postgis.rb
|
169
|
+
- lib/postgres_ext/postgis/active_record.rb
|
170
|
+
- lib/postgres_ext/postgis/active_record/connection_adapters.rb
|
171
|
+
- lib/postgres_ext/postgis/active_record/connection_adapters/postgresql.rb
|
172
|
+
- lib/postgres_ext/postgis/active_record/connection_adapters/postgresql/schema_statements.rb
|
173
|
+
- lib/postgres_ext/postgis/active_record/connection_adapters/postgresql_adapter.rb
|
174
|
+
- lib/postgres_ext/postgis/arel.rb
|
175
|
+
- lib/postgres_ext/postgis/arel/visitors.rb
|
176
|
+
- lib/postgres_ext/postgis/arel/visitors/postgresql.rb
|
177
|
+
- lib/postgres_ext/postgis/version.rb
|
178
|
+
- postgres_ext-postgis.gemspec
|
179
|
+
- test/migrations/geometry_test.rb
|
180
|
+
- test/querying/contains_test.rb
|
181
|
+
- test/schema_dumper/geometry_test.rb
|
182
|
+
- test/test_helper.rb
|
183
|
+
- test/type_casts/geometry_test.rb
|
184
|
+
homepage: https://github.com/dockyard/postgres_ext-postgis
|
185
|
+
licenses:
|
186
|
+
- MIT
|
187
|
+
metadata: {}
|
188
|
+
post_install_message:
|
189
|
+
rdoc_options: []
|
190
|
+
require_paths:
|
191
|
+
- lib
|
192
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
193
|
+
requirements:
|
194
|
+
- - ">="
|
195
|
+
- !ruby/object:Gem::Version
|
196
|
+
version: '0'
|
197
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
198
|
+
requirements:
|
199
|
+
- - ">="
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: '0'
|
202
|
+
requirements: []
|
203
|
+
rubyforge_project:
|
204
|
+
rubygems_version: 2.2.0
|
205
|
+
signing_key:
|
206
|
+
specification_version: 4
|
207
|
+
summary: Extending ActiveRecord's geospatial awareness with PostGIS
|
208
|
+
test_files:
|
209
|
+
- test/migrations/geometry_test.rb
|
210
|
+
- test/querying/contains_test.rb
|
211
|
+
- test/schema_dumper/geometry_test.rb
|
212
|
+
- test/test_helper.rb
|
213
|
+
- test/type_casts/geometry_test.rb
|