georuby 1.9.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +8 -0
- data/Gemfile.lock +29 -0
- data/History.txt +4 -0
- data/LICENSE +21 -0
- data/README.rdoc +184 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/georuby.gemspec +128 -0
- data/lib/geo_ruby.rb +23 -0
- data/lib/geo_ruby/geojson.rb +129 -0
- data/lib/geo_ruby/georss.rb +133 -0
- data/lib/geo_ruby/gpx.rb +1 -0
- data/lib/geo_ruby/gpx4r/gpx.rb +118 -0
- data/lib/geo_ruby/shp.rb +1 -0
- data/lib/geo_ruby/shp4r/dbf.rb +42 -0
- data/lib/geo_ruby/shp4r/shp.rb +718 -0
- data/lib/geo_ruby/simple_features/envelope.rb +167 -0
- data/lib/geo_ruby/simple_features/ewkb_parser.rb +218 -0
- data/lib/geo_ruby/simple_features/ewkt_parser.rb +336 -0
- data/lib/geo_ruby/simple_features/geometry.rb +236 -0
- data/lib/geo_ruby/simple_features/geometry_collection.rb +144 -0
- data/lib/geo_ruby/simple_features/geometry_factory.rb +81 -0
- data/lib/geo_ruby/simple_features/helper.rb +18 -0
- data/lib/geo_ruby/simple_features/line_string.rb +228 -0
- data/lib/geo_ruby/simple_features/linear_ring.rb +34 -0
- data/lib/geo_ruby/simple_features/multi_line_string.rb +63 -0
- data/lib/geo_ruby/simple_features/multi_point.rb +58 -0
- data/lib/geo_ruby/simple_features/multi_polygon.rb +64 -0
- data/lib/geo_ruby/simple_features/point.rb +381 -0
- data/lib/geo_ruby/simple_features/polygon.rb +175 -0
- data/nofxx-georuby.gemspec +149 -0
- data/spec/data/geojson/feature_collection.json +34 -0
- data/spec/data/georss/atom.xml +21 -0
- data/spec/data/georss/gml.xml +40 -0
- data/spec/data/georss/w3c.xml +22 -0
- data/spec/data/gpx/fells_loop.gpx +1077 -0
- data/spec/data/gpx/long.gpx +1642 -0
- data/spec/data/gpx/long.kml +31590 -0
- data/spec/data/gpx/long.nmea +2220 -0
- data/spec/data/gpx/short.gpx +13634 -0
- data/spec/data/gpx/short.kml +130 -0
- data/spec/data/gpx/tracktreks.gpx +706 -0
- data/spec/data/multipoint.dbf +0 -0
- data/spec/data/multipoint.shp +0 -0
- data/spec/data/multipoint.shx +0 -0
- data/spec/data/point.dbf +0 -0
- data/spec/data/point.shp +0 -0
- data/spec/data/point.shx +0 -0
- data/spec/data/polygon.dbf +0 -0
- data/spec/data/polygon.shp +0 -0
- data/spec/data/polygon.shx +0 -0
- data/spec/data/polyline.dbf +0 -0
- data/spec/data/polyline.shp +0 -0
- data/spec/data/polyline.shx +0 -0
- data/spec/geo_ruby/geojson_spec.rb +147 -0
- data/spec/geo_ruby/georss.rb +218 -0
- data/spec/geo_ruby/georss_spec.rb +14 -0
- data/spec/geo_ruby/gpx4r/gpx_spec.rb +106 -0
- data/spec/geo_ruby/shp4r/shp_spec.rb +239 -0
- data/spec/geo_ruby/simple_features/envelope_spec.rb +47 -0
- data/spec/geo_ruby/simple_features/ewkb_parser_spec.rb +158 -0
- data/spec/geo_ruby/simple_features/ewkt_parser_spec.rb +179 -0
- data/spec/geo_ruby/simple_features/geometry_collection_spec.rb +55 -0
- data/spec/geo_ruby/simple_features/geometry_factory_spec.rb +11 -0
- data/spec/geo_ruby/simple_features/geometry_spec.rb +32 -0
- data/spec/geo_ruby/simple_features/line_string_spec.rb +259 -0
- data/spec/geo_ruby/simple_features/linear_ring_spec.rb +24 -0
- data/spec/geo_ruby/simple_features/multi_line_string_spec.rb +54 -0
- data/spec/geo_ruby/simple_features/multi_point_spec.rb +35 -0
- data/spec/geo_ruby/simple_features/multi_polygon_spec.rb +50 -0
- data/spec/geo_ruby/simple_features/point_spec.rb +356 -0
- data/spec/geo_ruby/simple_features/polygon_spec.rb +122 -0
- data/spec/geo_ruby_spec.rb +27 -0
- data/spec/spec_helper.rb +73 -0
- metadata +228 -0
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe MultiPolygon do
|
4
|
+
|
5
|
+
it "test_multi_polygon_creation" do
|
6
|
+
multi_polygon1 = 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]]],256),Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)],256)
|
7
|
+
multi_polygon1.should be_instance_of(MultiPolygon)
|
8
|
+
multi_polygon1.length.should eql(2)
|
9
|
+
multi_polygon1[0].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]]],256)
|
10
|
+
|
11
|
+
multi_polygon2 = MultiPolygon.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]]],[[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]]],256)
|
12
|
+
multi_polygon1.should be_instance_of(MultiPolygon)
|
13
|
+
multi_polygon1.length.should eql(2)
|
14
|
+
|
15
|
+
multi_polygon2[0].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]]],256)
|
16
|
+
multi_polygon1.should == multi_polygon2
|
17
|
+
end
|
18
|
+
|
19
|
+
it "test_multi_polygon_binary" do
|
20
|
+
multi_polygon = 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]]],256),Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)],256)
|
21
|
+
multi_polygon.as_hex_ewkb.should eql("0106000020000100000200000001030000000200000004000000CDCCCCCCCCCC28406666666666A646C03333333333B34640CDCCCCCCCCCC44406DE7FBA9F1D211403D2CD49AE61DF13FCDCCCCCCCCCC28406666666666A646C004000000333333333333034033333333333315409A999999999915408A8EE4F21FD2F63FEC51B81E85EB2C40F6285C8FC2F5F03F3333333333330340333333333333154001030000000200000005000000000000000000000000000000000000000000000000001040000000000000000000000000000010400000000000001040000000000000000000000000000010400000000000000000000000000000000005000000000000000000F03F000000000000F03F0000000000000840000000000000F03F00000000000008400000000000000840000000000000F03F0000000000000840000000000000F03F000000000000F03F")
|
22
|
+
|
23
|
+
multi_polygon = MultiPolygon.from_polygons([Polygon.from_coordinates([[[12.4,-45.3,1.2],[45.4,41.6,1.2],[4.456,1.0698,1.2],[12.4,-45.3,1.2]],[[2.4,5.3,1.2],[5.4,1.4263,1.2],[14.46,1.06,1.2],[2.4,5.3,1.2]]],256,false,true),Polygon.from_coordinates([[[0,0,1.2],[4,0,1.2],[4,4,2.3],[0,4,1.2],[0,0,1.2]],[[1,1,2.2],[3,1,3.3],[3,3,1.1],[1,3,2.4],[1,1,2.2]]],256,false,true)],256,false,true)
|
24
|
+
multi_polygon.as_hex_ewkb.should eql("0106000020000100000200000001030000400200000004000000CDCCCCCCCCCC28406666666666A646C0333333333333F33F3333333333B34640CDCCCCCCCCCC4440333333333333F33F6DE7FBA9F1D211403D2CD49AE61DF13F333333333333F33FCDCCCCCCCCCC28406666666666A646C0333333333333F33F0400000033333333333303403333333333331540333333333333F33F9A999999999915408A8EE4F21FD2F63F333333333333F33FEC51B81E85EB2C40F6285C8FC2F5F03F333333333333F33F33333333333303403333333333331540333333333333F33F0103000040020000000500000000000000000000000000000000000000333333333333F33F00000000000010400000000000000000333333333333F33F00000000000010400000000000001040666666666666024000000000000000000000000000001040333333333333F33F00000000000000000000000000000000333333333333F33F05000000000000000000F03F000000000000F03F9A999999999901400000000000000840000000000000F03F6666666666660A40000000000000084000000000000008409A9999999999F13F000000000000F03F00000000000008403333333333330340000000000000F03F000000000000F03F9A99999999990140")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "test_multi_polygon_text" do
|
28
|
+
multi_polygon = 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]]],256),Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)],256)
|
29
|
+
multi_polygon.as_ewkt.should eql("SRID=256;MULTIPOLYGON(((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)),((0 0,4 0,4 4,0 4,0 0),(1 1,3 1,3 3,1 3,1 1)))")
|
30
|
+
|
31
|
+
multi_polygon = MultiPolygon.from_polygons([Polygon.from_coordinates([[[12.4,-45.3,2],[45.4,41.6,3],[4.456,1.0698,4],[12.4,-45.3,2]],[[2.4,5.3,1],[5.4,1.4263,3.44],[14.46,1.06,4.5],[2.4,5.3,1]]],4326,true),Polygon.from_coordinates([[[0,0,5.6],[4,0,5.4],[4,4,1],[0,4,23],[0,0,5.6]],[[1,1,2.3],[3,1,4],[3,3,5],[1,3,6],[1,1,2.3]]],4326,true)],4326,true)
|
32
|
+
multi_polygon.as_ewkt.should eql("SRID=4326;MULTIPOLYGON(((12.4 -45.3 2,45.4 41.6 3,4.456 1.0698 4,12.4 -45.3 2),(2.4 5.3 1,5.4 1.4263 3.44,14.46 1.06 4.5,2.4 5.3 1)),((0 0 5.6,4 0 5.4,4 4 1,0 4 23,0 0 5.6),(1 1 2.3,3 1 4,3 3 5,1 3 6,1 1 2.3)))")
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "Counting" do
|
36
|
+
|
37
|
+
before do
|
38
|
+
@mp = 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]]],256),Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)],256)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should have a points method" do
|
42
|
+
@mp.points[0].should be_instance_of(Point)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should flatten it right" do
|
46
|
+
@mp.should have(18).points
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,356 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
3
|
+
|
4
|
+
describe Point do
|
5
|
+
before(:each) do
|
6
|
+
@point = Point.new(4326)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should instantiatember" do
|
10
|
+
violated unless @point
|
11
|
+
@point.should be_instance_of(Point)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should have a nice matcher" do
|
15
|
+
@point.should be_a_point
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should have a very nice matcher" do
|
19
|
+
@point.should be_a_point(0.0, 0.0)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should have a very nice matcher" do
|
23
|
+
Point.from_x_y_z_m(1,2,3.33,"t").should be_a_point(1, 2, 3.33, "t")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should have a dumb matcher" do
|
27
|
+
Point.should be_geometric
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should be subclassable" do
|
31
|
+
place = Class.new(Point)
|
32
|
+
p = place.from_x_y(1,2)
|
33
|
+
p.should be_a place
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should have binary_geometry_type 2" do
|
37
|
+
@point.binary_geometry_type.should eql(1)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should have the correct srid" do
|
41
|
+
@point.srid.should eql(4326)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should not have z or m" do
|
45
|
+
@point.with_z.should be_false
|
46
|
+
@point.should_not be_with_z
|
47
|
+
@point.with_m.should be_false
|
48
|
+
@point.should_not be_with_m
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should set params to 0.0" do
|
52
|
+
@point.x.should eql(0.0)
|
53
|
+
@point.y.should eql(0.0)
|
54
|
+
@point.z.should eql(0.0)
|
55
|
+
@point.m.should eql(0.0)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should compare ok" do
|
59
|
+
point1= Point::new
|
60
|
+
point1.set_x_y(1.5,45.4)
|
61
|
+
point2= Point::new
|
62
|
+
point2.set_x_y(1.5,45.4)
|
63
|
+
point3= Point::new
|
64
|
+
point3.set_x_y(4.5,12.3)
|
65
|
+
point4= Point::new
|
66
|
+
point4.set_x_y_z(1.5,45.4,423)
|
67
|
+
point5= Point::new
|
68
|
+
point5.set_x_y(1.5,45.4)
|
69
|
+
point5.m=15
|
70
|
+
geometry= Geometry::new
|
71
|
+
|
72
|
+
point1.should == point2
|
73
|
+
point1.should_not == point3
|
74
|
+
point1.should_not == point4
|
75
|
+
point1.should_not == point5
|
76
|
+
point1.should_not == geometry
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "> Instantiation" do
|
80
|
+
|
81
|
+
it "should instantiate a 2d point" do
|
82
|
+
point = Point.from_x_y(10,20,123)
|
83
|
+
point.x.should eql(10)
|
84
|
+
point.y.should eql(20)
|
85
|
+
point.srid.should eql(123)
|
86
|
+
point.z.should eql(0.0)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should instantiate a 3d point" do
|
90
|
+
point = Point.from_x_y_z(-10,-20,-30)
|
91
|
+
point.x.should eql(-10)
|
92
|
+
point.y.should eql(-20)
|
93
|
+
point.z.should eql(-30)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should instantiate a 3d(m) point" do
|
97
|
+
point = Point.from_x_y_m(10,20,30)
|
98
|
+
point.x.should eql(10)
|
99
|
+
point.y.should eql(20)
|
100
|
+
point.m.should eql(30)
|
101
|
+
point.z.should eql(0.0)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should instantiate a 4d point" do
|
105
|
+
point = Point.from_x_y_z_m(10,20,30,40,123)
|
106
|
+
point.x.should eql(10)
|
107
|
+
point.y.should eql(20)
|
108
|
+
point.z.should eql(30)
|
109
|
+
point.m.should eql(40)
|
110
|
+
point.srid.should eql(123)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should instantiate a point from polar coordinates" do
|
114
|
+
point = Point.from_r_t(1.4142,45)
|
115
|
+
point.y.should be_within(0.1).of(1)
|
116
|
+
point.x.should be_within(0.1).of(1)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should instantiate from coordinates x,y" do
|
120
|
+
point = Point.from_coordinates([1.6,2.8],123)
|
121
|
+
point.x.should eql(1.6)
|
122
|
+
point.y.should eql(2.8)
|
123
|
+
point.should_not be_with_z
|
124
|
+
point.z.should eql(0.0)
|
125
|
+
point.srid.should eql(123)
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should instantiate from coordinates x,y,z" do
|
129
|
+
point = Point.from_coordinates([1.6,2.8,3.4],123, true)
|
130
|
+
point.x.should eql(1.6)
|
131
|
+
point.y.should eql(2.8)
|
132
|
+
point.z.should eql(3.4)
|
133
|
+
point.should be_with_z
|
134
|
+
point.srid.should eql(123)
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should instantiate from coordinates x,y,z,m" do
|
138
|
+
point = Point.from_coordinates([1.6,2.8,3.4,15],123, true, true)
|
139
|
+
point.x.should eql(1.6)
|
140
|
+
point.y.should eql(2.8)
|
141
|
+
point.z.should eql(3.4)
|
142
|
+
point.m.should eql(15)
|
143
|
+
point.should be_with_z
|
144
|
+
point.should be_with_m
|
145
|
+
point.srid.should eql(123)
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should have a bbox" do
|
149
|
+
bbox = Point.from_x_y_z_m(-1.6,2.8,-3.4,15,123).bounding_box
|
150
|
+
bbox.length.should eql(2)
|
151
|
+
bbox[0].should == Point.from_x_y_z(-1.6,2.8,-3.4)
|
152
|
+
bbox[1].should == Point.from_x_y_z(-1.6,2.8,-3.4)
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should parse lat long" do
|
156
|
+
Point.from_latlong("-20° 47' 26.37","-20° 47' 26.37").x.should be_within(0.00001).of(-20.790658)
|
157
|
+
Point.from_latlong("20° 47' 26.378","20° 47' 26.378").y.should be_within(0.00001).of(20.790658)
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should parse lat long w/o sec" do
|
161
|
+
Point.from_latlong("-20°47′26″","-20°47′26″").x.should be_within(0.00001).of(-20.790555)
|
162
|
+
Point.from_latlong("20°47′26″","20°47′26″").y.should be_within(0.00001).of(20.790555)
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should accept with W or S notation" do
|
166
|
+
Point.from_latlong("20° 47' 26.37 W","20° 47' 26.37 S").x.should be_within(0.00001).of(-20.790658)
|
167
|
+
Point.from_latlong("20° 47' 26.37 W","20° 47' 26.37 S").y.should be_within(0.00001).of(-20.790658)
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should instantiate a point from positive degrees" do
|
171
|
+
point = Point.from_latlong('47`20 06.09E','22`50 77.35N')
|
172
|
+
point.y.should be_within(0.000001).of(22.8548194)
|
173
|
+
point.x.should be_within(0.000001).of(47.335025)
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should instantiate a point from negative degrees" do
|
177
|
+
point = Point.from_latlong('47`20 06.09W','22`50 77.35S')
|
178
|
+
point.y.should be_within(0.000001).of(-22.8548194)
|
179
|
+
point.x.should be_within(0.000001).of(-47.335025)
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should print out nicely" do
|
183
|
+
Point.from_x_y(47.88, -20.1).as_latlong.should eql("47°52′48″, -20°06′00″")
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should print out nicely" do
|
187
|
+
Point.from_x_y(-20.78, 20.78).as_latlong(:full => true).should eql("-20°46′48.00″, 20°46′48.00″")
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should print out nicely" do
|
191
|
+
Point.from_x_y(47.11, -20.2).as_latlong(:full => true).should eql("47°06′36.00″, -20°11′60.00″")
|
192
|
+
end
|
193
|
+
|
194
|
+
it "should print out nicely" do
|
195
|
+
Point.from_x_y(47.11, -20.2).as_latlong(:coord => true).should eql("47°06′36″N, 20°11′60″W")
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should print out nicely" do
|
199
|
+
Point.from_x_y(-47.11, 20.2).as_latlong(:full => true,:coord => true).should eql("47°06′36.00″S, 20°11′60.00″E")
|
200
|
+
end
|
201
|
+
|
202
|
+
end
|
203
|
+
|
204
|
+
describe " > Distance & Bearing" do
|
205
|
+
|
206
|
+
before(:each) do
|
207
|
+
@p1 = Point.from_x_y(1,1)
|
208
|
+
@p2 = Point.from_x_y(2,2)
|
209
|
+
end
|
210
|
+
|
211
|
+
it "and a 3th grade child should calculate euclidian distance" do
|
212
|
+
@p1.euclidian_distance(@p2).
|
213
|
+
should be_within(0.00000001).of(1.4142135623731)
|
214
|
+
end
|
215
|
+
|
216
|
+
it "should calculate spherical distance" do
|
217
|
+
@p1.spherical_distance(@p2).
|
218
|
+
should be_within(0.00000001).of(157225.358003181)
|
219
|
+
end
|
220
|
+
|
221
|
+
it "should calculate ellipsoidal distance" do
|
222
|
+
@p1.ellipsoidal_distance(@p2).
|
223
|
+
should be_within(0.00000001).of(156876.149400742)
|
224
|
+
end
|
225
|
+
|
226
|
+
describe "Orthogonal Distance" do
|
227
|
+
before do
|
228
|
+
@line = LineString.from_coordinates([[0,0],[1,3]], 4326)
|
229
|
+
@line2 = LineString.from_coordinates([[1,1],[1,2]], 4326)
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should calcula orthogonal distance from a line (90 deg)" do
|
233
|
+
@p1.orthogonal_distance(@line).should be_within(0.001).of(1.414)
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should calcula orthogonal distance very close..." do
|
237
|
+
@p1.orthogonal_distance(@line2).should be_zero
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should calcula orthogonal distance from a line (90 deg)" do
|
241
|
+
@p2.orthogonal_distance(@line).should be_within(0.001).of(2.828)
|
242
|
+
end
|
243
|
+
|
244
|
+
it "should calcula orthogonal distance from a line (0 deg)" do
|
245
|
+
@p2.orthogonal_distance(@line2).should be_within(0.1).of(1.0)
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should calcula orthogonal distance from a line (0 deg)" do
|
249
|
+
@p2.orthogonal_distance(@line2).should be_within(0.1).of(1.0)
|
250
|
+
end
|
251
|
+
|
252
|
+
end
|
253
|
+
|
254
|
+
it "should calculate the bearing from apoint to another in degrees" do
|
255
|
+
@p1.bearing_to(@p2).should be_within(0.01).of(45.0)
|
256
|
+
end
|
257
|
+
|
258
|
+
it "should calculate the bearing from apoint to another in degrees" do
|
259
|
+
p3 = Point.from_x_y(1,-1)
|
260
|
+
@p1.bearing_to(p3).should be_within(0.01).of(180.0)
|
261
|
+
end
|
262
|
+
|
263
|
+
it "should calculate the bearing from apoint to another in degrees" do
|
264
|
+
p3 = Point.from_x_y(-1,-1)
|
265
|
+
@p1.bearing_to(p3).should be_within(0.01).of(225.0)
|
266
|
+
end
|
267
|
+
|
268
|
+
it "should calculate the bearing from apoint to another in degrees" do
|
269
|
+
p3 = Point.from_x_y(-1,1)
|
270
|
+
@p1.bearing_to(p3).should be_within(0.01).of(270.0)
|
271
|
+
end
|
272
|
+
|
273
|
+
it "should calculate the bearing from apoint to another in degrees" do
|
274
|
+
p3 = Point.from_x_y(2,-1)
|
275
|
+
@p1.bearing_to(p3).should be_within(0.0001).of(153.4349488)
|
276
|
+
end
|
277
|
+
|
278
|
+
it "should calculate a clone point bearing to 0" do
|
279
|
+
@p1.bearing_to(@p1).should eql(0)
|
280
|
+
end
|
281
|
+
|
282
|
+
it "should calculate the bearing from apoint to another in degrees" do
|
283
|
+
@p1.bearing_text(@p2).should eql(:ne)
|
284
|
+
end
|
285
|
+
|
286
|
+
it "should calculate the bearing from apoint to another in degrees" do
|
287
|
+
p3 = Point.from_x_y(-1,1)
|
288
|
+
@p1.bearing_text(p3).should eql(:w)
|
289
|
+
end
|
290
|
+
|
291
|
+
end
|
292
|
+
|
293
|
+
describe "> Export Formats" do
|
294
|
+
|
295
|
+
before(:each) do
|
296
|
+
@point = Point.from_x_y( -11.2431, 32.3141 )
|
297
|
+
end
|
298
|
+
|
299
|
+
it "should print nicely" do
|
300
|
+
@point.text_representation.should eql("-11.2431 32.3141")
|
301
|
+
end
|
302
|
+
|
303
|
+
it "should printoout as binary" do
|
304
|
+
Point.from_x_y(12.4,45.3,123).as_hex_ewkb.should eql("01010000207B000000CDCCCCCCCCCC28406666666666A64640")
|
305
|
+
point = Point.from_x_y_z_m(12.4,45.3,-3.5,15,123)
|
306
|
+
point.as_hex_ewkb.should eql("01010000E07B000000CDCCCCCCCCCC28406666666666A646400000000000000CC00000000000002E40")
|
307
|
+
point.as_hex_wkb.should eql("0101000000CDCCCCCCCCCC28406666666666A64640")
|
308
|
+
end
|
309
|
+
|
310
|
+
it "should printoout as text" do
|
311
|
+
Point.from_x_y(12.4,45.3,123).as_ewkt.should eql("SRID=123;POINT(12.4 45.3)")
|
312
|
+
point = Point.from_x_y_z(12.4,45.3,-3.5,123)
|
313
|
+
point.as_ewkt.should eql("SRID=123;POINT(12.4 45.3 -3.5)")
|
314
|
+
point.as_wkt.should eql("POINT(12.4 45.3)")
|
315
|
+
point.as_ewkt(false,true).should eql("POINT(12.4 45.3 -3.5)")
|
316
|
+
point = Point.from_x_y_m(12.4,45.3,-3.5,123)
|
317
|
+
point.as_ewkt.should eql("SRID=123;POINTM(12.4 45.3 -3.5)")
|
318
|
+
point.as_ewkt(true,true,false).should eql("SRID=123;POINT(12.4 45.3)")
|
319
|
+
end
|
320
|
+
|
321
|
+
it "should have a nice bounding box" do
|
322
|
+
@point.should have(2).bounding_box
|
323
|
+
@point.bounding_box.each do |point|
|
324
|
+
point.x.should eql(@point.x)
|
325
|
+
point.y.should eql(@point.y)
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
it "should print as kml too" do
|
330
|
+
@point.kml_representation.should eql("<Point>\n<coordinates>-11.2431,32.3141</coordinates>\n</Point>\n")
|
331
|
+
end
|
332
|
+
|
333
|
+
it "should print as georss" do
|
334
|
+
@point.georss_simple_representation(:georss_ns => 'hey').should eql("<hey:point>32.3141 -11.2431</hey:point>\n")
|
335
|
+
end
|
336
|
+
|
337
|
+
it "should print r (polar coords)" do
|
338
|
+
@point.r.should be_within(0.000001).of(34.214154)
|
339
|
+
end
|
340
|
+
|
341
|
+
it "should print theta as degrees" do
|
342
|
+
@point.theta_deg.should be_within(0.0001).of(289.184406352127)
|
343
|
+
end
|
344
|
+
|
345
|
+
it "should print theta as radians" do
|
346
|
+
@point.theta_rad.should be_within(0.0001).of(5.04722003626982)
|
347
|
+
end
|
348
|
+
|
349
|
+
it "should output as polar" do
|
350
|
+
@point.as_polar.should be_instance_of(Array)
|
351
|
+
@point.should have(2).as_polar #.length.should eql(2)
|
352
|
+
end
|
353
|
+
|
354
|
+
end
|
355
|
+
|
356
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Polygon do
|
4
|
+
|
5
|
+
describe "Instance Methods" do
|
6
|
+
|
7
|
+
let(:poly) { Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256) }
|
8
|
+
|
9
|
+
it "should check if contains point" do
|
10
|
+
poly.contains_point?(Point.from_x_y(3, 3)).should be_true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should check if not contains point" do
|
14
|
+
poly.contains_point?(Point.from_x_y(5, 5)).should be_false
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "tu converted" do
|
20
|
+
#no test of the binary representation for linear_rings : always with polygons and like line_string
|
21
|
+
it "should test_polygon_creation" do
|
22
|
+
linear_ring1 = LinearRing.from_coordinates([[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],256)
|
23
|
+
linear_ring2 = LinearRing.from_coordinates([[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]],256)
|
24
|
+
point1 = Point.from_x_y(12.4,-45.3,256)
|
25
|
+
point2 = Point.from_x_y(45.4,41.6,256)
|
26
|
+
point3 = Point.from_x_y(4.456,1.0698,256)
|
27
|
+
point4 = Point.from_x_y(12.4,-45.3,256)
|
28
|
+
point5 = Point.from_x_y(2.4,5.3,256)
|
29
|
+
point6 = Point.from_x_y(5.4,1.4263,256)
|
30
|
+
point7 = Point.from_x_y(14.46,1.06,256)
|
31
|
+
point8 = Point.from_x_y(2.4,5.3,256)
|
32
|
+
|
33
|
+
polygon = Polygon::new(256)
|
34
|
+
polygon.length.should be_zero
|
35
|
+
|
36
|
+
polygon << linear_ring1
|
37
|
+
polygon.length.should eql(1)
|
38
|
+
polygon[0].should == linear_ring1
|
39
|
+
|
40
|
+
#the validity of the hole is not checked : just for the sake of example
|
41
|
+
polygon << linear_ring2
|
42
|
+
polygon.length.should eql(2)
|
43
|
+
polygon[1].should == linear_ring2
|
44
|
+
|
45
|
+
polygon = Polygon.from_linear_rings([linear_ring1,linear_ring2],256)
|
46
|
+
polygon.class.should eql(Polygon)
|
47
|
+
polygon.length.should eql(2)
|
48
|
+
polygon[0].should == linear_ring1
|
49
|
+
polygon[1].should == linear_ring2
|
50
|
+
|
51
|
+
polygon = 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]]],256)
|
52
|
+
polygon.class.should eql(Polygon)
|
53
|
+
polygon.length.should eql(2)
|
54
|
+
polygon[0].should == linear_ring1
|
55
|
+
polygon[1].should == linear_ring2
|
56
|
+
|
57
|
+
polygon = Polygon.from_points([[point1,point2,point3,point4],[point5,point6,point7,point8]],256)
|
58
|
+
polygon.length.should eql(2)
|
59
|
+
polygon[0].should == linear_ring1
|
60
|
+
polygon[1].should == linear_ring2
|
61
|
+
|
62
|
+
polygon = Polygon.from_coordinates([[[12.4,-45.3,15.2],[45.4,41.6,2.4],[4.456,1.0698,5.6],[12.4,-45.3,6.1]],[[2.4,5.3,4.5],[5.4,1.4263,4.2],[14.46,1.06,123.1],[2.4,5.3,4.4]]],256,true)
|
63
|
+
polygon.class.should eql(Polygon)
|
64
|
+
polygon.length.should eql(2)
|
65
|
+
|
66
|
+
linear_ring1 = LinearRing.from_coordinates([[12.4,-45.3,15.2],[45.4,41.6,2.4],[4.456,1.0698,5.6],[12.4,-45.3,6.1]],256,true)
|
67
|
+
linear_ring2 = LinearRing.from_coordinates([[2.4,5.3,4.5],[5.4,1.4263,4.2],[14.46,1.06,123.1],[2.4,5.3,4.4]],256,true)
|
68
|
+
polygon[0].should == linear_ring1
|
69
|
+
polygon[1].should == linear_ring2
|
70
|
+
end
|
71
|
+
|
72
|
+
it "bbox" do
|
73
|
+
bbox = Polygon.from_coordinates([[[12.4,-45.3,15.2],[45.4,41.6,2.4],[4.456,1.0698,5.6],[12.4,-45.3,6.1]],[[2.4,5.3,4.5],[5.4,1.4263,4.2],[14.46,1.06,123.1],[2.4,5.3,4.4]]],256,true).bounding_box
|
74
|
+
bbox.length.should eql(2)
|
75
|
+
bbox[0].should == Point.from_x_y_z(4.456,-45.3,2.4)
|
76
|
+
bbox[1].should == Point.from_x_y_z(45.4,41.6,123.1)
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
it "test_polygon_equal" do
|
81
|
+
polygon1 = 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]]],256)
|
82
|
+
polygon2 = 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]]])
|
83
|
+
point = Point.from_x_y(12.4,-45.3,123)
|
84
|
+
|
85
|
+
polygon1.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]]],256)
|
86
|
+
polygon1.should_not == polygon2
|
87
|
+
polygon1.should_not == point
|
88
|
+
end
|
89
|
+
|
90
|
+
it "test_polygon_binary" do
|
91
|
+
polygon = Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)
|
92
|
+
#taken from PostGIS answer
|
93
|
+
polygon.as_hex_ewkb.should eql("0103000020000100000200000005000000000000000000000000000000000000000000000000001040000000000000000000000000000010400000000000001040000000000000000000000000000010400000000000000000000000000000000005000000000000000000F03F000000000000F03F0000000000000840000000000000F03F00000000000008400000000000000840000000000000F03F0000000000000840000000000000F03F000000000000F03F")
|
94
|
+
|
95
|
+
polygon = Polygon.from_coordinates([[[0,0,2],[4,0,2],[4,4,2],[0,4,2],[0,0,2]],[[1,1,2],[3,1,2],[3,3,2],[1,3,2],[1,1,2]]],256,true)
|
96
|
+
#taken from PostGIS answer
|
97
|
+
polygon.as_hex_ewkb.should eql("01030000A000010000020000000500000000000000000000000000000000000000000000000000004000000000000010400000000000000000000000000000004000000000000010400000000000001040000000000000004000000000000000000000000000001040000000000000004000000000000000000000000000000000000000000000004005000000000000000000F03F000000000000F03F00000000000000400000000000000840000000000000F03F0000000000000040000000000000084000000000000008400000000000000040000000000000F03F00000000000008400000000000000040000000000000F03F000000000000F03F0000000000000040")
|
98
|
+
|
99
|
+
polygon = Polygon.from_coordinates([[[0,0,2],[4,0,2],[4,4,2],[0,4,2],[0,0,2]],[[1,1,2],[3,1,2],[3,3,2],[1,3,2],[1,1,2]]],256,false,true)
|
100
|
+
polygon.as_hex_ewkb.should eql("010300006000010000020000000500000000000000000000000000000000000000000000000000004000000000000010400000000000000000000000000000004000000000000010400000000000001040000000000000004000000000000000000000000000001040000000000000004000000000000000000000000000000000000000000000004005000000000000000000F03F000000000000F03F00000000000000400000000000000840000000000000F03F0000000000000040000000000000084000000000000008400000000000000040000000000000F03F00000000000008400000000000000040000000000000F03F000000000000F03F0000000000000040")
|
101
|
+
|
102
|
+
polygon = 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,123],[3,3,2,12.2],[1,3,2,12],[1,1,2,12.3]]],256,true,true)
|
103
|
+
polygon.as_hex_ewkb.should eql("01030000E0000100000200000005000000000000000000000000000000000000000000000000000040CDCCCCCCCC8C46C00000000000001040000000000000000000000000000000400000000000001440000000000000104000000000000010400000000000000040AE47E17A14AE1240000000000000000000000000000010400000000000000040713D0AD7A370F53F000000000000000000000000000000000000000000000040CDCCCCCCCC8C46C005000000000000000000F03F000000000000F03F00000000000000409A999999999928400000000000000840000000000000F03F00000000000000400000000000C05E400000000000000840000000000000084000000000000000406666666666662840000000000000F03F000000000000084000000000000000400000000000002840000000000000F03F000000000000F03F00000000000000409A99999999992840")
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should test_polygon_text" do
|
107
|
+
polygon = Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)
|
108
|
+
polygon.as_ewkt.should eql("SRID=256;POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,3 1,3 3,1 3,1 1))")
|
109
|
+
|
110
|
+
polygon = Polygon.from_coordinates([[[0,0,2],[4,0,2],[4,4,2],[0,4,2],[0,0,2]],[[1,1,2],[3,1,2],[3,3,2],[1,3,2],[1,1,2]]],256,true)
|
111
|
+
polygon.as_ewkt.should eql("SRID=256;POLYGON((0 0 2,4 0 2,4 4 2,0 4 2,0 0 2),(1 1 2,3 1 2,3 3 2,1 3 2,1 1 2))")
|
112
|
+
|
113
|
+
polygon = Polygon.from_coordinates([[[0,0,2],[4,0,2],[4,4,2],[0,4,2],[0,0,2]],[[1,1,2],[3,1,2],[3,3,2],[1,3,2],[1,1,2]]],256,false,true)
|
114
|
+
polygon.as_ewkt.should eql("SRID=256;POLYGONM((0 0 2,4 0 2,4 4 2,0 4 2,0 0 2),(1 1 2,3 1 2,3 3 2,1 3 2,1 1 2))")
|
115
|
+
|
116
|
+
polygon = 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,123],[3,3,2,12.2],[1,3,2,12],[1,1,2,12.3]]],256,true,true)
|
117
|
+
polygon.as_ewkt.should eql("SRID=256;POLYGON((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 123,3 3 2 12.2,1 3 2 12,1 1 2 12.3))")
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|