gotime-postgis_adapter 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,65 @@
1
+ class TablePoint < ActiveRecord::Base
2
+ end
3
+
4
+ class TableKeywordColumnPoint < ActiveRecord::Base
5
+ end
6
+
7
+ class TableLineString < ActiveRecord::Base
8
+ end
9
+
10
+ class TablePolygon < ActiveRecord::Base
11
+ end
12
+
13
+ class TableMultiPoint < ActiveRecord::Base
14
+ end
15
+
16
+ class TableMultiLineString < ActiveRecord::Base
17
+ end
18
+
19
+ class TableMultiPolygon < ActiveRecord::Base
20
+ end
21
+
22
+ class TableGeometry < ActiveRecord::Base
23
+ end
24
+
25
+ class TableGeometryCollection < ActiveRecord::Base
26
+ end
27
+
28
+ class Table3dzPoint < ActiveRecord::Base
29
+ end
30
+
31
+ class Table3dmPoint < ActiveRecord::Base
32
+ end
33
+
34
+ class Table4dPoint < ActiveRecord::Base
35
+ end
36
+
37
+ class TableSridLineString < ActiveRecord::Base
38
+ end
39
+
40
+ class TableSrid4dPolygon < ActiveRecord::Base
41
+ end
42
+
43
+ class City < ActiveRecord::Base
44
+ acts_as_geom :geom => :polygon
45
+ end
46
+
47
+ class Position < ActiveRecord::Base
48
+ acts_as_geom :geom => :point
49
+ end
50
+
51
+ class Street < ActiveRecord::Base
52
+ acts_as_geom :geom => :line_string
53
+ end
54
+
55
+ class Road < ActiveRecord::Base
56
+ acts_as_geom :geom => :multi_line_string
57
+ end
58
+
59
+ class CommonGeo < ActiveRecord::Base
60
+ acts_as_geom :geom => :point
61
+ end
62
+
63
+ class DiffName < ActiveRecord::Base
64
+ acts_as_geom :the_geom => :point
65
+ end
@@ -0,0 +1,98 @@
1
+ #add some postgis specific tables
2
+ ActiveRecord::Schema.define() do
3
+
4
+ create_table "table_points", :force => true do |t|
5
+ t.string "data"
6
+ t.point "geom", :null=>false, :srid => 4326
7
+ end
8
+
9
+ create_table "table_keyword_column_points", :force => true do |t|
10
+ t.point "location", :null => false, :srid => 4326
11
+ end
12
+
13
+ create_table "table_line_strings", :force => true do |t|
14
+ t.integer "value"
15
+ t.line_string "geom", :null=>false, :srid => 4326
16
+ end
17
+
18
+ create_table "table_polygons", :force => true do |t|
19
+ t.polygon "geom", :null=>false, :srid => 4326
20
+ end
21
+
22
+ create_table "table_multi_points", :force => true do |t|
23
+ t.multi_point "geom", :null=>false, :srid => 4326
24
+ end
25
+
26
+ create_table "table_multi_line_strings", :force => true do |t|
27
+ t.multi_line_string "geom", :null=>false, :srid => 4326
28
+ end
29
+
30
+ create_table "table_multi_polygons", :force => true do |t|
31
+ t.multi_polygon "geom", :null=>false, :srid => 4326
32
+ end
33
+
34
+ create_table "table_geometries", :force => true do |t|
35
+ t.geometry "geom", :null=>false, :srid => 4326
36
+ end
37
+
38
+ create_table "table_geometry_collections", :force => true do |t|
39
+ t.geometry_collection "geom", :null=>false, :srid => 4326
40
+ end
41
+
42
+ create_table "table3dz_points", :force => true do |t|
43
+ t.column "data", :string
44
+ t.point "geom", :null => false , :with_z => true, :srid => 4326
45
+ end
46
+
47
+ create_table "table3dm_points", :force => true do |t|
48
+ t.point "geom", :null => false , :with_m => true, :srid => 4326
49
+ end
50
+
51
+ create_table "table4d_points", :force => true do |t|
52
+ t.point "geom", :null => false, :with_m => true, :with_z => true, :srid => 4326
53
+ end
54
+
55
+ create_table "table_srid_line_strings", :force => true do |t|
56
+ t.line_string "geom", :null => false , :srid => 4326
57
+ end
58
+
59
+ create_table "table_srid4d_polygons", :force => true do |t|
60
+ t.polygon "geom", :with_m => true, :with_z => true, :srid => 4326
61
+ end
62
+
63
+ create_table :cities, :force => true do |t|
64
+ t.string :data, :limit => 100
65
+ t.integer :value
66
+ t.polygon :geom, :null => false, :srid => 4326
67
+ end
68
+
69
+ create_table :positions, :force => true do |t|
70
+ t.string :data, :limit => 100
71
+ t.integer :value
72
+ t.point :geom, :null => false, :srid => 4326
73
+ end
74
+
75
+ create_table :streets, :force => true do |t|
76
+ t.string :data, :limit => 100
77
+ t.integer :value
78
+ t.line_string :geom, :null => false, :srid => 4326
79
+ end
80
+
81
+ create_table :roads, :force => true do |t|
82
+ t.string :data, :limit => 100
83
+ t.integer :value
84
+ t.multi_line_string :geom, :null => false, :srid => 4326
85
+ end
86
+
87
+ create_table :common_geos, :force => true do |t|
88
+ t.string :data, :limit => 100
89
+ t.integer :value
90
+ t.point :geom, :null => false, :srid => 4326
91
+ end
92
+
93
+ create_table :diff_names, :force => true do |t|
94
+ t.string :data
95
+ t.point :the_geom, :null => false, :srid => 4326
96
+ end
97
+
98
+ end
@@ -0,0 +1,30 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ class DiffColumn < ActiveRecord::Base
4
+ acts_as_geom :ponto => :point
5
+ end
6
+
7
+ class NotInDb < ActiveRecord::Base
8
+ acts_as_geom :geom
9
+ end
10
+
11
+ describe "ActsAsGeom" do
12
+
13
+ it "should get the geom type" do
14
+ City.connection.columns("cities").select { |c| c.name == "geom" }[0]
15
+ City.get_geom_type(:geom).should eql(:polygon)
16
+ end
17
+
18
+ it "should get the geom type" do
19
+ Position.get_geom_type(:geom).should eql(:point)
20
+ end
21
+
22
+ it "should not interfere with migrations" do
23
+ NotInDb.get_geom_type(:geom).should be_nil
24
+ end
25
+
26
+ it "should query a diff column name" do
27
+ # DiffColumn
28
+ end
29
+
30
+ end
@@ -0,0 +1,254 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "CommonSpatialAdapter" do
4
+
5
+ class Park < ActiveRecord::Base; end
6
+ class Viewpark < ActiveRecord::Base; end
7
+
8
+ describe "Migration" do
9
+
10
+ before(:all) do
11
+ @connection = ActiveRecord::Base.connection
12
+ ActiveRecord::Schema.define do
13
+ create_table "parks", :force => true do |t|
14
+ t.string "data", :limit => 100
15
+ t.integer "value"
16
+ t.polygon "geom", :null => false, :srid => 4326 , :with_z => true, :with_m => true
17
+ end
18
+ end
19
+ end
20
+
21
+ it "should test_creation_modification" do
22
+ @connection.columns("parks").length.should eql(4) # the 3 defined + id
23
+ end
24
+
25
+ it "should test columns" do
26
+ @connection.columns("parks").each do |col|
27
+ if col.name == "geom"
28
+ col.class.should eql(ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn)
29
+ col.geometry_type.should eql(:polygon)
30
+ col.type.should eql(:geometry)
31
+ col.null.should be_false
32
+ col.srid.should eql(4326)
33
+ col.with_z.should be_true
34
+ col.with_m.should be_true
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "Add" do
40
+
41
+ before(:all)do
42
+ ActiveRecord::Schema.define do
43
+ add_column "parks","geom2", :multi_point, :srid => 4326
44
+ end
45
+ end
46
+
47
+ it "should test_creation_modification" do
48
+ @connection.columns("parks").length.should eql(5) # the 3 defined + id
49
+ end
50
+
51
+ it "should test columns" do
52
+ @connection.columns("parks").each do |col|
53
+ if col.name == "geom2"
54
+ col.class.should eql(ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn)
55
+ col.geometry_type.should eql(:multi_point)
56
+ col.type.should eql(:geometry)
57
+ col.null.should be_true
58
+ col.srid.should eql(4326)
59
+ col.with_z.should be_false
60
+ col.with_m.should be_false
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "remove" do
67
+ before(:all) do
68
+ ActiveRecord::Schema.define do
69
+ remove_column "parks","geom2"
70
+ end
71
+ end
72
+
73
+ it "should test_creation_modification" do
74
+ @connection.columns("parks").length.should eql(4) # the 3 defined + id
75
+ end
76
+
77
+ it "should get rid of the right one" do
78
+ @connection.columns("parks").each do |col|
79
+ violated if col.name == "geom2"
80
+ end
81
+ end
82
+ end
83
+
84
+ describe "indexes" do
85
+
86
+ it "should have 0 indexes" do
87
+ @connection.indexes("parks").length.should eql(0)
88
+ end
89
+
90
+ it "should create one" do
91
+ ActiveRecord::Schema.define do
92
+ add_index "parks","geom",:spatial=>true
93
+ end
94
+ @connection.indexes("parks").length.should eql(1)
95
+ @connection.indexes("parks")[0].spatial.should be_true
96
+ end
97
+
98
+ it "should remove too" do
99
+ ActiveRecord::Schema.define do
100
+ remove_index "parks", "geom"
101
+ end
102
+ @connection.indexes("parks").length.should eql(0)
103
+ end
104
+
105
+ it "should work with points" do
106
+ ActiveRecord::Schema.define do
107
+ remove_column "parks","geom2"
108
+ add_column "parks","geom2", :point
109
+ add_index "parks","geom2",:spatial=>true,:name => "example_spatial_index"
110
+ end
111
+ @connection.indexes("parks").length.should eql(1)
112
+ @connection.indexes("parks")[0].spatial.should be_true
113
+ @connection.indexes("parks")[0].name.should eql("example_spatial_index")
114
+ end
115
+
116
+ end
117
+
118
+ end
119
+
120
+ describe "Keywords" do
121
+
122
+ before(:all) do
123
+ ActiveRecord::Schema.define do
124
+ create_table "parks", :force => true do |t|
125
+ t.string "data", :limit => 100
126
+ t.integer "value"
127
+ #location is a postgreSQL keyword and is surrounded by double-quotes ("") when appearing in constraint descriptions ; tests a bug corrected in version 39
128
+ t.point "location", :null=>false,:srid => -1, :with_m => true, :with_z => true
129
+ end
130
+ end
131
+
132
+ @connection = ActiveRecord::Base.connection
133
+ @columns = @connection.columns("parks")
134
+ end
135
+
136
+ it "should get the columsn length" do
137
+ @connection.indexes("parks").length.should eql(0) # the 3 defined + id
138
+ end
139
+
140
+ it "should get the columns too" do
141
+ @connection.columns("parks").each do |col|
142
+ if col.name == "geom2"
143
+ col.class.should eql(ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn)
144
+ col.geometry_type.should eql(:point)
145
+ col.type.should eql(:geometry)
146
+ col.null.should be_true
147
+ col.srid.should eql(-1)
148
+ col.with_z.should be_false
149
+ col.with_m.should be_false
150
+ end
151
+ end
152
+ end
153
+ end
154
+
155
+ describe "Views" do
156
+
157
+ before(:all) do
158
+ ActiveRecord::Schema.define do
159
+ create_table "parks", :force => true do |t|
160
+ t.column "data" , :string, :limit => 100
161
+ t.column "value", :integer
162
+ t.column "geom", :point,:null=>false, :srid => 4326
163
+ end
164
+ end
165
+
166
+ Park.create!(:data => "Test", :geom => Point.from_x_y(1.2,4.5))
167
+
168
+ ActiveRecord::Base.connection.execute('CREATE VIEW viewparks as SELECT * from parks')
169
+ #if not ActiveRecord::Base.connection.execute("select * from geometry_columns where f_table_name = 'viewparks' and f_geometry_column = 'geom'") #do not add if already there
170
+ #mark the geom column in the view as geometric
171
+ #ActiveRecord::Base.connection.execute("insert into geometry_columns values ('','public','viewparks','geom',2,-1,'POINT')")
172
+ #end
173
+ end
174
+
175
+ it "should works" do
176
+ pt = Viewpark.find(:first)
177
+ pt.data.should eql("Test")
178
+ pt.geom.should == Point.from_x_y(1.2,4.5)
179
+ end
180
+
181
+ after(:all) do
182
+ ActiveRecord::Base.connection.execute('DROP VIEW viewparks')
183
+ end
184
+ end
185
+
186
+ describe "Dump" do
187
+ before(:all) do
188
+ #Force the creation of a table
189
+ ActiveRecord::Schema.define do
190
+ create_table "parks", :force => true do |t|
191
+ t.string "data" , :limit => 100
192
+ t.integer "value"
193
+ t.multi_polygon "geom", :null=>false,:srid => -1, :with_m => true, :with_z => true
194
+ end
195
+ add_index "parks","geom",:spatial=>true,:name => "example_spatial_index"
196
+ end
197
+ #dump it : tables from other tests will be dumped too but not a problem
198
+ File.open('schema.rb', "w") do |file|
199
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
200
+ end
201
+ #load it again
202
+ load('schema.rb')
203
+ #delete the schema file
204
+ File.delete('schema.rb')
205
+ @connection = ActiveRecord::Base.connection
206
+ @columns = @connection.columns("parks")
207
+ end
208
+
209
+ it "should create" do
210
+ @columns.length.should eql(4) # the 3 defined + id
211
+ end
212
+
213
+ it "should get the same stuff bakc" do
214
+ @columns.each do |col|
215
+ if col.name == "geom"
216
+ col.class.should eql(ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn)
217
+ col.geometry_type.should eql(:multi_polygon)
218
+ col.type.should eql(:geometry)
219
+ col.null.should be_false
220
+ col.srid.should eql(-1)
221
+ col.with_z.should be_true
222
+ col.with_m.should be_true
223
+ end
224
+ end
225
+ end
226
+
227
+ it "should get the indexes back too" do
228
+ @connection.indexes("parks").length.should eql(1)
229
+ @connection.indexes("parks")[0].spatial.should be_true
230
+ @connection.indexes("parks")[0].name.should eql("example_spatial_index")
231
+ end
232
+ end
233
+
234
+
235
+ describe "Fixtures" do
236
+
237
+ it "should test_long_fixture" do
238
+ Polygon.from_coordinates([[[144.857742,13.598263],
239
+ [144.862362,13.589922],[144.865169,13.587336],[144.862927,13.587665],
240
+ [144.861292,13.587321],[144.857597,13.585299],[144.847845,13.573858],
241
+ [144.846225,13.571014],[144.843605,13.566047],[144.842157,13.563831],
242
+ [144.841202,13.561991],[144.838305,13.556465],[144.834645,13.549919],
243
+ [144.834352,13.549395],[144.833825,13.548454],[144.831839,13.544451],
244
+ [144.830845,13.54081],[144.821543,13.545695],[144.8097993,13.55186285],
245
+ [144.814753,13.55755],[144.816744,13.56176944],[144.818862,13.566258],
246
+ [144.819402,13.568565],[144.822373,13.572223],[144.8242032,13.57381149],
247
+ [144.82634,13.575666],[144.83416,13.590365],[144.83514,13.595657],
248
+ [144.834284,13.59652],[144.834024,13.598031],[144.83719,13.598061],
249
+ [144.857742,13.598263]]]).to_fixture_format.split(/\s+/).should eql(["0103000020E61000000100000020000000FBCC599F721B62404ED026874F322B40056A3178981B6240BF61A2410A2E2B406B10E676AF1B6240E486DF4DB72C2B40BC7A15199D1B6240F701486DE22C2B40CE893DB48F1B62400E828E56B52C2B40BA84436F711B624054C37E4FAC2B2B407862D68B211B62408F183DB7D0252B40D8817346141B6240C51D6FF25B242B40BFB7E9CFFE1A624071FF91E9D0212B401EA33CF3F21A624024F1F274AE202B408EEA7420EB1A6240ED4ACB48BD1F2B4058E20165D31A6240BEBC00FBE81C2B40A3586E69B51A6240E6E5B0FB8E192B40452BF702B31A6240FE2B2B4D4A192B40CA32C4B1AE1A624084B872F6CE182B403291D26C9E1A62408B8C0E48C2162B4072E14048961A624014B35E0CE5142B403FA88B144A1A6240732EC55565172B405CBA38E0E9196240344179C48D1A2B402C2AE274121A624005C58F31771D2B40892650C4221A62406D62793EA01F2B40FDBD141E341A62405D328E91EC212B40DD088B8A381A6240EC4CA1F31A232B40A1832EE1501A6240BB09BE69FA242B4046A863DF5F1A6240F23C9F9ECA252B400D6C9560711A624099D6A6B1BD262B4034F44F70B11A6240F5673F52442E2B409B728577B91A624056444DF4F9302B406FF25B74B21A6240E1D1C6116B312B4088821953B01A624005FD851E31322B4039D1AE42CA1A6240AF06280D35322B40FBCC599F721B62404ED026874F322B40"])
250
+ end
251
+
252
+ end
253
+
254
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe "Point" do
4
+
5
+ before(:all) do
6
+ @c1 ||= City.create!(:data => "City1", :geom => Polygon.from_coordinates([[[12,45],[45,41],[4,1],[12,45]],[[2,5],[5,1],[14,1],[2,5]]],4326))
7
+ @c2 ||= City.create!(:data => "City1", :geom => Polygon.from_coordinates([[[22,66],[65,65],[20,10],[22,66]],[[10,15],[15,11],[34,14],[10,15]]],4326))
8
+ @c3 ||= City.create!(:data => "City3", :geom => 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))
9
+ @s1 ||= Street.create!(:data => "Street1", :geom => LineString.from_coordinates([[1,1],[2,2]],4326))
10
+ @s2 ||= Street.create!(:data => "Street2", :geom => LineString.from_coordinates([[4,4],[7,7]],4326))
11
+ @s3 ||= Street.create!(:data => "Street3", :geom => LineString.from_coordinates([[8,8],[18,18],[20,20],[25,25],[30,30],[38,38]],4326))
12
+ @s4 ||= Street.create!(:data => "Street3", :geom => LineString.from_coordinates([[10,8],[15,18]],4326))
13
+ @p1 ||= Position.create!(:data => "Point1", :geom => Point.from_x_y(1,1,4326))
14
+ @p2 ||= Position.create!(:data => "Point2", :geom => Point.from_x_y(5,5,4326))
15
+ @p3 ||= Position.create!(:data => "Point3", :geom => Point.from_x_y(8,8,4326))
16
+ end
17
+
18
+ describe "BBox operations" do
19
+
20
+ it "should check stricly left" do
21
+ @p1.bbox("<<", @c1).should be_true
22
+ end
23
+
24
+ it "should check stricly right" do
25
+ @p1.bbox(">>", @c1).should be_false
26
+ end
27
+
28
+ it { @p1.should be_strictly_left_of(@c1) }
29
+ it { @p1.should_not be_strictly_right_of(@c1) }
30
+ it { @p1.should_not be_overlaps_or_right_of(@c1) }
31
+ it { @p1.should be_overlaps_or_left_of(@c1) }
32
+ it { @p1.should_not be_completely_contained_by(@c1) }
33
+ it { @c2.completely_contains?(@p1).should be_false }
34
+ it { @p1.should be_overlaps_or_above(@c1) }
35
+ it { @p1.should be_overlaps_or_below(@c1) }
36
+ it { @p1.should_not be_strictly_above(@c1) }
37
+ it { @p1.should_not be_strictly_below(@c1) }
38
+ it { @p1.interacts_with?(@c1).should be_false }
39
+
40
+ it { @p1.binary_equal?(@c1).should be_false }
41
+ it { @p1.same_as?(@c1).should be_false }
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,79 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe "ClassMethods" do
4
+ before(:all) do
5
+ @c1 ||= City.create!(:data => "CityClass", :geom => Polygon.from_coordinates([[[12,45],[45,41],[4,1],[12,45]],[[2,5],[5,1],[14,1],[2,5]]],4326))
6
+ @c2 ||= City.create!(:data => "CityClass", :geom => Polygon.from_coordinates([[[10,10],[10,50],[50,50],[10,10]],[[2,5],[5,1],[14,1],[2,5]]],4326))
7
+ @s1 ||= Street.create!(:data => "StreetClass", :geom => LineString.from_coordinates([[1,1],[99,88]],4326))
8
+ @s2 ||= Street.create!(:data => "StreetClassTiny", :geom => LineString.from_coordinates([[1,1],[1.1,1.1]],4326))
9
+ @p1 ||= Position.create!(:data => "PointClass", :geom => Point.from_x_y(99,99,4326))
10
+ @p2 ||= Position.create!(:data => "PointClassClose", :geom => Point.from_x_y(99.9,99.9,4326))
11
+ @p3 ||= Position.create!(:data => "PointInsideCity", :geom => Point.from_x_y(15.0,15.0,4326))
12
+ end
13
+
14
+ after(:all) do
15
+ [City, Street, Position].each { |m| m.delete_all }
16
+ end
17
+
18
+ it "should find the closest other point" do
19
+ Position.close_to(Point.from_x_y(99,99,4326), :srid => 4326)[0].data.should == @p1.data
20
+ end
21
+
22
+ it "should find the closest other point and limit" do
23
+ Position.close_to(Point.from_x_y(99,99,4326), :limit => 2).should have(2).positions
24
+ end
25
+
26
+ it "should find the closest other point" do
27
+ Position.closest_to(Point.from_x_y(99,99,4326)).data.should == @p1.data
28
+ end
29
+
30
+ it "should sort by size" do
31
+ Street.by_length.first.data.should == "StreetClassTiny"
32
+ Street.by_length.last.data.should == "StreetClass"
33
+ end
34
+
35
+ it "largest" do
36
+ Street.longest.data.should == "StreetClass"
37
+ end
38
+
39
+ it "should sort by linestring length" do
40
+ Street.by_length.should be_instance_of(Array)
41
+ end
42
+
43
+ it "should sort by linestring length" do
44
+ Street.by_length(:limit => 2).should have(2).streets
45
+ end
46
+
47
+ it "should find the longest" do
48
+ Street.longest.should == @s1
49
+ end
50
+
51
+ it "should find all dwithin one" do
52
+ Position.all_within(@s1.geom).should be_instance_of(Array)
53
+ end
54
+
55
+ it "should find all dwithin one" do
56
+ City.by_perimeter.should be_instance_of(Array)
57
+ end
58
+
59
+ it "should sort by polygon area" do
60
+ City.by_area.should be_instance_of(Array)
61
+ end
62
+
63
+ it "should sort by all dwithin" do
64
+ City.all_dwithin(@s1.geom).should eql([@c1, @c2])
65
+ end
66
+
67
+ it "should find all within polygon" do
68
+ Position.all_within(@c1.geom).should eql([@p3])#Array)
69
+ end
70
+
71
+ it "should find all within polygon 2" do
72
+ Position.all_within(@c2.geom).should eql([])#Array)
73
+ end
74
+
75
+ it "should sort by all within" do
76
+ City.by_boundaries.should be_instance_of(Array)
77
+ end
78
+
79
+ end