ppe-postgis-adapter 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,224 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.dirname(__FILE__) + '/spec_helper.rb'
3
+
4
+ describe "PostgisAdapter" do
5
+
6
+ describe "Point" do
7
+ it "should record a point nicely" do
8
+ pt = TablePoint.new(:data => "Test", :geom => Point.from_x_y(1.2,4.5))
9
+ pt.save.should be_true
10
+ end
11
+
12
+ it "should find a point nicely" do
13
+ find = TablePoint.find(:last)
14
+ find.should be_instance_of(TablePoint)
15
+ find.geom.should be_instance_of(Point)
16
+ end
17
+
18
+ it "should find`em all for hellsake..." do
19
+ find = TablePoint.all
20
+ find.should be_instance_of(Array)
21
+ find.last.geom.x.should eql(1.2)
22
+ end
23
+
24
+ it "should est_3dz_points" do
25
+ pt = Table3dzPoint.create!(:data => "Hello!",:geom => Point.from_x_y_z(-1.6,2.8,-3.4))
26
+ pt = Table3dzPoint.find(:first)
27
+ pt.geom.should be_instance_of(Point)
28
+ pt.geom.z.should eql(-3.4)
29
+ end
30
+
31
+ it "should est_3dm_points" do
32
+ pt = Table3dmPoint.create!(:geom => Point.from_x_y_m(-1.6,2.8,-3.4))
33
+ pt = Table3dmPoint.find(:first)
34
+ pt.geom.should == Point.from_x_y_m(-1.6,2.8,-3.4)
35
+ pt.geom.m.should eql(-3.4)
36
+ end
37
+
38
+ it "should est_4d_points" do
39
+ pt = Table4dPoint.create!(:geom => Point.from_x_y_z_m(-1.6,2.8,-3.4,15))
40
+ pt = Table4dPoint.find(:first)
41
+ pt.geom.should be_instance_of(Point)
42
+ pt.geom.z.should eql(-3.4)
43
+ pt.geom.m.should eql(15.0)
44
+ end
45
+
46
+ it "should test_keyword_column_point" do
47
+ pt = TableKeywordColumnPoint.create!(:location => Point.from_x_y(1.2,4.5))
48
+ find = TableKeywordColumnPoint.find(:first)
49
+ find.location.should == Point.from_x_y(1.2,4.5)
50
+ end
51
+
52
+ it "should test multipoint" do
53
+ mp = TableMultiPoint.create!(:geom => MultiPoint.from_coordinates([[12.4,-4326.3],[-65.1,4326.4],[4326.55555555,4326]]))
54
+ find = TableMultiPoint.find(:first)
55
+ find.geom.should == MultiPoint.from_coordinates([[12.4,-4326.3],[-65.1,4326.4],[4326.55555555,4326]])
56
+ end
57
+
58
+ end
59
+
60
+ describe "LineString" do
61
+ it "should record a linestring nicely" do
62
+ @ls = TableLineString.new(:value => 3, :geom => LineString.from_coordinates([[1.4,2.5],[1.5,6.7]]))
63
+ @ls.save.should be_true
64
+ end
65
+
66
+ it "should find" do
67
+ find = TableLineString.find(:first)
68
+ find.geom.should be_instance_of(LineString)
69
+ find.geom.points.first.y.should eql(2.5)
70
+ end
71
+
72
+ it "should test_srid_line_string" do
73
+ ls = TableSridLineString.create!(:geom => LineString.from_coordinates([[1.4,2.5],[1.5,6.7]],4326))
74
+ ls = TableSridLineString.find(:first)
75
+ ls_e = LineString.from_coordinates([[1.4,2.5],[1.5,6.7]],4326)
76
+ ls.geom.should be_instance_of(LineString)
77
+ ls.geom.srid.should eql(4326)
78
+ end
79
+
80
+ it "hsould test_multi_line_string" do
81
+ ml = TableMultiLineString.create!(:geom => MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.432612,-0.012]]),LineString.from_coordinates([[1.5,45.2],[-54.432612,-0.012],[45.4326,4326.3]])]))
82
+ find = TableMultiLineString.find(:first)
83
+ find.geom.should == MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.432612,-0.012]]),LineString.from_coordinates([[1.5,45.2],[-54.432612,-0.012],[45.4326,4326.3]])])
84
+ end
85
+ end
86
+
87
+ describe "Polygon" do
88
+
89
+ it "should create" do
90
+ pg = TablePolygon.new(: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]]]))
91
+ pg.save.should be_true
92
+ end
93
+
94
+ it "should get it back" do
95
+ pg = TablePolygon.find(:first)
96
+ pg.geom.should == 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]]])
97
+ end
98
+
99
+ it "should test_multi_polygon" do
100
+ mp = TableMultiPolygon.create!( :geom => 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]]])]))
101
+ find = TableMultiPolygon.find(:first)
102
+ find.geom.should == 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]]])])
103
+ end
104
+
105
+ it "should test_srid_4d_polygon" do
106
+ pg = TableSrid4dPolygon.create(:geom => Polygon.from_coordinates([[[0,0,2,-45.1],[4,0,2,5],[4,4,2,4.67],[0,4,2,1.34],[0,0,2,-45.1]],[[1,1,2,12.3],[3,1,2,4326],[3,3,2,12.2],[1,3,2,12],[1,1,2,12.3]]],4326,true,true))
107
+ find = TableSrid4dPolygon.find(:first)
108
+ pg_e = Polygon.from_coordinates([[[0,0,2,-45.1],[4,0,2,5],[4,4,2,4.67],[0,4,2,1.34],[0,0,2,-45.1]],[[1,1,2,12.3],[3,1,2,4326],[3,3,2,12.2],[1,3,2,12],[1,1,2,12.3]]],4326,true,true)
109
+ pg.geom.should == pg_e
110
+ pg.geom.srid.should eql(4326)
111
+ end
112
+ end
113
+
114
+ describe "Geometry" do
115
+
116
+ it "should test_geometry" do
117
+ gm = TableGeometry.create!(:geom => LineString.from_coordinates([[12.4,-45.3],[45.4,41.6],[4.456,1.0698]]))
118
+ find = TableGeometry.find(:first)
119
+ find.geom.should == LineString.from_coordinates([[12.4,-45.3],[45.4,41.6],[4.456,1.0698]])
120
+ end
121
+
122
+ it "should test_geometry_collection" do
123
+ gc = TableGeometryCollection.create!(:geom => GeometryCollection.from_geometries([Point.from_x_y(4.67,45.4),LineString.from_coordinates([[5.7,12.45],[67.55,54]])]))
124
+ find = TableGeometryCollection.find(:first)
125
+ find.geom.should == GeometryCollection.from_geometries([Point.from_x_y(4.67,45.4),LineString.from_coordinates([[5.7,12.45],[67.55,54]])])
126
+ end
127
+
128
+ end
129
+
130
+ describe "Find" do
131
+
132
+ ActiveRecord::Schema.define() do
133
+ create_table :areas, :force => true do |t|
134
+ t.string :data, :limit => 100
135
+ t.integer :value
136
+ t.point :geom, :null => false, :srid => 4326
137
+ end
138
+ add_index :areas, :geom, :spatial => true, :name => "areas_spatial_index"
139
+ end
140
+
141
+ class Area < ActiveRecord::Base
142
+ end
143
+
144
+ it "should create some points" do
145
+ Area.create!(:data => "Point1", :geom => Point.from_x_y(1.2,0.75,4326))
146
+ Area.create!(:data => "Point2",:geom => Point.from_x_y(0.6,1.3,4326))
147
+ Area.create!(:data => "Point3", :geom => Point.from_x_y(2.5,2,4326))
148
+ end
149
+
150
+ it "should find by geom" do
151
+ pts = Area.find_all_by_geom(LineString.from_coordinates([[0,0],[2,2]],4326))
152
+ pts.should be_instance_of(Array)
153
+ pts.length.should eql(2)
154
+ pts[0].data.should match(/Point/)
155
+ pts[1].data.should match(/Point/)
156
+ end
157
+
158
+ it "should find by geom again" do
159
+ pts = Area.find_all_by_geom(LineString.from_coordinates([[2.49,1.99],[2.51,2.01]],4326))
160
+ pts[0].data.should eql("Point3")
161
+ end
162
+
163
+ it "should find by geom column bbox condition" do
164
+ pts = Area.find_all_by_geom([[0,0],[2,2],4326])
165
+ pts.should be_instance_of(Array)
166
+ pts.length.should eql(2)
167
+ pts[0].data.should match(/Point/)
168
+ pts[1].data.should match(/Point/)
169
+ end
170
+
171
+ it "should not mess with rails finder" do
172
+ pts = Area.find_all_by_data "Point1"
173
+ pts.should have(1).park
174
+ end
175
+
176
+ end
177
+
178
+ describe "PostgreSQL-specific types and default values" do
179
+
180
+ # Verify that a non-NULL column with a default value is handled correctly.
181
+ # Additionally, if the database uses UTF8 encoding, set the binary (bytea)
182
+ # column value to an illegal UTF8 string; it should be stored as the
183
+ # specified binary string and not as a text string. (The binary data test
184
+ # cannot fail if the database uses SQL_ASCII or LATIN1 encoding.)
185
+
186
+ ActiveRecord::Schema.define() do
187
+ create_table :binary_defaults, :force => true do |t|
188
+ t.string :name, :null => false
189
+ t.string :data, :null => false, :default => ''
190
+ t.binary :value
191
+ end
192
+ end
193
+
194
+ class BinaryDefault < ActiveRecord::Base
195
+ end
196
+
197
+ it "should create some records" do
198
+ if BinaryDefault.connection.encoding == "UTF8"
199
+ BinaryDefault.create!(:name => "foo", :data => "baz",
200
+ :value => "f\xf4o") # fôo as ISO-8859-1 (i.e., not valid UTF-8 data)
201
+ BinaryDefault.create!(:name => "bar", # data value not specified, should use default
202
+ :value => "b\xe5r") # bår as ISO-8859-1 (i.e., not valid UTF-8 data)
203
+ else
204
+ BinaryDefault.create!(:name => "foo", :data => "baz")
205
+ BinaryDefault.create!(:name => "bar")
206
+ end
207
+ end
208
+
209
+ it "should find the records" do
210
+ foo = BinaryDefault.find_by_name("foo")
211
+ bar = BinaryDefault.find_by_name("bar")
212
+
213
+ foo.data.should eql("baz")
214
+ bar.data.should eql("")
215
+
216
+ if BinaryDefault.connection.encoding == "UTF8"
217
+ foo.value.should eql("f\xf4o")
218
+ bar.value.should eql("b\xe5r")
219
+ end
220
+ end
221
+
222
+ end
223
+
224
+ 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,65 @@
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
+ @s1 ||= Street.create!(:data => "StreetClass", :geom => LineString.from_coordinates([[1,1],[99,88]],4326))
7
+ @s2 ||= Street.create!(:data => "StreetClassTiny", :geom => LineString.from_coordinates([[1,1],[1.1,1.1]],4326))
8
+ @p1 ||= Position.create!(:data => "PointClass", :geom => Point.from_x_y(99,99,4326))
9
+ @p2 ||= Position.create!(:data => "PointClassClose", :geom => Point.from_x_y(99.9,99.9,4326))
10
+ end
11
+
12
+ it "should find the closest other point" do
13
+ Position.close_to(Point.from_x_y(99,99,4326), :srid => 4326)[0].data.should == @p1.data
14
+ end
15
+
16
+ it "should find the closest other point and limit" do
17
+ Position.close_to(Point.from_x_y(99,99,4326), :limit => 2).should have(2).positions
18
+ end
19
+
20
+ it "should find the closest other point" do
21
+ Position.closest_to(Point.from_x_y(99,99,4326)).data.should == @p1.data
22
+ end
23
+
24
+ it "should sort by size" do
25
+ Street.by_length.first.data.should == "StreetClassTiny"
26
+ Street.by_length.last.data.should == "StreetClass"
27
+ end
28
+
29
+ it "largest" do
30
+ Street.longest.data.should == "StreetClass"
31
+ end
32
+
33
+ it "should sort by linestring length" do
34
+ Street.by_length.should be_instance_of(Array)
35
+ end
36
+
37
+ it "should sort by linestring length" do
38
+ Street.by_length(:limit => 2).should have(2).streets
39
+ end
40
+
41
+ it "should find the longest" do
42
+ Street.longest.should == @s1
43
+ end
44
+
45
+ it "should find all dwithin one" do
46
+ Position.all_within(@s1.geom).should be_instance_of(Array)
47
+ end
48
+
49
+ it "should find all dwithin one" do
50
+ City.by_perimeter.should be_instance_of(Array)
51
+ end
52
+
53
+ it "should sort by polygon area" do
54
+ City.by_area.should be_instance_of(Array)
55
+ end
56
+
57
+ it "should sort by all within" do
58
+ City.all_within(@s1.geom).should be_instance_of(Array)
59
+ end
60
+
61
+ it "should sort by all within" do
62
+ City.by_boundaries.should be_instance_of(Array)
63
+ end
64
+
65
+ end