georuby 1.9.3

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.
Files changed (75) hide show
  1. data/Gemfile +8 -0
  2. data/Gemfile.lock +29 -0
  3. data/History.txt +4 -0
  4. data/LICENSE +21 -0
  5. data/README.rdoc +184 -0
  6. data/Rakefile +48 -0
  7. data/VERSION +1 -0
  8. data/georuby.gemspec +128 -0
  9. data/lib/geo_ruby.rb +23 -0
  10. data/lib/geo_ruby/geojson.rb +129 -0
  11. data/lib/geo_ruby/georss.rb +133 -0
  12. data/lib/geo_ruby/gpx.rb +1 -0
  13. data/lib/geo_ruby/gpx4r/gpx.rb +118 -0
  14. data/lib/geo_ruby/shp.rb +1 -0
  15. data/lib/geo_ruby/shp4r/dbf.rb +42 -0
  16. data/lib/geo_ruby/shp4r/shp.rb +718 -0
  17. data/lib/geo_ruby/simple_features/envelope.rb +167 -0
  18. data/lib/geo_ruby/simple_features/ewkb_parser.rb +218 -0
  19. data/lib/geo_ruby/simple_features/ewkt_parser.rb +336 -0
  20. data/lib/geo_ruby/simple_features/geometry.rb +236 -0
  21. data/lib/geo_ruby/simple_features/geometry_collection.rb +144 -0
  22. data/lib/geo_ruby/simple_features/geometry_factory.rb +81 -0
  23. data/lib/geo_ruby/simple_features/helper.rb +18 -0
  24. data/lib/geo_ruby/simple_features/line_string.rb +228 -0
  25. data/lib/geo_ruby/simple_features/linear_ring.rb +34 -0
  26. data/lib/geo_ruby/simple_features/multi_line_string.rb +63 -0
  27. data/lib/geo_ruby/simple_features/multi_point.rb +58 -0
  28. data/lib/geo_ruby/simple_features/multi_polygon.rb +64 -0
  29. data/lib/geo_ruby/simple_features/point.rb +381 -0
  30. data/lib/geo_ruby/simple_features/polygon.rb +175 -0
  31. data/nofxx-georuby.gemspec +149 -0
  32. data/spec/data/geojson/feature_collection.json +34 -0
  33. data/spec/data/georss/atom.xml +21 -0
  34. data/spec/data/georss/gml.xml +40 -0
  35. data/spec/data/georss/w3c.xml +22 -0
  36. data/spec/data/gpx/fells_loop.gpx +1077 -0
  37. data/spec/data/gpx/long.gpx +1642 -0
  38. data/spec/data/gpx/long.kml +31590 -0
  39. data/spec/data/gpx/long.nmea +2220 -0
  40. data/spec/data/gpx/short.gpx +13634 -0
  41. data/spec/data/gpx/short.kml +130 -0
  42. data/spec/data/gpx/tracktreks.gpx +706 -0
  43. data/spec/data/multipoint.dbf +0 -0
  44. data/spec/data/multipoint.shp +0 -0
  45. data/spec/data/multipoint.shx +0 -0
  46. data/spec/data/point.dbf +0 -0
  47. data/spec/data/point.shp +0 -0
  48. data/spec/data/point.shx +0 -0
  49. data/spec/data/polygon.dbf +0 -0
  50. data/spec/data/polygon.shp +0 -0
  51. data/spec/data/polygon.shx +0 -0
  52. data/spec/data/polyline.dbf +0 -0
  53. data/spec/data/polyline.shp +0 -0
  54. data/spec/data/polyline.shx +0 -0
  55. data/spec/geo_ruby/geojson_spec.rb +147 -0
  56. data/spec/geo_ruby/georss.rb +218 -0
  57. data/spec/geo_ruby/georss_spec.rb +14 -0
  58. data/spec/geo_ruby/gpx4r/gpx_spec.rb +106 -0
  59. data/spec/geo_ruby/shp4r/shp_spec.rb +239 -0
  60. data/spec/geo_ruby/simple_features/envelope_spec.rb +47 -0
  61. data/spec/geo_ruby/simple_features/ewkb_parser_spec.rb +158 -0
  62. data/spec/geo_ruby/simple_features/ewkt_parser_spec.rb +179 -0
  63. data/spec/geo_ruby/simple_features/geometry_collection_spec.rb +55 -0
  64. data/spec/geo_ruby/simple_features/geometry_factory_spec.rb +11 -0
  65. data/spec/geo_ruby/simple_features/geometry_spec.rb +32 -0
  66. data/spec/geo_ruby/simple_features/line_string_spec.rb +259 -0
  67. data/spec/geo_ruby/simple_features/linear_ring_spec.rb +24 -0
  68. data/spec/geo_ruby/simple_features/multi_line_string_spec.rb +54 -0
  69. data/spec/geo_ruby/simple_features/multi_point_spec.rb +35 -0
  70. data/spec/geo_ruby/simple_features/multi_polygon_spec.rb +50 -0
  71. data/spec/geo_ruby/simple_features/point_spec.rb +356 -0
  72. data/spec/geo_ruby/simple_features/polygon_spec.rb +122 -0
  73. data/spec/geo_ruby_spec.rb +27 -0
  74. data/spec/spec_helper.rb +73 -0
  75. metadata +228 -0
@@ -0,0 +1,239 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ include GeoRuby::Shp4r
4
+
5
+ describe Shp4r do
6
+
7
+ describe "Point" do
8
+ before(:each) do
9
+ @shpfile = ShpFile.open(File.dirname(__FILE__) + '/../../data/point.shp')
10
+ end
11
+
12
+ it "should parse ok" do
13
+ @shpfile.record_count.should eql(2)
14
+ @shpfile.should have(1).fields
15
+ @shpfile.shp_type.should eql(ShpType::POINT)
16
+ end
17
+
18
+ it "should parse fields" do
19
+ field = @shpfile.fields.first
20
+ field.name.should eql("Hoyoyo")
21
+ field.type.should eql("N")
22
+ end
23
+
24
+ it "should parse record 1" do
25
+ rec = @shpfile[0]
26
+ rec.geometry.should be_kind_of Point
27
+ rec.geometry.x.should be_within(0.00001).of(-90.08375)
28
+ rec.geometry.y.should be_within(0.00001).of(34.39996)
29
+ rec.data["Hoyoyo"].should eql(6)
30
+ end
31
+
32
+ it "should parse record 2" do
33
+ rec = @shpfile[1]
34
+ rec.geometry.should be_kind_of Point
35
+ rec.geometry.x.should be_within(0.00001).of(-87.82580)
36
+ rec.geometry.y.should be_within(0.00001).of(33.36416)
37
+ rec.data["Hoyoyo"].should eql(9)
38
+ end
39
+
40
+ end
41
+
42
+ describe "Polyline" do
43
+ before(:each) do
44
+ @shpfile = ShpFile.open(File.dirname(__FILE__) + '/../../data/polyline.shp')
45
+ end
46
+
47
+ it "should parse ok" do
48
+ @shpfile.record_count.should eql(1)
49
+ @shpfile.should have(1).fields
50
+ @shpfile.shp_type.should eql(ShpType::POLYLINE)
51
+ end
52
+
53
+ it "should parse fields" do
54
+ field = @shpfile.fields.first
55
+ field.name.should eql("Chipoto")
56
+ # Dbf now uses the decimal to choose between int and float
57
+ # So here is N instead of F
58
+ field.type.should eql("N")
59
+ end
60
+
61
+ it "should parse record 1" do
62
+ rec = @shpfile[0]
63
+ rec.geometry.should be_kind_of MultiLineString
64
+ rec.geometry.length.should eql(1)
65
+ rec.geometry[0].length.should eql(6)
66
+ rec.data["Chipoto"].should eql(5.678)
67
+ end
68
+
69
+ end
70
+
71
+ describe "Polygon" do
72
+ before(:each) do
73
+ @shpfile = ShpFile.open(File.dirname(__FILE__) + '/../../data/polygon.shp')
74
+ end
75
+
76
+ it "should parse ok" do
77
+ @shpfile.record_count.should eql(1)
78
+ @shpfile.should have(1).fields
79
+ @shpfile.shp_type.should eql(ShpType::POLYGON)
80
+ end
81
+
82
+ it "should parse fields" do
83
+ field = @shpfile.fields.first
84
+ field.name.should eql("Hello")
85
+ field.type.should eql("C")
86
+ end
87
+
88
+ it "should parse record 1" do
89
+ rec = @shpfile[0]
90
+ rec.geometry.should be_kind_of MultiPolygon
91
+ rec.geometry.length.should eql(1)
92
+ rec.geometry[0].length.should eql(1)
93
+ rec.geometry[0][0].length.should eql(7)
94
+ rec.data["Hello"].should eql("Bouyoul!")
95
+ end
96
+ end
97
+
98
+ describe "Write" do
99
+ def cp_all_shp(file1,file2)
100
+ FileUtils.copy(file1 + ".shp",file2 + ".shp")
101
+ FileUtils.copy(file1 + ".shx",file2 + ".shx")
102
+ FileUtils.copy(file1 + ".dbf",file2 + ".dbf")
103
+ end
104
+
105
+ def rm_all_shp(file)
106
+ FileUtils.rm(file + ".shp")
107
+ FileUtils.rm(file + ".shx")
108
+ FileUtils.rm(file + ".dbf")
109
+ end
110
+
111
+ it "test_point" do
112
+ cp_all_shp(File.dirname(__FILE__) + '/../../data/point',
113
+ File.dirname(__FILE__) + '/../../data/point2')
114
+ shpfile = ShpFile.open(File.dirname(__FILE__) + '/../../data/point2.shp')
115
+
116
+ shpfile.transaction do |tr|
117
+ tr.should be_instance_of ShpTransaction
118
+ tr.add(ShpRecord.new(Point.from_x_y(123.4,123.4),'Hoyoyo' => 5))
119
+ tr.add(ShpRecord.new(Point.from_x_y(-16.67,16.41),'Hoyoyo' => -7))
120
+ tr.delete(1)
121
+ end
122
+
123
+ shpfile.record_count.should eql(3)
124
+
125
+ shpfile.close
126
+ rm_all_shp(File.dirname(__FILE__) + '/../../data/point2')
127
+ end
128
+
129
+ it "test_linestring" do
130
+ cp_all_shp(File.dirname(__FILE__) + '/../../data/polyline',
131
+ File.dirname(__FILE__) + '/../../data/polyline2')
132
+
133
+ shpfile = ShpFile.open(File.dirname(__FILE__) + '/../../data/polyline2.shp')
134
+
135
+ shpfile.transaction do |tr|
136
+ tr.should be_instance_of ShpTransaction
137
+ tr.add(ShpRecord.new(LineString.from_coordinates([[123.4,123.4],[45.6,12.3]]),'Chipoto' => 5.6778))
138
+ tr.add(ShpRecord.new(LineString.from_coordinates([[23.4,13.4],[45.6,12.3],[12,-67]]),'Chipoto' => -7.1))
139
+ tr.delete(0)
140
+ end
141
+
142
+ shpfile.record_count.should eql(2)
143
+ shpfile.close
144
+ rm_all_shp(File.dirname(__FILE__) + '/../../data/polyline2')
145
+ end
146
+
147
+ it "test_polygon" do
148
+ cp_all_shp(File.dirname(__FILE__) + '/../../data/polygon',
149
+ File.dirname(__FILE__) + '/../../data/polygon2')
150
+ shpfile = ShpFile.open(File.dirname(__FILE__) + '/../../data/polygon2.shp')
151
+
152
+ shpfile.transaction do |tr|
153
+ tr.should be_instance_of ShpTransaction
154
+ tr.delete(0)
155
+ tr.add(ShpRecord.new(Polygon.from_coordinates([[[0,0],[40,0],[40,40],[0,40],[0,0]],[[10,10],[10,20],[20,20],[10,10]]]),'Hello' => "oook"))
156
+ end
157
+
158
+ shpfile.record_count.should eql(1)
159
+
160
+ shpfile.close
161
+ rm_all_shp(File.dirname(__FILE__) + '/../../data/polygon2')
162
+ end
163
+
164
+ it "test_multipoint" do
165
+ cp_all_shp(File.dirname(__FILE__) + '/../../data/multipoint',
166
+ File.dirname(__FILE__) + '/../../data/multipoint2')
167
+ shpfile = ShpFile.open(File.dirname(__FILE__) + '/../../data/multipoint2.shp')
168
+
169
+ shpfile.transaction do |tr|
170
+ tr.should be_instance_of ShpTransaction
171
+ tr.add(ShpRecord.new(MultiPoint.from_coordinates([[45.6,-45.1],[12.4,98.2],[51.2,-0.12],[156.12345,56.109]]),'Hello' => 5,"Hoyoyo" => "AEZAE"))
172
+ end
173
+
174
+ shpfile.record_count.should eql(2)
175
+
176
+ shpfile.close
177
+ rm_all_shp(File.dirname(__FILE__) + '/../../data/multipoint2')
178
+ end
179
+
180
+ it "test_multi_polygon" do
181
+ cp_all_shp(File.dirname(__FILE__) + '/../../data/polygon',
182
+ File.dirname(__FILE__) + '/../../data/polygon4')
183
+
184
+ shpfile = ShpFile.open(File.dirname(__FILE__) + '/../../data/polygon4.shp')
185
+
186
+ shpfile.transaction do |tr|
187
+ tr.should be_instance_of ShpTransaction
188
+ tr.add(ShpRecord.new(MultiPolygon.from_polygons([Polygon.from_coordinates([[[0,0],[40,0],[40,40],[0,40],[0,0]],[[10,10],[10,20],[20,20],[10,10]]])]),'Hello' => "oook"))
189
+ end
190
+
191
+ shpfile.record_count.should eql(2)
192
+
193
+ shpfile.close
194
+
195
+ rm_all_shp(File.dirname(__FILE__) + '/../../data/polygon4')
196
+ end
197
+
198
+ it "test_rollback" do
199
+ cp_all_shp(File.dirname(__FILE__) + '/../../data/polygon',
200
+ File.dirname(__FILE__) + '/../../data/polygon5')
201
+
202
+ shpfile = ShpFile.open(File.dirname(__FILE__) + '/../../data/polygon5.shp')
203
+
204
+ shpfile.transaction do |tr|
205
+ tr.should be_instance_of ShpTransaction
206
+ tr.add(ShpRecord.new(MultiPolygon.from_polygons([Polygon.from_coordinates([[[0,0],[40,0],[40,40],[0,40],[0,0]],[[10,10],[10,20],[20,20],[10,10]]])]),'Hello' => "oook"))
207
+ tr.rollback
208
+ end
209
+ shpfile.record_count.should eql(1)
210
+
211
+ shpfile.close
212
+
213
+ rm_all_shp(File.dirname(__FILE__) + '/../../data/polygon5')
214
+
215
+ end
216
+
217
+ it "test_creation" do
218
+ shpfile = ShpFile.create(File.dirname(__FILE__) + '/../../data/point3.shp',ShpType::POINT,[Dbf::Field.new("Hoyoyo","C",10,0)])
219
+ shpfile.transaction do |tr|
220
+ tr.add(ShpRecord.new(Point.from_x_y(123,123.4),'Hoyoyo' => "HJHJJ"))
221
+ end
222
+ shpfile.record_count.should eql(1)
223
+ shpfile.close
224
+ rm_all_shp(File.dirname(__FILE__) + '/../../data/point3')
225
+ end
226
+
227
+ it "test_creation_multipoint" do
228
+ shpfile = ShpFile.create(File.dirname(__FILE__) + '/../../data/multipoint3.shp',ShpType::MULTIPOINT,[Dbf::Field.new("Hoyoyo","C",10),Dbf::Field.new("Hello","N",10)])
229
+ shpfile.transaction do |tr|
230
+ tr.add(ShpRecord.new(MultiPoint.from_coordinates([[123,123.4],[345,12.2]]),'Hoyoyo' => "HJHJJ","Hello" => 5))
231
+ end
232
+ shpfile.record_count.should eql(1)
233
+ shpfile.close
234
+ rm_all_shp(File.dirname(__FILE__) + '/../../data/multipoint3')
235
+ end
236
+
237
+ end
238
+
239
+ end
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Envelope do
4
+ before(:each) do
5
+ @srid = 4269
6
+ @env = Envelope.from_points([Point.from_x_y(10,20, @srid),Point.from_x_y(20,30, @srid)], @srid)
7
+ end
8
+
9
+ it "should initialize" do
10
+ @env.should be_instance_of(Envelope)
11
+ end
12
+
13
+ it "converted tu" do
14
+ linear_ring = LinearRing.from_coordinates([[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],256)
15
+ polygon = Polygon.from_linear_rings([linear_ring],256)
16
+ e = polygon.envelope
17
+
18
+ e.lower_corner.class.should eql(Point)
19
+ e.upper_corner.class.should eql(Point)
20
+
21
+ e.lower_corner.x.should eql(4.456)
22
+ e.lower_corner.y.should eql(-45.3)
23
+ e.upper_corner.x.should eql(45.4)
24
+ e.upper_corner.y.should eql(41.6)
25
+
26
+ line_string = LineString.from_coordinates([[13.6,-49.3],[45.4,44.6],[14.2,1.09],[13.6,-49.3]],256)
27
+ e2 = line_string.envelope
28
+
29
+ e3 = e.extend(e2)
30
+
31
+ e3.lower_corner.x.should eql(4.456)
32
+ e3.lower_corner.y.should eql(-49.3)
33
+ e3.upper_corner.x.should eql(45.4)
34
+ e3.upper_corner.y.should eql(44.6)
35
+ end
36
+
37
+ it "should have a center" do
38
+ @env.center.x.should eql(15)
39
+ @env.center.y.should eql(25)
40
+ @env.center.srid.should eql(@env.srid)
41
+ end
42
+
43
+ it "should print a kml_representation" do
44
+ @env.as_kml.should eql("<LatLonAltBox>\n<north>30</north>\n<south>20</south>\n<east>20</east>\n<west>10</west>\n</LatLonAltBox>\n")
45
+ end
46
+
47
+ end
@@ -0,0 +1,158 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe EWKBParser do
4
+
5
+ before(:each) do
6
+ @factory = GeometryFactory::new
7
+ @hex_ewkb_parser = HexEWKBParser::new(@factory)
8
+ end
9
+
10
+ it "test_point2d" do
11
+ @hex_ewkb_parser.parse("01010000207B000000CDCCCCCCCCCC28406666666666A64640")
12
+ point = @factory.geometry
13
+ point.should be_instance_of Point
14
+ point.should == Point.from_x_y(12.4,45.3,123)
15
+ end
16
+
17
+ it "test_point2d_BigEndian" do
18
+ @hex_ewkb_parser.parse("00000000014013A035BD512EC7404A3060C38F3669")
19
+ point = @factory.geometry
20
+ point.should be_instance_of Point
21
+ point.should == Point.from_x_y(4.906455,52.377953)
22
+ end
23
+
24
+ it "test_point3dz" do
25
+ @hex_ewkb_parser.parse("01010000A07B000000CDCCCCCCCCCC28406666666666A646400000000000000CC0")
26
+ point = @factory.geometry
27
+ point.should be_instance_of Point
28
+ point.should == Point.from_x_y_z(12.4,45.3,-3.5,123)
29
+ end
30
+
31
+ it "test_point4d" do
32
+ @hex_ewkb_parser.parse("01010000E07B000000CDCCCCCCCCCC28406666666666A646400000000000000CC00000000000002E40")
33
+ point = @factory.geometry
34
+ point.should be_instance_of Point
35
+ point.should == Point.from_x_y_z_m(12.4,45.3,-3.5,15,123)
36
+ end
37
+
38
+ it "test_line_string" do
39
+ @hex_ewkb_parser.parse("01020000200001000002000000CDCCCCCCCCCC28406666666666A646C03333333333B34640CDCCCCCCCCCC4440")
40
+ line_string = @factory.geometry
41
+ line_string.should be_instance_of LineString
42
+ line_string.should == LineString.from_coordinates([[12.4,-45.3],[45.4,41.6]],256)
43
+
44
+ @hex_ewkb_parser.parse("01020000A00001000002000000CDCCCCCCCCCC28406666666666A646C06666666666A641403333333333B34640CDCCCCCCCCCC44409A99999999992840")
45
+ line_string = @factory.geometry
46
+ line_string.should be_instance_of LineString
47
+ line_string.should == LineString.from_coordinates([[12.4,-45.3,35.3],[45.4,41.6,12.3]],256,true)
48
+
49
+ @hex_ewkb_parser.parse("01020000E00001000002000000CDCCCCCCCCCC28406666666666A646C06666666666A64140CDCCCCCCCC8C46403333333333B34640CDCCCCCCCCCC44409A999999999928403D0AD7A3701D4440")
50
+ line_string = @factory.geometry
51
+ line_string.should be_instance_of LineString
52
+ line_string.should == LineString.from_coordinates([[12.4,-45.3,35.3,45.1],[45.4,41.6,12.3,40.23]],256,true,true)
53
+ end
54
+
55
+ it "test_polygon" do
56
+ @hex_ewkb_parser.parse("0103000020000100000200000005000000000000000000000000000000000000000000000000001040000000000000000000000000000010400000000000001040000000000000000000000000000010400000000000000000000000000000000005000000000000000000F03F000000000000F03F0000000000000840000000000000F03F00000000000008400000000000000840000000000000F03F0000000000000840000000000000F03F000000000000F03F")
57
+ polygon = @factory.geometry
58
+ polygon.should be_instance_of Polygon
59
+ polygon.should == Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)
60
+
61
+ @hex_ewkb_parser.parse("01030000A000010000020000000500000000000000000000000000000000000000000000000000004000000000000010400000000000000000000000000000004000000000000010400000000000001040000000000000004000000000000000000000000000001040000000000000004000000000000000000000000000000000000000000000004005000000000000000000F03F000000000000F03F00000000000000400000000000000840000000000000F03F0000000000000040000000000000084000000000000008400000000000000040000000000000F03F00000000000008400000000000000040000000000000F03F000000000000F03F0000000000000040")
62
+ polygon = @factory.geometry
63
+ polygon.should be_instance_of Polygon
64
+ polygon.should == 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)
65
+
66
+ @hex_ewkb_parser.parse("010300006000010000020000000500000000000000000000000000000000000000000000000000004000000000000010400000000000000000000000000000004000000000000010400000000000001040000000000000004000000000000000000000000000001040000000000000004000000000000000000000000000000000000000000000004005000000000000000000F03F000000000000F03F00000000000000400000000000000840000000000000F03F0000000000000040000000000000084000000000000008400000000000000040000000000000F03F00000000000008400000000000000040000000000000F03F000000000000F03F0000000000000040")
67
+ polygon = @factory.geometry
68
+ polygon.should be_instance_of Polygon
69
+ polygon.should == 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)
70
+
71
+ @hex_ewkb_parser.parse("01030000E0000100000200000005000000000000000000000000000000000000000000000000000040CDCCCCCCCC8C46C00000000000001040000000000000000000000000000000400000000000001440000000000000104000000000000010400000000000000040AE47E17A14AE1240000000000000000000000000000010400000000000000040713D0AD7A370F53F000000000000000000000000000000000000000000000040CDCCCCCCCC8C46C005000000000000000000F03F000000000000F03F00000000000000409A999999999928400000000000000840000000000000F03F00000000000000400000000000C05E400000000000000840000000000000084000000000000000406666666666662840000000000000F03F000000000000084000000000000000400000000000002840000000000000F03F000000000000F03F00000000000000409A99999999992840")
72
+ polygon = @factory.geometry
73
+ polygon.should be_instance_of Polygon
74
+ polygon.should == 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)
75
+ end
76
+
77
+ it "test_geometry_collection" do
78
+ @hex_ewkb_parser.parse("010700002000010000020000000101000000AE47E17A14AE12403333333333B34640010200000002000000CDCCCCCCCCCC16406666666666E628403333333333E350400000000000004B40")
79
+ geometry_collection = @factory.geometry
80
+ geometry_collection.should be_instance_of GeometryCollection
81
+
82
+ geometry_collection.should == GeometryCollection.from_geometries([Point.from_x_y(4.67,45.4,256),LineString.from_coordinates([[5.7,12.45],[67.55,54]],256)],256)
83
+ geometry_collection[0].srid.should eql(256)
84
+
85
+ @hex_ewkb_parser.parse("01070000E0000100000200000001010000C0AE47E17A14AE12403333333333B34640F6285C8FC2D54640666666666666024001020000C002000000CDCCCCCCCCCC16406666666666E628403D0AD7A3703D124033333333339358403333333333E350400000000000004B4066666666666628403333333333330B40")
86
+ geometry_collection = @factory.geometry
87
+ geometry_collection.should be_instance_of GeometryCollection
88
+ geometry_collection.should == GeometryCollection.from_geometries([Point.from_x_y_z_m(4.67,45.4,45.67,2.3,256),LineString.from_coordinates([[5.7,12.45,4.56,98.3],[67.55,54,12.2,3.4]],256,true, true)],256,true, true)
89
+ geometry_collection[0].srid.should eql(256)
90
+ end
91
+
92
+ it "test_multi_point" do
93
+ @hex_ewkb_parser.parse("0104000020BC010000030000000101000000CDCCCCCCCCCC28403333333333D35EC0010100000066666666664650C09A99999999D95E4001010000001F97DD388EE35E400000000000C05E40")
94
+ multi_point = @factory.geometry
95
+ multi_point.should be_instance_of MultiPoint
96
+ multi_point.should == MultiPoint.from_coordinates([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]],444)
97
+ multi_point.srid.should eql(444)
98
+ multi_point[0].srid.should eql(444)
99
+
100
+ @hex_ewkb_parser.parse("01040000A0BC010000030000000101000080CDCCCCCCCCCC28403333333333D35EC00000000000001240010100008066666666664650C09A99999999D95E40333333333333F33F01010000801F97DD388EE35E400000000000C05E406666666666660240")
101
+ multi_point = @factory.geometry
102
+ multi_point.should be_instance_of MultiPoint
103
+ multi_point.should == MultiPoint.from_coordinates([[12.4,-123.3,4.5],[-65.1,123.4,1.2],[123.55555555,123,2.3]],444,true)
104
+ multi_point.srid.should eql(444)
105
+ multi_point[0].srid.should eql(444)
106
+ end
107
+
108
+ it "test_multi_line_string" do
109
+ @hex_ewkb_parser.parse("01050000200001000002000000010200000002000000000000000000F83F9A99999999994640E4BD6A65C20F4BC0FA7E6ABC749388BF010200000003000000000000000000F83F9A99999999994640E4BD6A65C20F4BC0FA7E6ABC749388BF39B4C876BE8F46403333333333D35E40")
110
+ multi_line_string = @factory.geometry
111
+ multi_line_string.should be_instance_of MultiLineString
112
+ multi_line_string.should == MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012]],256),LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012],[45.123,123.3]],256)],256)
113
+ multi_line_string.srid.should eql(256)
114
+ multi_line_string[0].srid.should eql(256)
115
+
116
+ @hex_ewkb_parser.parse("0105000020000100000200000001020000C002000000000000000000F83F9A99999999994640CDCCCCCCCCCCF43F333333333333F33FE4BD6A65C20F4BC0FA7E6ABC749388BF333333333333F33F000000000000124001020000C003000000000000000000F83F9A99999999994640666666666666144000000000000012C0E4BD6A65C20F4BC0FA7E6ABC749388BF3333333333331BC03333333333330B4039B4C876BE8F46403333333333D35E40000000000000124033333333333315C0")
117
+ multi_line_string = @factory.geometry
118
+ multi_line_string.should be_instance_of MultiLineString
119
+ multi_line_string.should == MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2,1.3,1.2],[-54.12312,-0.012,1.2,4.5]],256,true,true),LineString.from_coordinates([[1.5,45.2,5.1,-4.5],[-54.12312,-0.012,-6.8,3.4],[45.123,123.3,4.5,-5.3]],256,true,true)],256,true,true)
120
+ multi_line_string.srid.should eql(256)
121
+ multi_line_string[0].srid.should eql(256)
122
+ end
123
+
124
+ it "test_multi_polygon" do
125
+ @hex_ewkb_parser.parse("0106000020000100000200000001030000000200000004000000CDCCCCCCCCCC28406666666666A646C03333333333B34640CDCCCCCCCCCC44406DE7FBA9F1D211403D2CD49AE61DF13FCDCCCCCCCCCC28406666666666A646C004000000333333333333034033333333333315409A999999999915408A8EE4F21FD2F63FEC51B81E85EB2C40F6285C8FC2F5F03F3333333333330340333333333333154001030000000200000005000000000000000000000000000000000000000000000000001040000000000000000000000000000010400000000000001040000000000000000000000000000010400000000000000000000000000000000005000000000000000000F03F000000000000F03F0000000000000840000000000000F03F00000000000008400000000000000840000000000000F03F0000000000000840000000000000F03F000000000000F03F")
126
+ multi_polygon = @factory.geometry
127
+ multi_polygon.should be_instance_of MultiPolygon
128
+ multi_polygon.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]]],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)
129
+ multi_polygon.srid.should eql(256)
130
+ multi_polygon[0].srid.should eql(256)
131
+
132
+ @hex_ewkb_parser.parse("0106000020000100000200000001030000400200000004000000CDCCCCCCCCCC28406666666666A646C0333333333333F33F3333333333B34640CDCCCCCCCCCC4440333333333333F33F6DE7FBA9F1D211403D2CD49AE61DF13F333333333333F33FCDCCCCCCCCCC28406666666666A646C0333333333333F33F0400000033333333333303403333333333331540333333333333F33F9A999999999915408A8EE4F21FD2F63F333333333333F33FEC51B81E85EB2C40F6285C8FC2F5F03F333333333333F33F33333333333303403333333333331540333333333333F33F0103000040020000000500000000000000000000000000000000000000333333333333F33F00000000000010400000000000000000333333333333F33F00000000000010400000000000001040666666666666024000000000000000000000000000001040333333333333F33F00000000000000000000000000000000333333333333F33F05000000000000000000F03F000000000000F03F9A999999999901400000000000000840000000000000F03F6666666666660A40000000000000084000000000000008409A9999999999F13F000000000000F03F00000000000008403333333333330340000000000000F03F000000000000F03F9A99999999990140")
133
+ multi_polygon = @factory.geometry
134
+ multi_polygon.should be_instance_of MultiPolygon
135
+ multi_polygon.should == 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)
136
+ multi_polygon.srid.should eql(256)
137
+ multi_polygon[0].srid.should eql(256)
138
+ end
139
+
140
+
141
+ it "test_failure_trailing_data" do
142
+ #added A345 at the end
143
+ lambda {@hex_ewkb_parser.parse("01010000207B000000CDCCCCCCCCCC28406666666666A64640A345")}.should raise_error(EWKBFormatError)
144
+ end
145
+
146
+ it "test_failure_unknown_geometry_type" do
147
+ lambda {@hex_ewkb_parser.parse("01090000207B000000CDCCCCCCCCCC28406666666666A64640")}.should raise_error(EWKBFormatError)
148
+ end
149
+
150
+ it "test_failure_m" do
151
+ lambda {@hex_ewkb_parser.parse("01010000607B000000CDCCCCCCCCCC28406666666666A64640")}.should raise_error(EWKBFormatError)
152
+ end
153
+
154
+ it "test_failure_truncated_data" do
155
+ lambda {@hex_ewkb_parser.parse("01010000207B000000CDCCCCCCCCCC2840666666")}.should raise_error(EWKBFormatError)
156
+ end
157
+
158
+ end
@@ -0,0 +1,179 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe EWKTParser do
4
+
5
+ before(:each) do
6
+ @factory = GeometryFactory::new
7
+ @ewkt_parser = EWKTParser::new(@factory)
8
+ end
9
+
10
+ it "test_point" do
11
+ ewkt="POINT( 3.456 0.123)"
12
+ @ewkt_parser.parse(ewkt)
13
+ point = @factory.geometry
14
+ point.should be_instance_of Point
15
+ point.should == Point.from_x_y(3.456,0.123)
16
+ end
17
+
18
+ it "test_point_with_srid" do
19
+ ewkt="SRID=245;POINT(0.0 2.0)"
20
+ @ewkt_parser.parse(ewkt)
21
+ point = @factory.geometry
22
+ point.should be_instance_of Point
23
+ point.should == Point.from_x_y(0,2,245)
24
+ point.srid.should eql(245)
25
+ ewkt.should == point.as_ewkt(true,false)
26
+ end
27
+
28
+ it "test_point3dz" do
29
+ ewkt="POINT(3.456 0.123 123.667)"
30
+ @ewkt_parser.parse(ewkt)
31
+ point = @factory.geometry
32
+ point.should be_instance_of Point
33
+ point.should == Point.from_x_y_z(3.456,0.123,123.667)
34
+ ewkt.should == point.as_ewkt(false)
35
+ end
36
+
37
+ it "test_point3dm" do
38
+ ewkt="POINTM(3.456 0.123 123.667)"
39
+ @ewkt_parser.parse(ewkt)
40
+ point = @factory.geometry
41
+ point.should be_instance_of Point
42
+ point.should == Point.from_x_y_m(3.456,0.123,123.667)
43
+ ewkt.should == point.as_ewkt(false)
44
+ end
45
+
46
+ it "test_point4d" do
47
+ ewkt="POINT(3.456 0.123 123.667 15.0)"
48
+ @ewkt_parser.parse(ewkt)
49
+ point = @factory.geometry
50
+ point.should be_instance_of Point
51
+ point.should == Point.from_x_y_z_m(3.456,0.123,123.667,15.0)
52
+ ewkt.should == point.as_ewkt(false)
53
+ end
54
+
55
+
56
+ it "test_linestring" do
57
+ @ewkt_parser.parse("LINESTRING(3.456 0.123,123.44 123.56,54555.22 123.3)")
58
+ line_string = @factory.geometry
59
+ line_string.should be_instance_of LineString
60
+ line_string.should == LineString.from_coordinates([[3.456,0.123],[123.44,123.56],[54555.22,123.3]])
61
+
62
+ @ewkt_parser.parse("SRID=256;LINESTRING(12.4 -45.3,45.4 41.6)")
63
+ line_string = @factory.geometry
64
+ line_string.should be_instance_of LineString
65
+ line_string.should == LineString.from_coordinates([[12.4,-45.3],[45.4,41.6]],256)
66
+
67
+ @ewkt_parser.parse("SRID=256;LINESTRING(12.4 -45.3 35.3,45.4 41.6 12.3)")
68
+ line_string = @factory.geometry
69
+ line_string.should be_instance_of LineString
70
+ line_string.should == LineString.from_coordinates([[12.4,-45.3,35.3],[45.4,41.6,12.3]],256,true)
71
+
72
+ @ewkt_parser.parse("SRID=256;LINESTRINGM(12.4 -45.3 35.3,45.4 41.6 12.3)")
73
+ line_string = @factory.geometry
74
+ line_string.should be_instance_of LineString
75
+ line_string.should == LineString.from_coordinates([[12.4,-45.3,35.3],[45.4,41.6,12.3]],256,false,true)
76
+
77
+ @ewkt_parser.parse("SRID=256;LINESTRING(12.4 -45.3 35.3 25.2,45.4 41.6 12.3 13.75)")
78
+ line_string = @factory.geometry
79
+ line_string.should be_instance_of LineString
80
+ line_string.should == LineString.from_coordinates([[12.4,-45.3,35.3,25.2],[45.4,41.6,12.3,13.75]],256,true,true)
81
+ end
82
+
83
+ it "test_polygon" do
84
+ @ewkt_parser.parse("POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,3 1,3 3,1 3,1 1))")
85
+ polygon = @factory.geometry
86
+ polygon.should be_instance_of Polygon
87
+ polygon.should == Polygon.from_coordinates([[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[3,1],[3,3],[1,3],[1,1]]],256)
88
+
89
+ @ewkt_parser.parse("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))")
90
+ polygon = @factory.geometry
91
+ polygon.should be_instance_of Polygon
92
+ polygon.should == 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)
93
+
94
+ @ewkt_parser.parse("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))")
95
+ polygon = @factory.geometry
96
+ polygon.should be_instance_of Polygon
97
+ polygon.should == 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)
98
+
99
+ @ewkt_parser.parse("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))")
100
+ polygon = @factory.geometry
101
+ polygon.should be_instance_of Polygon
102
+ polygon.should == 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
+ end
104
+
105
+ it "test_multi_point" do
106
+ #Form output by the current version of PostGIS. Future versions will output the one in the specification
107
+ @ewkt_parser.parse("SRID=444;MULTIPOINT(12.4 -123.3,-65.1 123.4,123.55555555 123)")
108
+ multi_point = @factory.geometry
109
+ multi_point.should be_instance_of MultiPoint
110
+ multi_point.should == MultiPoint.from_coordinates([[12.4,-123.3],[-65.1,123.4],[123.55555555,123]],444)
111
+ multi_point.srid.should eql(444)
112
+ multi_point[0].srid.should eql(444)
113
+
114
+ @ewkt_parser.parse("SRID=444;MULTIPOINT(12.4 -123.3 4.5,-65.1 123.4 6.7,123.55555555 123 7.8)")
115
+ multi_point = @factory.geometry
116
+ multi_point.should be_instance_of MultiPoint
117
+ multi_point.should == MultiPoint.from_coordinates([[12.4,-123.3,4.5],[-65.1,123.4,6.7],[123.55555555,123,7.8]],444,true)
118
+ multi_point.srid.should eql(444)
119
+ multi_point[0].srid.should eql(444)
120
+
121
+ #Form in the EWKT specification (from the OGC)
122
+ @ewkt_parser.parse("SRID=444;MULTIPOINT( ( 12.4 -123.3 4.5 ) , (-65.1 123.4 6.7),(123.55555555 123 7.8))")
123
+ multi_point = @factory.geometry
124
+ multi_point.should be_instance_of MultiPoint
125
+ multi_point.should == MultiPoint.from_coordinates([[12.4,-123.3,4.5],[-65.1,123.4,6.7],[123.55555555,123,7.8]],444,true)
126
+ multi_point.srid.should eql(444)
127
+ multi_point[0].srid.should eql(444)
128
+ end
129
+
130
+ it "test_multi_line_string" do
131
+ @ewkt_parser.parse("SRID=256;MULTILINESTRING((1.5 45.2,-54.12312 -0.012),(1.5 45.2,-54.12312 -0.012,45.123 123.3))")
132
+ multi_line_string = @factory.geometry
133
+ multi_line_string.should be_instance_of MultiLineString
134
+ multi_line_string.should == MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012]],256),LineString.from_coordinates([[1.5,45.2],[-54.12312,-0.012],[45.123,123.3]],256)],256)
135
+ multi_line_string.srid.should eql(256)
136
+ multi_line_string[0].srid.should eql(256)
137
+
138
+ @ewkt_parser.parse("SRID=256;MULTILINESTRING((1.5 45.2 1.3 1.2,-54.12312 -0.012 1.2 4.5),(1.5 45.2 5.1 -4.5,-54.12312 -0.012 -6.8 3.4,45.123 123.3 4.5 -5.3))")
139
+ multi_line_string = @factory.geometry
140
+ multi_line_string.should be_instance_of MultiLineString
141
+ multi_line_string.should == MultiLineString.from_line_strings([LineString.from_coordinates([[1.5,45.2,1.3,1.2],[-54.12312,-0.012,1.2,4.5]],256,true,true),LineString.from_coordinates([[1.5,45.2,5.1,-4.5],[-54.12312,-0.012,-6.8,3.4],[45.123,123.3,4.5,-5.3]],256,true,true)],256,true,true)
142
+ multi_line_string.srid.should eql(256)
143
+ multi_line_string[0].srid.should eql(256)
144
+ end
145
+
146
+ it "test_multi_polygon" do
147
+ ewkt="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 0.0,4.0 0.0,4.0 4.0,0.0 4.0,0.0 0.0),(1.0 1.0,3.0 1.0,3.0 3.0,1.0 3.0,1.0 1.0)))"
148
+ @ewkt_parser.parse(ewkt)
149
+ multi_polygon = @factory.geometry
150
+ multi_polygon.should be_instance_of MultiPolygon
151
+ multi_polygon.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]]],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)
152
+ multi_polygon.srid.should eql(256)
153
+ multi_polygon[0].srid.should eql(256)
154
+ ewkt.should == multi_polygon.as_ewkt
155
+
156
+ ewkt="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)))"
157
+ @ewkt_parser.parse(ewkt)
158
+ multi_polygon = @factory.geometry
159
+ multi_polygon.should be_instance_of MultiPolygon
160
+ multi_polygon.should == 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)
161
+ multi_polygon.srid.should eql(4326)
162
+ multi_polygon[0].srid.should eql(4326)
163
+ end
164
+
165
+ it "test_geometry_collection" do
166
+ @ewkt_parser.parse("SRID=256;GEOMETRYCOLLECTION(POINT(4.67 45.4),LINESTRING(5.7 12.45,67.55 54),POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,3 1,3 3,1 3,1 1)))")
167
+ geometry_collection = @factory.geometry
168
+ geometry_collection.should be_instance_of GeometryCollection
169
+ geometry_collection.should == GeometryCollection.from_geometries([Point.from_x_y(4.67,45.4,256),LineString.from_coordinates([[5.7,12.45],[67.55,54]],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)
170
+ geometry_collection[0].srid.should eql(256)
171
+
172
+ @ewkt_parser.parse("SRID=256;GEOMETRYCOLLECTIONM(POINTM(4.67 45.4 45.6),LINESTRINGM(5.7 12.45 5.6,67.55 54 6.7))")
173
+ geometry_collection = @factory.geometry
174
+ geometry_collection.should be_instance_of GeometryCollection
175
+ geometry_collection.should == GeometryCollection.from_geometries([Point.from_x_y_m(4.67,45.4,45.6,256),LineString.from_coordinates([[5.7,12.45,5.6],[67.55,54,6.7]],256,false,true)],256,false,true)
176
+ geometry_collection[0].srid.should eql(256)
177
+ end
178
+
179
+ end