activerecord-postgis-adapter 0.2.3 → 0.3.0
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.
- data/History.rdoc +8 -0
- data/README.rdoc +126 -71
- data/Version +1 -1
- data/lib/active_record/connection_adapters/postgis_adapter.rb +13 -379
- data/lib/active_record/connection_adapters/postgis_adapter/arel_tosql.rb +63 -0
- data/lib/{rgeo/active_record → active_record/connection_adapters}/postgis_adapter/databases.rake +1 -1
- data/lib/active_record/connection_adapters/postgis_adapter/main_adapter.rb +280 -0
- data/lib/active_record/connection_adapters/postgis_adapter/railtie.rb +64 -0
- data/lib/active_record/connection_adapters/postgis_adapter/spatial_column.rb +168 -0
- data/lib/active_record/connection_adapters/postgis_adapter/spatial_table_definition.rb +136 -0
- data/lib/active_record/connection_adapters/postgis_adapter/version.rb +62 -0
- data/lib/rgeo/active_record/postgis_adapter/railtie.rb +2 -21
- data/test/tc_basic.rb +8 -159
- data/test/tc_ddl.rb +270 -0
- data/test/tc_spatial_queries.rb +155 -0
- metadata +18 -8
@@ -0,0 +1,136 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# PostGIS adapter for ActiveRecord
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2010 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
|
37
|
+
# :stopdoc:
|
38
|
+
|
39
|
+
module ActiveRecord
|
40
|
+
|
41
|
+
module ConnectionAdapters
|
42
|
+
|
43
|
+
module PostGISAdapter
|
44
|
+
|
45
|
+
|
46
|
+
class SpatialTableDefinition < ConnectionAdapters::TableDefinition
|
47
|
+
|
48
|
+
def column(name_, type_, options_={})
|
49
|
+
if (info_ = @base.spatial_column_constructor(type_.to_sym))
|
50
|
+
options_[:type] ||= info_[:type] || type_
|
51
|
+
type_ = :spatial
|
52
|
+
end
|
53
|
+
super(name_, type_, options_)
|
54
|
+
if type_ == :spatial
|
55
|
+
col_ = self[name_]
|
56
|
+
col_.extend(SpatialColumnDefinitionMethods) unless col_.respond_to?(:geographic?)
|
57
|
+
options_.merge!(col_.limit) if col_.limit.is_a?(::Hash)
|
58
|
+
col_.set_spatial_type(options_[:type])
|
59
|
+
col_.set_geographic(options_[:geographic])
|
60
|
+
col_.set_srid((options_[:srid] || 4326).to_i)
|
61
|
+
col_.set_has_z(options_[:has_z])
|
62
|
+
col_.set_has_m(options_[:has_m])
|
63
|
+
end
|
64
|
+
self
|
65
|
+
end
|
66
|
+
|
67
|
+
def to_sql
|
68
|
+
@columns.find_all{ |c_| !c_.respond_to?(:geographic?) || c_.geographic? }.map{ |c_| c_.to_sql } * ', '
|
69
|
+
end
|
70
|
+
|
71
|
+
def non_geographic_spatial_columns
|
72
|
+
@columns.find_all{ |c_| c_.respond_to?(:geographic?) && !c_.geographic? }
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
module SpatialColumnDefinitionMethods
|
79
|
+
|
80
|
+
def spatial_type
|
81
|
+
defined?(@spatial_type) && @spatial_type
|
82
|
+
end
|
83
|
+
|
84
|
+
def geographic?
|
85
|
+
defined?(@geographic) && @geographic
|
86
|
+
end
|
87
|
+
|
88
|
+
def srid
|
89
|
+
defined?(@srid) ? @srid : 4326
|
90
|
+
end
|
91
|
+
|
92
|
+
def has_z?
|
93
|
+
defined?(@has_z) && @has_z
|
94
|
+
end
|
95
|
+
|
96
|
+
def has_m?
|
97
|
+
defined?(@has_m) && @has_m
|
98
|
+
end
|
99
|
+
|
100
|
+
def set_geographic(value_)
|
101
|
+
@geographic = value_ ? true : false
|
102
|
+
end
|
103
|
+
|
104
|
+
def set_spatial_type(value_)
|
105
|
+
@spatial_type = value_.to_s
|
106
|
+
end
|
107
|
+
|
108
|
+
def set_srid(value_)
|
109
|
+
@srid = value_
|
110
|
+
end
|
111
|
+
|
112
|
+
def set_has_z(value_)
|
113
|
+
@has_z = value_ ? true : false
|
114
|
+
end
|
115
|
+
|
116
|
+
def set_has_m(value_)
|
117
|
+
@has_m = value_ ? true : false
|
118
|
+
end
|
119
|
+
|
120
|
+
def sql_type
|
121
|
+
type_ = spatial_type.upcase.gsub('_', '')
|
122
|
+
type_ << 'Z' if has_z?
|
123
|
+
type_ << 'M' if has_m?
|
124
|
+
"GEOGRAPHY(#{type_},#{srid})"
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
135
|
+
|
136
|
+
# :startdoc:
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# PostGIS adapter for ActiveRecord
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2010 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
|
37
|
+
begin
|
38
|
+
require 'versionomy'
|
39
|
+
rescue ::LoadError
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
module ActiveRecord
|
44
|
+
|
45
|
+
module ConnectionAdapters
|
46
|
+
|
47
|
+
module PostGISAdapter
|
48
|
+
|
49
|
+
|
50
|
+
# Current version of PostGISAdapter as a frozen string
|
51
|
+
VERSION_STRING = ::File.read(::File.dirname(__FILE__)+'/../../../../Version').strip.freeze
|
52
|
+
|
53
|
+
# Current version of PostGISAdapter as a Versionomy object, if the
|
54
|
+
# Versionomy gem is available; otherwise equal to VERSION_STRING.
|
55
|
+
VERSION = defined?(::Versionomy) ? ::Versionomy.parse(VERSION_STRING) : VERSION_STRING
|
56
|
+
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -34,25 +34,6 @@
|
|
34
34
|
;
|
35
35
|
|
36
36
|
|
37
|
-
|
37
|
+
puts "WARNING: rgeo/active_record/postgis_adapter/railtie is deprecated. Please use active_record/connection_adapters/postgis_adapter/railtie."
|
38
38
|
|
39
|
-
|
40
|
-
module RGeo
|
41
|
-
|
42
|
-
module ActiveRecord
|
43
|
-
|
44
|
-
module PostgisAdapter
|
45
|
-
|
46
|
-
class Railtie < ::Rails::Railtie
|
47
|
-
|
48
|
-
rake_tasks do
|
49
|
-
load "rgeo/active_record/postgis_adapter/databases.rake"
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
39
|
+
require 'active_record/connection_adapters/postgis_adapter/railtie'
|
data/test/tc_basic.rb
CHANGED
@@ -66,6 +66,11 @@ module RGeo
|
|
66
66
|
end
|
67
67
|
|
68
68
|
|
69
|
+
def test_version
|
70
|
+
assert_not_nil(::ActiveRecord::ConnectionAdapters::PostGISAdapter::VERSION)
|
71
|
+
end
|
72
|
+
|
73
|
+
|
69
74
|
def test_postgis_available
|
70
75
|
connection_ = create_ar_class.connection
|
71
76
|
assert_equal('PostGIS', connection_.adapter_name)
|
@@ -73,57 +78,6 @@ module RGeo
|
|
73
78
|
end
|
74
79
|
|
75
80
|
|
76
|
-
def test_create_simple_geometry
|
77
|
-
klass_ = create_ar_class
|
78
|
-
klass_.connection.create_table(:spatial_test) do |t_|
|
79
|
-
t_.column 'latlon', :geometry
|
80
|
-
end
|
81
|
-
assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
82
|
-
col_ = klass_.columns.last
|
83
|
-
assert_equal(::RGeo::Feature::Geometry, col_.geometric_type)
|
84
|
-
assert_equal(false, col_.geographic?)
|
85
|
-
assert_equal(4326, col_.srid)
|
86
|
-
assert(klass_.cached_attributes.include?('latlon'))
|
87
|
-
klass_.connection.drop_table(:spatial_test)
|
88
|
-
assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
89
|
-
end
|
90
|
-
|
91
|
-
|
92
|
-
def test_create_simple_geography
|
93
|
-
klass_ = create_ar_class
|
94
|
-
klass_.connection.create_table(:spatial_test) do |t_|
|
95
|
-
t_.column 'latlon', :geometry, :geographic => true
|
96
|
-
end
|
97
|
-
col_ = klass_.columns.last
|
98
|
-
assert_equal(::RGeo::Feature::Geometry, col_.geometric_type)
|
99
|
-
assert_equal(true, col_.geographic?)
|
100
|
-
assert_equal(4326, col_.srid)
|
101
|
-
assert(klass_.cached_attributes.include?('latlon'))
|
102
|
-
assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
103
|
-
end
|
104
|
-
|
105
|
-
|
106
|
-
def test_create_point_geometry
|
107
|
-
klass_ = create_ar_class
|
108
|
-
klass_.connection.create_table(:spatial_test) do |t_|
|
109
|
-
t_.column 'latlon', :point
|
110
|
-
end
|
111
|
-
assert_equal(::RGeo::Feature::Point, klass_.columns.last.geometric_type)
|
112
|
-
assert(klass_.cached_attributes.include?('latlon'))
|
113
|
-
end
|
114
|
-
|
115
|
-
|
116
|
-
def test_create_geometry_with_index
|
117
|
-
klass_ = create_ar_class
|
118
|
-
klass_.connection.create_table(:spatial_test) do |t_|
|
119
|
-
t_.column 'latlon', :geometry
|
120
|
-
end
|
121
|
-
klass_.connection.change_table(:spatial_test) do |t_|
|
122
|
-
t_.index([:latlon], :spatial => true)
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
|
127
81
|
def test_set_and_get_point
|
128
82
|
klass_ = populate_ar_class(:latlon_point)
|
129
83
|
obj_ = klass_.new
|
@@ -182,91 +136,12 @@ module RGeo
|
|
182
136
|
end
|
183
137
|
|
184
138
|
|
185
|
-
def test_add_geometry_column
|
186
|
-
klass_ = create_ar_class
|
187
|
-
klass_.connection.create_table(:spatial_test) do |t_|
|
188
|
-
t_.column('latlon', :geometry)
|
189
|
-
end
|
190
|
-
klass_.connection.change_table(:spatial_test) do |t_|
|
191
|
-
t_.column('geom2', :point, :srid => 4326)
|
192
|
-
t_.column('name', :string)
|
193
|
-
end
|
194
|
-
assert_equal(2, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
195
|
-
cols_ = klass_.columns
|
196
|
-
assert_equal(::RGeo::Feature::Geometry, cols_[-3].geometric_type)
|
197
|
-
assert_equal(4326, cols_[-3].srid)
|
198
|
-
assert_equal(::RGeo::Feature::Point, cols_[-2].geometric_type)
|
199
|
-
assert_equal(4326, cols_[-2].srid)
|
200
|
-
assert_equal(false, cols_[-2].geographic?)
|
201
|
-
assert_nil(cols_[-1].geometric_type)
|
202
|
-
end
|
203
|
-
|
204
|
-
|
205
|
-
def test_add_geography_column
|
206
|
-
klass_ = create_ar_class
|
207
|
-
klass_.connection.create_table(:spatial_test) do |t_|
|
208
|
-
t_.column('latlon', :geometry)
|
209
|
-
end
|
210
|
-
klass_.connection.change_table(:spatial_test) do |t_|
|
211
|
-
t_.column('geom2', :point, :srid => 4326, :geographic => true)
|
212
|
-
t_.column('name', :string)
|
213
|
-
end
|
214
|
-
assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
215
|
-
cols_ = klass_.columns
|
216
|
-
assert_equal(::RGeo::Feature::Geometry, cols_[-3].geometric_type)
|
217
|
-
assert_equal(4326, cols_[-3].srid)
|
218
|
-
assert_equal(::RGeo::Feature::Point, cols_[-2].geometric_type)
|
219
|
-
assert_equal(4326, cols_[-2].srid)
|
220
|
-
assert_equal(true, cols_[-2].geographic?)
|
221
|
-
assert_nil(cols_[-1].geometric_type)
|
222
|
-
end
|
223
|
-
|
224
|
-
|
225
|
-
def test_drop_geometry_column
|
226
|
-
klass_ = create_ar_class
|
227
|
-
klass_.connection.create_table(:spatial_test) do |t_|
|
228
|
-
t_.column('latlon', :geometry)
|
229
|
-
t_.column('geom2', :point, :srid => 4326)
|
230
|
-
end
|
231
|
-
klass_.connection.change_table(:spatial_test) do |t_|
|
232
|
-
t_.remove('geom2')
|
233
|
-
end
|
234
|
-
assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
235
|
-
cols_ = klass_.columns
|
236
|
-
assert_equal(::RGeo::Feature::Geometry, cols_[-1].geometric_type)
|
237
|
-
assert_equal('latlon', cols_[-1].name)
|
238
|
-
assert_equal(4326, cols_[-1].srid)
|
239
|
-
assert_equal(false, cols_[-1].geographic?)
|
240
|
-
end
|
241
|
-
|
242
|
-
|
243
|
-
def test_drop_geography_column
|
244
|
-
klass_ = create_ar_class
|
245
|
-
klass_.connection.create_table(:spatial_test) do |t_|
|
246
|
-
t_.column('latlon', :geometry)
|
247
|
-
t_.column('geom2', :point, :srid => 4326, :geographic => true)
|
248
|
-
t_.column('geom3', :point, :srid => 4326)
|
249
|
-
end
|
250
|
-
klass_.connection.change_table(:spatial_test) do |t_|
|
251
|
-
t_.remove('geom2')
|
252
|
-
end
|
253
|
-
assert_equal(2, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
254
|
-
cols_ = klass_.columns
|
255
|
-
assert_equal(::RGeo::Feature::Point, cols_[-1].geometric_type)
|
256
|
-
assert_equal('geom3', cols_[-1].name)
|
257
|
-
assert_equal(false, cols_[-1].geographic?)
|
258
|
-
assert_equal(::RGeo::Feature::Geometry, cols_[-2].geometric_type)
|
259
|
-
assert_equal('latlon', cols_[-2].name)
|
260
|
-
assert_equal(false, cols_[-2].geographic?)
|
261
|
-
end
|
262
|
-
|
263
|
-
|
264
139
|
def test_readme_example
|
265
140
|
klass_ = create_ar_class
|
266
141
|
klass_.connection.create_table(:spatial_test) do |t_|
|
267
|
-
t_.column(:
|
268
|
-
t_.line_string(:path)
|
269
|
-
t_.
|
142
|
+
t_.column(:shape, :geometry)
|
143
|
+
t_.line_string(:path, :srid => 3785)
|
144
|
+
t_.point(:latlon, :geographic => true)
|
270
145
|
end
|
271
146
|
klass_.connection.change_table(:spatial_test) do |t_|
|
272
147
|
t_.index(:latlon, :spatial => true)
|
@@ -284,32 +159,6 @@ module RGeo
|
|
284
159
|
end
|
285
160
|
|
286
161
|
|
287
|
-
def test_query_point
|
288
|
-
klass_ = populate_ar_class(:latlon_point)
|
289
|
-
obj_ = klass_.new
|
290
|
-
obj_.latlon = @factory.point(1, 2)
|
291
|
-
obj_.save!
|
292
|
-
id_ = obj_.id
|
293
|
-
obj2_ = klass_.where(:latlon => @factory.multi_point([@factory.point(1, 2)])).first
|
294
|
-
assert_equal(id_, obj2_.id)
|
295
|
-
obj3_ = klass_.where(:latlon => @factory.point(2, 2)).first
|
296
|
-
assert_nil(obj3_)
|
297
|
-
end
|
298
|
-
|
299
|
-
|
300
|
-
def test_query_point_wkt
|
301
|
-
klass_ = populate_ar_class(:latlon_point)
|
302
|
-
obj_ = klass_.new
|
303
|
-
obj_.latlon = @factory.point(1, 2)
|
304
|
-
obj_.save!
|
305
|
-
id_ = obj_.id
|
306
|
-
obj2_ = klass_.where(:latlon => 'SRID=4326;POINT(1 2)').first
|
307
|
-
assert_equal(id_, obj2_.id)
|
308
|
-
obj3_ = klass_.where(:latlon => 'SRID=4326;POINT(2 2)').first
|
309
|
-
assert_nil(obj3_)
|
310
|
-
end
|
311
|
-
|
312
|
-
|
313
162
|
end
|
314
163
|
|
315
164
|
end
|
data/test/tc_ddl.rb
ADDED
@@ -0,0 +1,270 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Tests for the PostGIS ActiveRecord adapter
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2010 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
require 'test/unit'
|
37
|
+
require 'rgeo/active_record/adapter_test_helper'
|
38
|
+
|
39
|
+
|
40
|
+
module RGeo
|
41
|
+
module ActiveRecord # :nodoc:
|
42
|
+
module PostGISAdapter # :nodoc:
|
43
|
+
module Tests # :nodoc:
|
44
|
+
|
45
|
+
class TestDDL < ::Test::Unit::TestCase # :nodoc:
|
46
|
+
|
47
|
+
DATABASE_CONFIG_PATH = ::File.dirname(__FILE__)+'/database.yml'
|
48
|
+
include AdapterTestHelper
|
49
|
+
|
50
|
+
define_test_methods do
|
51
|
+
|
52
|
+
|
53
|
+
def test_create_simple_geometry
|
54
|
+
klass_ = create_ar_class
|
55
|
+
klass_.connection.create_table(:spatial_test) do |t_|
|
56
|
+
t_.column 'latlon', :geometry
|
57
|
+
end
|
58
|
+
assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
59
|
+
col_ = klass_.columns.last
|
60
|
+
assert_equal(::RGeo::Feature::Geometry, col_.geometric_type)
|
61
|
+
assert_equal(false, col_.geographic?)
|
62
|
+
assert_equal(4326, col_.srid)
|
63
|
+
assert(klass_.cached_attributes.include?('latlon'))
|
64
|
+
klass_.connection.drop_table(:spatial_test)
|
65
|
+
assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
def test_create_simple_geography
|
70
|
+
klass_ = create_ar_class
|
71
|
+
klass_.connection.create_table(:spatial_test) do |t_|
|
72
|
+
t_.column 'latlon', :geometry, :geographic => true
|
73
|
+
end
|
74
|
+
col_ = klass_.columns.last
|
75
|
+
assert_equal(::RGeo::Feature::Geometry, col_.geometric_type)
|
76
|
+
assert_equal(true, col_.geographic?)
|
77
|
+
assert_equal(4326, col_.srid)
|
78
|
+
assert(klass_.cached_attributes.include?('latlon'))
|
79
|
+
assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
def test_create_point_geometry
|
84
|
+
klass_ = create_ar_class
|
85
|
+
klass_.connection.create_table(:spatial_test) do |t_|
|
86
|
+
t_.column 'latlon', :point
|
87
|
+
end
|
88
|
+
assert_equal(::RGeo::Feature::Point, klass_.columns.last.geometric_type)
|
89
|
+
assert(klass_.cached_attributes.include?('latlon'))
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
def test_create_geometry_with_index
|
94
|
+
klass_ = create_ar_class
|
95
|
+
klass_.connection.create_table(:spatial_test) do |t_|
|
96
|
+
t_.column 'latlon', :geometry
|
97
|
+
end
|
98
|
+
klass_.connection.change_table(:spatial_test) do |t_|
|
99
|
+
t_.index([:latlon], :spatial => true)
|
100
|
+
end
|
101
|
+
assert(klass_.connection.indexes(:spatial_test).last.spatial)
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
def test_add_geometry_column
|
106
|
+
klass_ = create_ar_class
|
107
|
+
klass_.connection.create_table(:spatial_test) do |t_|
|
108
|
+
t_.column('latlon', :geometry)
|
109
|
+
end
|
110
|
+
klass_.connection.change_table(:spatial_test) do |t_|
|
111
|
+
t_.column('geom2', :point, :srid => 4326)
|
112
|
+
t_.column('name', :string)
|
113
|
+
end
|
114
|
+
assert_equal(2, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
115
|
+
cols_ = klass_.columns
|
116
|
+
assert_equal(::RGeo::Feature::Geometry, cols_[-3].geometric_type)
|
117
|
+
assert_equal(4326, cols_[-3].srid)
|
118
|
+
assert_equal(::RGeo::Feature::Point, cols_[-2].geometric_type)
|
119
|
+
assert_equal(4326, cols_[-2].srid)
|
120
|
+
assert_equal(false, cols_[-2].geographic?)
|
121
|
+
assert_nil(cols_[-1].geometric_type)
|
122
|
+
end
|
123
|
+
|
124
|
+
|
125
|
+
def test_add_geography_column
|
126
|
+
klass_ = create_ar_class
|
127
|
+
klass_.connection.create_table(:spatial_test) do |t_|
|
128
|
+
t_.column('latlon', :geometry)
|
129
|
+
end
|
130
|
+
klass_.connection.change_table(:spatial_test) do |t_|
|
131
|
+
t_.column('geom2', :point, :srid => 4326, :geographic => true)
|
132
|
+
t_.column('name', :string)
|
133
|
+
end
|
134
|
+
assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
135
|
+
cols_ = klass_.columns
|
136
|
+
assert_equal(::RGeo::Feature::Geometry, cols_[-3].geometric_type)
|
137
|
+
assert_equal(4326, cols_[-3].srid)
|
138
|
+
assert_equal(::RGeo::Feature::Point, cols_[-2].geometric_type)
|
139
|
+
assert_equal(4326, cols_[-2].srid)
|
140
|
+
assert_equal(true, cols_[-2].geographic?)
|
141
|
+
assert_nil(cols_[-1].geometric_type)
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
def test_drop_geometry_column
|
146
|
+
klass_ = create_ar_class
|
147
|
+
klass_.connection.create_table(:spatial_test) do |t_|
|
148
|
+
t_.column('latlon', :geometry)
|
149
|
+
t_.column('geom2', :point, :srid => 4326)
|
150
|
+
end
|
151
|
+
klass_.connection.change_table(:spatial_test) do |t_|
|
152
|
+
t_.remove('geom2')
|
153
|
+
end
|
154
|
+
assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
155
|
+
cols_ = klass_.columns
|
156
|
+
assert_equal(::RGeo::Feature::Geometry, cols_[-1].geometric_type)
|
157
|
+
assert_equal('latlon', cols_[-1].name)
|
158
|
+
assert_equal(4326, cols_[-1].srid)
|
159
|
+
assert_equal(false, cols_[-1].geographic?)
|
160
|
+
end
|
161
|
+
|
162
|
+
|
163
|
+
def test_drop_geography_column
|
164
|
+
klass_ = create_ar_class
|
165
|
+
klass_.connection.create_table(:spatial_test) do |t_|
|
166
|
+
t_.column('latlon', :geometry)
|
167
|
+
t_.column('geom2', :point, :srid => 4326, :geographic => true)
|
168
|
+
t_.column('geom3', :point, :srid => 4326)
|
169
|
+
end
|
170
|
+
klass_.connection.change_table(:spatial_test) do |t_|
|
171
|
+
t_.remove('geom2')
|
172
|
+
end
|
173
|
+
assert_equal(2, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
174
|
+
cols_ = klass_.columns
|
175
|
+
assert_equal(::RGeo::Feature::Point, cols_[-1].geometric_type)
|
176
|
+
assert_equal('geom3', cols_[-1].name)
|
177
|
+
assert_equal(false, cols_[-1].geographic?)
|
178
|
+
assert_equal(::RGeo::Feature::Geometry, cols_[-2].geometric_type)
|
179
|
+
assert_equal('latlon', cols_[-2].name)
|
180
|
+
assert_equal(false, cols_[-2].geographic?)
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
def test_create_simple_geometry_using_shortcut
|
185
|
+
klass_ = create_ar_class
|
186
|
+
klass_.connection.create_table(:spatial_test) do |t_|
|
187
|
+
t_.geometry 'latlon'
|
188
|
+
end
|
189
|
+
assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
190
|
+
col_ = klass_.columns.last
|
191
|
+
assert_equal(::RGeo::Feature::Geometry, col_.geometric_type)
|
192
|
+
assert_equal(false, col_.geographic?)
|
193
|
+
assert_equal(4326, col_.srid)
|
194
|
+
assert(klass_.cached_attributes.include?('latlon'))
|
195
|
+
klass_.connection.drop_table(:spatial_test)
|
196
|
+
assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
def test_create_simple_geography_using_shortcut
|
201
|
+
klass_ = create_ar_class
|
202
|
+
klass_.connection.create_table(:spatial_test) do |t_|
|
203
|
+
# t_.method_missing(:geometry, 'latlon', :geographic => true)
|
204
|
+
t_.geometry 'latlon', :geographic => true
|
205
|
+
end
|
206
|
+
col_ = klass_.columns.last
|
207
|
+
assert_equal(::RGeo::Feature::Geometry, col_.geometric_type)
|
208
|
+
assert_equal(true, col_.geographic?)
|
209
|
+
assert_equal(4326, col_.srid)
|
210
|
+
assert(klass_.cached_attributes.include?('latlon'))
|
211
|
+
assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
212
|
+
end
|
213
|
+
|
214
|
+
|
215
|
+
def test_create_point_geometry_using_shortcut
|
216
|
+
klass_ = create_ar_class
|
217
|
+
klass_.connection.create_table(:spatial_test) do |t_|
|
218
|
+
t_.point 'latlon'
|
219
|
+
end
|
220
|
+
assert_equal(::RGeo::Feature::Point, klass_.columns.last.geometric_type)
|
221
|
+
assert(klass_.cached_attributes.include?('latlon'))
|
222
|
+
end
|
223
|
+
|
224
|
+
|
225
|
+
def test_create_geometry_with_options
|
226
|
+
klass_ = create_ar_class
|
227
|
+
klass_.connection.create_table(:spatial_test) do |t_|
|
228
|
+
t_.column 'region', :polygon, :has_m => true, :srid => 3785
|
229
|
+
end
|
230
|
+
assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
231
|
+
col_ = klass_.columns.last
|
232
|
+
assert_equal(::RGeo::Feature::Polygon, col_.geometric_type)
|
233
|
+
assert_equal(false, col_.geographic?)
|
234
|
+
assert_equal(false, col_.has_z?)
|
235
|
+
assert_equal(true, col_.has_m?)
|
236
|
+
assert_equal(3785, col_.srid)
|
237
|
+
assert_equal({:has_m => true, :type => 'polygon', :srid => 3785}, col_.limit)
|
238
|
+
assert(klass_.cached_attributes.include?('region'))
|
239
|
+
klass_.connection.drop_table(:spatial_test)
|
240
|
+
assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
241
|
+
end
|
242
|
+
|
243
|
+
|
244
|
+
def test_create_geometry_using_limit
|
245
|
+
klass_ = create_ar_class
|
246
|
+
klass_.connection.create_table(:spatial_test) do |t_|
|
247
|
+
t_.spatial 'region', :limit => {:has_m => true, :srid => 3785, :type => :polygon}
|
248
|
+
end
|
249
|
+
assert_equal(1, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
250
|
+
col_ = klass_.columns.last
|
251
|
+
assert_equal(::RGeo::Feature::Polygon, col_.geometric_type)
|
252
|
+
assert_equal(false, col_.geographic?)
|
253
|
+
assert_equal(false, col_.has_z)
|
254
|
+
assert_equal(true, col_.has_m)
|
255
|
+
assert_equal(3785, col_.srid)
|
256
|
+
assert_equal({:has_m => true, :type => 'polygon', :srid => 3785}, col_.limit)
|
257
|
+
assert(klass_.cached_attributes.include?('region'))
|
258
|
+
klass_.connection.drop_table(:spatial_test)
|
259
|
+
assert_equal(0, klass_.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
|
260
|
+
end
|
261
|
+
|
262
|
+
|
263
|
+
end
|
264
|
+
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|