shapelib 0.7.0 → 0.7.2

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.
data/test/ztest1.rb DELETED
@@ -1,253 +0,0 @@
1
- require 'shapelib'
2
- require 'libtest.rb'
3
-
4
- def test1
5
- testblock('test1') do
6
- file_delete(*%w(test1.dbf test1.shp test1.shx))
7
- s = ShapeLib::ShapeFile.new('test1.shp', :Point, [["id", :Integer, 8]])
8
- assert(s.field_count, 1)
9
- s.add_field 'name', ShapeLib::String, 12
10
- assert(s.field_count, 2)
11
- assert(s.field_index('name'), 1)
12
- assert(s.field_type('name'), :String)
13
- assert(s.field_type(1), :String)
14
- s.add_field 'k', ShapeLib::Integer, 3
15
- assert(s.field_count, 3)
16
- s.add_field 'y', "Float", 8, 3
17
- assert(s.field_index('y'), 3)
18
- assert(s.field_type('y'), :Float)
19
- assert(s.field_count, 4)
20
- assert(s.size, 0)
21
-
22
- shape = ShapeLib::Point.new(100, 101, 'id' => 234)
23
- shape['name'] = 'foo'
24
- shape['k'] = 18
25
- fval = shape['y'] = 31.415926
26
- assert(s.write(shape), 0)
27
- assert(s.size, 1)
28
- assert(shape.x, 100.0)
29
-
30
- assert(s.fields, %w(id name k y))
31
- assert(s.field_type(1), :String)
32
- assert(s.field_name(1), 'name')
33
-
34
- assert(shape['id'], 234)
35
- assert(shape['name'], 'foo')
36
- assert(shape['k'], 18)
37
- assert(shape['y'], fval)
38
- assert(shape['unspecified'], nil)
39
-
40
- shape = ShapeLib.new_point(200, 201)
41
- assert(s.write(shape), 1)
42
- assert(s.size, 2)
43
-
44
- assert(shape['id'], nil)
45
- assert(shape.x, 200.0)
46
-
47
- shape = s.read(0)
48
- assert(shape.shape_id, 0)
49
- assert(shape['id'], 234)
50
- assert(shape['name'], 'foo')
51
- assert(shape['k'], 18)
52
- # note that only 3 digits after decimal point is conserved.
53
- assert(shape['y'], 31.416)
54
- assert(shape['unspecified'], nil)
55
- assert(shape['unspecified2'], nil)
56
-
57
- assert s.maxbound, [200, 201, 0, 0]
58
-
59
- s.close
60
-
61
- assert_file 'test1.dbf', <<EOF
62
- 035f071a02000000a10020000000000000000000000000000000000000000000
63
- 69640000000000000000004e0000000008000000000000000000000000000000
64
- 6e616d650000000000000043000000000c000000000000000000000000000000
65
- 6b000000000000000000004e0000000003000000000000000000000000000000
66
- 79000000000000000000004e0000000008030000000000000000000000000000
67
- 0d202020202020323334666f6f202020202020202020203138202033312e3431
68
- 36202a2a2a2a2a2a2a2a0000000000000000000000002a2a2a2a2a2a2a2a2a2a
69
- 2a
70
- EOF
71
- assert_file 'test1.shp', <<EOF
72
- 0000270a00000000000000000000000000000000000000000000004ee8030000
73
- 0100000000000000000059400000000000405940000000000000694000000000
74
- 0020694000000000000000000000000000000000000000000000000000000000
75
- 00000000000000010000000a0100000000000000000059400000000000405940
76
- 000000020000000a0100000000000000000069400000000000206940
77
- EOF
78
- assert_file 'test1.shx', <<EOF
79
- 0000270a00000000000000000000000000000000000000000000003ae8030000
80
- 0100000000000000000059400000000000405940000000000000694000000000
81
- 0020694000000000000000000000000000000000000000000000000000000000
82
- 00000000000000320000000a000000400000000a
83
- EOF
84
-
85
- file_delete(*%w(test1.dbf test1.shp test1.shx))
86
- end
87
- end
88
-
89
- def test2
90
- testblock('test2') do
91
- file_delete(*%w(test2.dbf test2.shp test2.shx))
92
- s = ShapeLib::ShapeFile.new('test2.shp', ShapeLib::PointM)
93
- assert(s.class.to_s, "ShapeLib::ShapeFile")
94
- assert(s.field_count, 0)
95
- s.add_field 'name', ShapeLib::String, 12
96
- assert(s.field_count, 1)
97
- assert(s.size, 0)
98
-
99
- shape = ShapeLib.new_point(301, 302, 303)
100
- assert(shape.class.to_s, "ShapeLib::PointM")
101
- s.write(shape)
102
- assert(s.size, 1)
103
- assert(shape.m, 303)
104
-
105
- shape = ShapeLib::PointM.new(401, 402, 403)
106
- assert(shape.class.to_s, "ShapeLib::PointM")
107
- s.write(shape)
108
- assert(s.size, 2)
109
-
110
- s.close
111
- assert_file 'test2.shp', <<EOF
112
- 0000270a000000000000000000000000000000000000000000000056e8030000
113
- 150000000000000000d072400000000000e07240000000000010794000000000
114
- 00207940000000000000000000000000000000000000000000f0724000000000
115
- 00307940000000010000000e150000000000000000d072400000000000e07240
116
- 0000000000f07240000000020000000e15000000000000000010794000000000
117
- 002079400000000000307940
118
- EOF
119
-
120
- sx = ShapeLib::ShapeFile.open('test2.shp')
121
- assert sx.shape_type, :PointM
122
- assert sx.minbound, [301.0, 302.0, 303.0, 0]
123
- assert sx.maxbound, [401.0, 402.0, 403.0, 0]
124
- sx.close
125
-
126
- file_delete(*%w(test2.dbf test2.shp test2.shx))
127
- end
128
- end
129
-
130
- def testA1
131
- testblock('testA1') do
132
- file_delete(*%w(testa1.dbf testa1.shp testa1.shx))
133
- s = ShapeLib::ShapeFile.new('testa1.shp', "Arc")
134
- assert(s.class.to_s, "ShapeLib::ShapeFile")
135
- assert(s.field_count, 0)
136
- s.add_field 'name', ShapeLib::String, 12
137
- assert(s.field_count, 1)
138
- assert(s.size, 0)
139
-
140
- p1 = ShapeLib.new_point(1.1, 1.2)
141
- p2 = ShapeLib.new_point(2.1, 2.2)
142
- p3 = ShapeLib.new_point(3.1, 3.2)
143
- shape = ShapeLib::Arc.new([p1, p2, p3])
144
- assert(shape.class.to_s, 'ShapeLib::Arc')
145
- assert(shape.n_parts, 1)
146
- assert(shape.n_vertices, 3)
147
- assert(shape.shape_id, nil)
148
- assert(shape.shape_type, :Arc)
149
- assert(s.write(shape), 0)
150
-
151
- arc2 = [[101, 102], [111, 112], [121, 122]]
152
- shape = ShapeLib::Arc.new([p1, p3], arc2)
153
- assert(shape.n_parts, 2)
154
- assert(s.write(shape), 1)
155
-
156
- assert(shape.shape_id, nil)
157
- assert(s.read(1).shape_id, 1)
158
-
159
- file_delete(*%w(testa1.dbf testa1.shp testa1.shx))
160
- end
161
- end
162
-
163
- def test3
164
- testblock('test3') do
165
- pt = ShapeLib::Point.new 4324434.13545, 3495423412.4324324
166
- assert(pt.wkt, "POINT(4324434.135 3495423412)")
167
- assert(pt.part_type, nil)
168
- mp = ShapeLib::MultiPoint.new [1, 2], [3, 4], [5, 6]
169
- assert(mp.wkt, "MULTIPOINT(1 2, 3 4, 5 6)")
170
-
171
- ar = ShapeLib::Arc.new [1, 2], [3, 4], [5, 6]
172
- assert(ar.wkt, "LINESTRING(1 2, 3 4, 5 6)")
173
- assert(ar.xvals, [1, 3, 5])
174
- assert(ar.yvals, [2, 4, 6])
175
- ar = ShapeLib::Arc.new([[1, 2], [3, 4], [5, 6]])
176
- assert(ar.wkt, "LINESTRING(1 2, 3 4, 5 6)")
177
- ar = ShapeLib::Arc.new([[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10]])
178
- assert(ar.wkt, "MULTILINESTRING((1 2, 3 4, 5 6), (7 8, 9 10))")
179
- assert ar.xvals, [1, 3, 5, 7, 9]
180
-
181
- ar2 = ShapeLib::Shape.new(arh = ar.to_h)
182
- assert(ar2.class.to_s, "ShapeLib::Arc")
183
- assert(ar2.to_h, arh)
184
- assert(ar2.wkt, ar.wkt)
185
-
186
- arm = ShapeLib::ArcM.new [1, 2, 3], [4, 5, 6], [7, 8, 9]
187
- assert arm.mvals, [3, 6, 9]
188
- arm = ShapeLib::ArcM.new [1, 2], [4, 5], [7, 8]
189
- assert arm.mvals, [nil, nil, nil]
190
-
191
- pl = ShapeLib::Polygon.new [1, 2], [3, 4], [5, 6.5], [1, 2]
192
- assert(pl.wkt, "POLYGON((1 2, 3 4, 5 6.5, 1 2))")
193
- assert(pl.rewind_polygon, true)
194
- assert(pl.wkt, "POLYGON((1 2, 5 6.5, 3 4, 1 2))")
195
- pl = ShapeLib::Polygon.new [[1, 2], [3, 4], [5, 6.5], [1, 2]]
196
- assert(pl.wkt, "POLYGON((1 2, 3 4, 5 6.5, 1 2))")
197
- pl = ShapeLib::Polygon.new([[1, 2], [3, 4], [5, 6.5], [1, 2]],
198
- [[7, 8], [9, 10], [11, 12.5], [7, 8]])
199
- assert(pl.part_type, [:Ring, :Ring])
200
- assert(pl.wkt,
201
- "POLYGON((1 2, 3 4, 5 6.5, 1 2), (7 8, 9 10, 11 12.5, 7 8))")
202
-
203
- pl['abbrev'] = 'WI'
204
- pl['state'] = 'Wisconsin'
205
- pl['update'] = '2006-02-16'
206
- assert ShapeLib::Shape.new(pl.to_h).to_h, pl.to_h
207
-
208
- mq = ShapeLib::MultiPatch.new :OuterRing, [1, 2], [3, 4], [5, 6.5], [1, 2]
209
- assert(mq.wkt, "POLYGON((1 2, 3 4, 5 6.5, 1 2))")
210
- end
211
- end
212
-
213
- def test4
214
- testblock('test4') do
215
- file_delete(*%w(test4.dbf test4.shp test4.shx))
216
- s = ShapeLib::ShapeFile.new('test4.shp', :PointM)
217
- assert(s.class.to_s, "ShapeLib::ShapeFile")
218
- assert(s.field_count, 0)
219
- assert(s.size, 0)
220
- # shape = ShapeLib.new_point(301, 302, 303, 'foo' => 123)
221
- shape = ShapeLib.new_point(301, 302, 303)
222
- assert(s.write(shape), 0)
223
- shape = ShapeLib::PointM.new(401, 402, 403)
224
- assert(s.write(shape), 1)
225
- s.close
226
-
227
- fp = ShapeLib::ShapeFile.open('test4')
228
- assert fp.size, 2
229
- shape = fp.read(0)
230
- assert(shape.class.to_s, "ShapeLib::Shape")
231
- assert fp.size, 2
232
- fp.close
233
-
234
- ShapeLib::ShapeFile.open('test4.shp') do |fp|
235
- shape = fp.read
236
- assert(shape.shape_id, 0)
237
- shape = fp.read
238
- assert(shape.shape_id, 1)
239
- assert(fp.read, nil)
240
- i = 0
241
- fp.each do |shape|
242
- assert shape.shape_id, i
243
- i += 1
244
- end
245
- assert i, 2
246
- end
247
-
248
- file_delete(*%w(test4.dbf test4.shp test4.shx))
249
- end
250
- end
251
-
252
-
253
- exit(test1 + test2 + testA1 + test3 + test4)