spatial_adapter 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,218 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ class MigratedGeometryModel < ActiveRecord::Base
4
+ end
5
+
6
+ describe "Spatially-enabled Migrations" do
7
+ before :each do
8
+ postgis_connection
9
+ @connection = ActiveRecord::Base.connection
10
+ end
11
+
12
+ describe "creating tables" do
13
+ after :each do
14
+ @connection.drop_table "migrated_geometry_models"
15
+ end
16
+
17
+ SpatialAdapter.geometry_data_types.keys.each do |type|
18
+ it "should create #{type.to_s} columns" do
19
+ ActiveRecord::Schema.define do
20
+ create_table :migrated_geometry_models, :force => true do |t|
21
+ t.integer :extra
22
+ t.send(type, :geom)
23
+ end
24
+ end
25
+
26
+ geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
27
+ geom_column.should be_a(SpatialAdapter::SpatialColumn)
28
+ geom_column.geometry_type.should == type
29
+ geom_column.type.should == :geometry
30
+ geom_column.with_z.should == false
31
+ geom_column.with_m.should == false
32
+ geom_column.srid.should == -1
33
+ end
34
+ end
35
+
36
+ it "should create 3d (xyz) geometry columns" do
37
+ ActiveRecord::Schema.define do
38
+ create_table :migrated_geometry_models, :force => true do |t|
39
+ t.integer :extra
40
+ t.point :geom, :with_z => true
41
+ end
42
+ end
43
+
44
+ geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
45
+ geom_column.should be_a(SpatialAdapter::SpatialColumn)
46
+ geom_column.with_z.should == true
47
+ geom_column.with_m.should == false
48
+ geom_column.srid.should == -1
49
+ end
50
+
51
+
52
+ it "should create 3d (xym) geometry columns" do
53
+ ActiveRecord::Schema.define do
54
+ create_table :migrated_geometry_models, :force => true do |t|
55
+ t.integer :extra
56
+ t.point :geom, :with_m => true
57
+ end
58
+ end
59
+
60
+ geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
61
+ geom_column.should be_a(SpatialAdapter::SpatialColumn)
62
+ geom_column.geometry_type.should == :point
63
+ geom_column.type.should == :geometry
64
+ geom_column.with_z.should == false
65
+ geom_column.with_m.should == true
66
+ geom_column.srid.should == -1
67
+ end
68
+
69
+
70
+ it "should create 4d (xyzm) geometry columns" do
71
+ ActiveRecord::Schema.define do
72
+ create_table :migrated_geometry_models, :force => true do |t|
73
+ t.integer :extra
74
+ t.point :geom, :with_z => true, :with_m => true
75
+ end
76
+ end
77
+
78
+ geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
79
+ geom_column.should be_a(SpatialAdapter::SpatialColumn)
80
+ geom_column.geometry_type.should == :point
81
+ geom_column.type.should == :geometry
82
+ geom_column.with_z.should == true
83
+ geom_column.with_m.should == true
84
+ geom_column.srid.should == -1
85
+ end
86
+
87
+
88
+ it "should create GEOMETRY columns with specified SRID" do
89
+ ActiveRecord::Schema.define do
90
+ create_table :migrated_geometry_models, :force => true do |t|
91
+ t.integer :extra
92
+ t.geometry :geom, :srid => 4326
93
+ end
94
+ end
95
+
96
+ geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
97
+ geom_column.should be_a(SpatialAdapter::SpatialColumn)
98
+ geom_column.geometry_type.should == :geometry
99
+ geom_column.type.should == :geometry
100
+ geom_column.with_z.should == false
101
+ geom_column.with_m.should == false
102
+ geom_column.srid.should == 4326
103
+ end
104
+ end
105
+
106
+ describe "adding columns" do
107
+ before :each do
108
+ ActiveRecord::Schema.define do
109
+ create_table :migrated_geometry_models, :force => true do |t|
110
+ t.integer :extra
111
+ end
112
+ end
113
+ end
114
+
115
+ after :each do
116
+ @connection.drop_table "migrated_geometry_models"
117
+ end
118
+
119
+ SpatialAdapter.geometry_data_types.keys.each do |type|
120
+ it "should add #{type.to_s} columns" do
121
+ ActiveRecord::Schema.define do
122
+ add_column :migrated_geometry_models, :geom, type
123
+ end
124
+
125
+ geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
126
+ geom_column.should be_a(SpatialAdapter::SpatialColumn)
127
+ geom_column.geometry_type.should == type
128
+ geom_column.type.should == :geometry
129
+ geom_column.with_z.should == false
130
+ geom_column.with_m.should == false
131
+ geom_column.srid.should == -1
132
+ end
133
+ end
134
+
135
+ it "should add 3d (xyz) geometry columns" do
136
+ ActiveRecord::Schema.define do
137
+ add_column :migrated_geometry_models, :geom, :point, :with_z => true
138
+ end
139
+
140
+ geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
141
+ geom_column.should be_a(SpatialAdapter::SpatialColumn)
142
+ geom_column.with_z.should == true
143
+ geom_column.with_m.should == false
144
+ geom_column.srid.should == -1
145
+ end
146
+
147
+
148
+ it "should add 3d (xym) geometry columns" do
149
+ ActiveRecord::Schema.define do
150
+ add_column :migrated_geometry_models, :geom, :point, :with_m => true
151
+ end
152
+
153
+ geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
154
+ geom_column.should be_a(SpatialAdapter::SpatialColumn)
155
+ geom_column.geometry_type.should == :point
156
+ geom_column.type.should == :geometry
157
+ geom_column.with_z.should == false
158
+ geom_column.with_m.should == true
159
+ geom_column.srid.should == -1
160
+ end
161
+
162
+
163
+ it "should add 4d (xyzm) geometry columns" do
164
+ ActiveRecord::Schema.define do
165
+ add_column :migrated_geometry_models, :geom, :point, :with_z => true, :with_m => true
166
+ end
167
+
168
+ geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
169
+ geom_column.should be_a(SpatialAdapter::SpatialColumn)
170
+ geom_column.geometry_type.should == :point
171
+ geom_column.type.should == :geometry
172
+ geom_column.with_z.should == true
173
+ geom_column.with_m.should == true
174
+ geom_column.srid.should == -1
175
+ end
176
+
177
+ it "should add GEOMETRY columns with specified SRID" do
178
+ ActiveRecord::Schema.define do
179
+ add_column :migrated_geometry_models, :geom, :geometry, :srid => 4326
180
+ end
181
+
182
+ geom_column = @connection.columns(:migrated_geometry_models).select{|c| c.name == 'geom'}.first
183
+ geom_column.should be_a(SpatialAdapter::SpatialColumn)
184
+ geom_column.geometry_type.should == :geometry
185
+ geom_column.type.should == :geometry
186
+ geom_column.with_z.should == false
187
+ geom_column.with_m.should == false
188
+ geom_column.srid.should == 4326
189
+ end
190
+ end
191
+
192
+ describe "removing columns" do
193
+ before :each do
194
+ end
195
+
196
+ after :each do
197
+ @connection.drop_table "migrated_geometry_models"
198
+ end
199
+
200
+ SpatialAdapter.geometry_data_types.keys.each do |type|
201
+ it "should remove #{type.to_s} columns using DropGeometryColumn" do
202
+ ActiveRecord::Schema.define do
203
+ create_table :migrated_geometry_models, :force => true do |t|
204
+ t.integer :extra
205
+ t.send(type, :geom)
206
+ end
207
+ end
208
+
209
+ @connection.should_receive(:execute).with(/DropGeometryColumn(.*migrated_geometry_models.*geom)/)
210
+ ActiveRecord::Schema.define do
211
+ remove_column :migrated_geometry_models, :geom
212
+ end
213
+ @connection.should_receive(:execute).with(anything())
214
+ end
215
+ end
216
+ end
217
+
218
+ end
@@ -0,0 +1,136 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'db/postgis_raw'
3
+ require 'models/common'
4
+
5
+ describe "Spatially-enabled Models" do
6
+ before :each do
7
+ postgis_connection
8
+ @connection = ActiveRecord::Base.connection
9
+ end
10
+
11
+ describe "inserting records" do
12
+ it 'should save Point objects' do
13
+ model = PointModel.new(:extra => 'test', :geom => GeometryFactory.point)
14
+ @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.point.as_hex_ewkb))
15
+ model.save.should == true
16
+ end
17
+
18
+ it 'should save LineString objects' do
19
+ model = LineStringModel.new(:extra => 'test', :geom => GeometryFactory.line_string)
20
+ @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.line_string.as_hex_ewkb))
21
+ model.save.should == true
22
+ end
23
+
24
+ it 'should save Polygon objects' do
25
+ model = PolygonModel.new(:extra => 'test', :geom => GeometryFactory.polygon)
26
+ @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.polygon.as_hex_ewkb))
27
+ model.save.should == true
28
+ end
29
+
30
+ it 'should save MultiPoint objects' do
31
+ model = MultiPointModel.new(:extra => 'test', :geom => GeometryFactory.multi_point)
32
+ @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.multi_point.as_hex_ewkb))
33
+ model.save.should == true
34
+ end
35
+
36
+ it 'should save MultiLineString objects' do
37
+ model = MultiLineStringModel.new(:extra => 'test', :geom => GeometryFactory.multi_line_string)
38
+ @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.multi_line_string.as_hex_ewkb))
39
+ model.save.should == true
40
+ end
41
+
42
+ it 'should save MultiPolygon objects' do
43
+ model = MultiPolygonModel.new(:extra => 'test', :geom => GeometryFactory.multi_polygon)
44
+ @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.multi_polygon.as_hex_ewkb))
45
+ model.save.should == true
46
+ end
47
+
48
+ it 'should save GeometryCollection objects' do
49
+ model = GeometryCollectionModel.new(:extra => 'test', :geom => GeometryFactory.geometry_collection)
50
+ @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.geometry_collection.as_hex_ewkb))
51
+ model.save.should == true
52
+ end
53
+
54
+ it 'should save Geometry objects' do
55
+ model = GeometryModel.new(:extra => 'test', :geom => GeometryFactory.point)
56
+ @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.point.as_hex_ewkb))
57
+ model.save.should == true
58
+ end
59
+
60
+ it 'should save 3D Point (with Z coord) objects' do
61
+ model = PointzModel.new(:extra => 'test', :geom => GeometryFactory.pointz)
62
+ @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.pointz.as_hex_ewkb))
63
+ model.save.should == true
64
+ end
65
+
66
+ it 'should save 3D Point (with M coord) objects' do
67
+ model = PointmModel.new(:extra => 'test', :geom => GeometryFactory.pointm)
68
+ @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.pointm.as_hex_ewkb))
69
+ model.save.should == true
70
+ end
71
+
72
+ it 'should save 4D Point objects' do
73
+ model = Point4Model.new(:extra => 'test', :geom => GeometryFactory.point4)
74
+ @connection.should_receive(:select_value).with(Regexp.new(GeometryFactory.point4.as_hex_ewkb))
75
+ model.save.should == true
76
+ end
77
+ end
78
+
79
+ describe "finding records" do
80
+ it 'should retrieve Point objects' do
81
+ model = PointModel.create(:extra => 'test', :geom => GeometryFactory.point)
82
+ PointModel.find(model.id).geom.should == GeometryFactory.point
83
+ end
84
+
85
+ it 'should retrieve LineString objects' do
86
+ model = LineStringModel.create(:extra => 'test', :geom => GeometryFactory.line_string)
87
+ LineStringModel.find(model.id).geom.should == GeometryFactory.line_string
88
+ end
89
+
90
+ it 'should retrieve Polygon objects' do
91
+ model = PolygonModel.create(:extra => 'test', :geom => GeometryFactory.polygon)
92
+ PolygonModel.find(model.id).geom.should == GeometryFactory.polygon
93
+ end
94
+
95
+ it 'should retrieve MultiPoint objects' do
96
+ model = MultiPointModel.create(:extra => 'test', :geom => GeometryFactory.multi_point)
97
+ MultiPointModel.find(model.id).geom.should == GeometryFactory.multi_point
98
+ end
99
+
100
+ it 'should retrieve MultiLineString objects' do
101
+ model = MultiLineStringModel.create(:extra => 'test', :geom => GeometryFactory.multi_line_string)
102
+ MultiLineStringModel.find(model.id).geom.should == GeometryFactory.multi_line_string
103
+ end
104
+
105
+ it 'should retrieve MultiPolygon objects' do
106
+ model = MultiPolygonModel.create(:extra => 'test', :geom => GeometryFactory.multi_polygon)
107
+ MultiPolygonModel.find(model.id).geom.should == GeometryFactory.multi_polygon
108
+ end
109
+
110
+ it 'should retrieve GeometryCollection objects' do
111
+ model = GeometryCollectionModel.create(:extra => 'test', :geom => GeometryFactory.geometry_collection)
112
+ GeometryCollectionModel.find(model.id).geom.should == GeometryFactory.geometry_collection
113
+ end
114
+
115
+ it 'should retrieve Geometry objects' do
116
+ model = GeometryModel.create(:extra => 'test', :geom => GeometryFactory.point)
117
+ GeometryModel.find(model.id).geom.should == GeometryFactory.point
118
+ end
119
+
120
+ it 'should retrieve 3D Point (with Z coord) objects' do
121
+ model = PointzModel.create(:extra => 'test', :geom => GeometryFactory.pointz)
122
+ PointzModel.find(model.id).geom.should == GeometryFactory.pointz
123
+ end
124
+
125
+ it 'should retrieve 3D Point (with M coord) objects' do
126
+ model = PointmModel.create(:extra => 'test', :geom => GeometryFactory.pointm)
127
+ PointmModel.find(model.id).geom.should == GeometryFactory.pointm
128
+ end
129
+
130
+ it 'should retrieve 4D Point objects' do
131
+ model = Point4Model.create(:extra => 'test', :geom => GeometryFactory.point4)
132
+ Point4Model.find(model.id).geom.should == GeometryFactory.point4
133
+ end
134
+ end
135
+ end
136
+
@@ -0,0 +1,58 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Spatially-enabled Schema Dumps" do
4
+ before :all do
5
+ postgis_connection
6
+ @connection = ActiveRecord::Base.connection
7
+
8
+ # Create a new table
9
+ ActiveRecord::Schema.define do
10
+ create_table :migrated_geometry_models, :force => true do |t|
11
+ t.integer :extra
12
+ t.point :geom, :with_m => true, :with_z => true, :srid => 4326
13
+ end
14
+ add_index :migrated_geometry_models, :geom, :spatial => true, :name => 'test_spatial_index'
15
+ end
16
+
17
+ File.open('schema.rb', "w") do |file|
18
+ ActiveRecord::SchemaDumper.dump(@connection, file)
19
+ end
20
+
21
+ # Drop the original table
22
+ @connection.execute "drop table migrated_geometry_models"
23
+
24
+ # Load the dumped schema
25
+ load('schema.rb')
26
+ end
27
+
28
+ after :all do
29
+ # delete the schema file
30
+ File.delete('schema.rb')
31
+
32
+ # Drop the new table
33
+ @connection.execute "drop table migrated_geometry_models"
34
+ end
35
+
36
+ it "should preserve spatial attributes of tables" do
37
+ columns = @connection.columns("migrated_geometry_models")
38
+
39
+ columns.should have(3).items
40
+ geom_column = columns.select{|c| c.name == 'geom'}.first
41
+ geom_column.should be_a(SpatialAdapter::SpatialColumn)
42
+ geom_column.geometry_type.should == :point
43
+ geom_column.type.should == :geometry
44
+ geom_column.with_z.should == true
45
+ geom_column.with_m.should == true
46
+ geom_column.srid.should == 4326
47
+ end
48
+
49
+ it "should preserve spatial indexes" do
50
+ indexes = @connection.indexes("migrated_geometry_models")
51
+
52
+ indexes.should have(1).item
53
+
54
+ indexes.first.name.should == 'test_spatial_index'
55
+ indexes.first.columns.should == ["geom"]
56
+ indexes.first.spatial.should == true
57
+ end
58
+ end
@@ -0,0 +1,73 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ gem 'activerecord', '=2.3.5'
4
+
5
+ $:.unshift((File.join(File.dirname(__FILE__), '..', 'lib')))
6
+ require 'spatial_adapter'
7
+
8
+ include GeoRuby::SimpleFeatures
9
+
10
+ # Don't output migration logging
11
+ ActiveRecord::Migration.verbose = false
12
+
13
+ def postgis_connection
14
+ ActiveRecord::Base.establish_connection(
15
+ :adapter => 'postgresql',
16
+ :database => 'spatial_adapter'
17
+ )
18
+ # Turn off those annoying NOTICE messages
19
+ ActiveRecord::Base.connection.execute 'set client_min_messages = warning'
20
+ end
21
+
22
+ def mysql_connection
23
+ ActiveRecord::Base.establish_connection(
24
+ :adapter => 'mysql',
25
+ :database => 'spatial_adapter',
26
+ :username => 'root',
27
+ :host => 'localhost'
28
+ )
29
+ end
30
+
31
+ class GeometryFactory
32
+ class << self
33
+ def point
34
+ Point.from_x_y(1, 2, 4326)
35
+ end
36
+
37
+ def line_string
38
+ LineString.from_coordinates([[1.4,2.5],[1.5,6.7]], 4326)
39
+ end
40
+
41
+ def polygon
42
+ Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]], 4326)
43
+ end
44
+
45
+ def multi_point
46
+ MultiPoint.from_coordinates([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]], 4326)
47
+ end
48
+
49
+ def multi_line_string
50
+ MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012]]),LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012],[45.123,123.3]])], 4326)
51
+ end
52
+
53
+ def multi_polygon
54
+ MultiPolygon.from_polygons([Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]]),Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]])], 4326)
55
+ end
56
+
57
+ def geometry_collection
58
+ GeometryCollection.from_geometries([Point.from_x_y(4.67,45.4),LineString.from_coordinates([[5.7,12.45],[67.55,54]])], 4326)
59
+ end
60
+
61
+ def pointz
62
+ Point.from_x_y_z(1, 2, 3, 4326)
63
+ end
64
+
65
+ def pointm
66
+ Point.from_x_y_m(1, 2, 3, 4326)
67
+ end
68
+
69
+ def point4
70
+ Point.from_x_y_z_m(1, 2, 3, 4, 4326)
71
+ end
72
+ end
73
+ end