ffi-geos 0.0.1.beta1

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.
@@ -0,0 +1,53 @@
1
+
2
+ $: << File.dirname(__FILE__)
3
+ require 'test_helper'
4
+
5
+ class MiscTests < Test::Unit::TestCase
6
+ include TestHelper
7
+
8
+ def thread_tester(name, dims, byte_order, polygon, pause)
9
+ msg = proc { |*args| @messages << "#{name}: #{args.inspect}" }
10
+
11
+ 3.times {
12
+ sleep(pause)
13
+ wktr = Geos::WktReader.new
14
+ wkbw = Geos::WkbWriter.new
15
+ wkbw.byte_order = byte_order
16
+ wkbw.output_dimensions = dims
17
+ geom = wktr.read(polygon)
18
+ msg[geom.valid?]
19
+ msg[wkbw.write_hex(geom)]
20
+ GC.start
21
+ }
22
+ end
23
+
24
+ def test_multithreading
25
+ @messages = []
26
+
27
+ t1 = Thread.new {
28
+ thread_tester('t1', 2, 0, 'POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))', 0.2)
29
+ }
30
+
31
+ t2 = Thread.new {
32
+ thread_tester('t2', 3, 1, 'POLYGON((0 0 0, 0 5 0, 5 5 0, 5 10 0, 0 0 0))', 0.1)
33
+ }
34
+
35
+ t1.join
36
+ t2.join
37
+
38
+ assert_equal([
39
+ "t1: [\"000000000300000001000000050000000000000000000000000000000000000000000000004014000000000000401400000000000040140000000000004014000000000000000000000000000000000000000000000000000000000000\"]",
40
+ "t1: [\"000000000300000001000000050000000000000000000000000000000000000000000000004014000000000000401400000000000040140000000000004014000000000000000000000000000000000000000000000000000000000000\"]",
41
+ "t1: [\"000000000300000001000000050000000000000000000000000000000000000000000000004014000000000000401400000000000040140000000000004014000000000000000000000000000000000000000000000000000000000000\"]",
42
+ "t1: [true]",
43
+ "t1: [true]",
44
+ "t1: [true]",
45
+ "t2: [\"01030000800100000005000000000000000000000000000000000000000000000000000000000000000000000000000000000014400000000000000000000000000000144000000000000014400000000000000000000000000000144000000000000024400000000000000000000000000000000000000000000000000000000000000000\"]",
46
+ "t2: [\"01030000800100000005000000000000000000000000000000000000000000000000000000000000000000000000000000000014400000000000000000000000000000144000000000000014400000000000000000000000000000144000000000000024400000000000000000000000000000000000000000000000000000000000000000\"]",
47
+ "t2: [\"01030000800100000005000000000000000000000000000000000000000000000000000000000000000000000000000000000014400000000000000000000000000000144000000000000014400000000000000000000000000000144000000000000024400000000000000000000000000000000000000000000000000000000000000000\"]",
48
+ "t2: [false]",
49
+ "t2: [false]",
50
+ "t2: [false]"
51
+ ], @messages.sort)
52
+ end
53
+ end
@@ -0,0 +1,31 @@
1
+
2
+ $: << File.dirname(__FILE__)
3
+ require 'test_helper'
4
+
5
+ class PointTests < Test::Unit::TestCase
6
+ include TestHelper
7
+
8
+ def test_default_srid
9
+ geom = read('POINT(0 0)')
10
+ assert_equal(0, geom.srid)
11
+ end
12
+
13
+ def test_setting_srid_manually
14
+ geom = read('POINT(0 0)')
15
+ geom.srid = 4326
16
+ assert_equal(4326, geom.srid)
17
+ end
18
+
19
+ def test_dimensions
20
+ geom = read('POINT(1 2)')
21
+ assert_equal(0, geom.dimensions)
22
+
23
+ geom = read('POINT(1 2 3)')
24
+ assert_equal(0, geom.dimensions)
25
+ end
26
+
27
+ def test_num_geometries
28
+ geom = read('POINT(1 2)')
29
+ assert_equal(1, geom.num_geometries)
30
+ end
31
+ end
@@ -0,0 +1,28 @@
1
+
2
+ $: << File.dirname(__FILE__)
3
+ require 'test_helper'
4
+
5
+ if defined?(Geos::PreparedGeometry)
6
+ class PreparedGeometryTests < Test::Unit::TestCase
7
+ include TestHelper
8
+
9
+ def test_contains_properly
10
+ geom_a = read('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))').to_prepared
11
+ geom_b = read('POLYGON((2 2, 2 3, 3 3, 3 2, 2 2))')
12
+
13
+ assert(geom_a.contains_properly?(geom_b))
14
+
15
+ geom_a = read('POLYGON((2 2, 2 3, 3 3, 3 2, 2 2))').to_prepared
16
+ geom_b = read('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))')
17
+
18
+ assert(!geom_a.contains_properly?(geom_b))
19
+ end
20
+
21
+ def test_intersects
22
+ geom_a = read('LINESTRING(0 0, 10 10)').to_prepared
23
+ geom_b = read('LINESTRING(0 10, 10 0)')
24
+
25
+ assert(geom_a.intersects?(geom_b))
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,54 @@
1
+
2
+ $: << File.dirname(__FILE__)
3
+ require 'test_helper'
4
+
5
+ if defined?(Geos::STRtree)
6
+ class STRtreeTests < Test::Unit::TestCase
7
+ include TestHelper
8
+
9
+ def setup_tree
10
+ @tree = Geos::STRtree.new(3)
11
+ @item_1 = { :item_1 => :test }
12
+ @item_2 = [ :test ]
13
+ @item_3 = Object.new
14
+
15
+ @geom_1 = read('LINESTRING(0 0, 10 10)')
16
+ @geom_2 = read('LINESTRING(20 20, 30 30)')
17
+ @geom_3 = read('LINESTRING(20 20, 30 30)')
18
+
19
+ @tree.insert(@geom_1, @item_1)
20
+ @tree.insert(@geom_2, @item_2)
21
+ @tree.insert(@geom_3, @item_3)
22
+ end
23
+
24
+ def test_disallowed_inserts
25
+ setup_tree
26
+
27
+ @tree.query(read('POINT(5 5)'))
28
+
29
+ assert_raise(RuntimeError) do
30
+ @tree.insert(read('POINT(0 0)'), 'test')
31
+ end
32
+ end
33
+
34
+ def test_query
35
+ setup_tree
36
+
37
+ assert_equal(1, @tree.query(read('LINESTRING(5 5, 6 6)')).length)
38
+ assert_equal(0, @tree.query(read('LINESTRING(20 0, 30 10)')).length)
39
+ assert_equal(2, @tree.query(read('LINESTRING(25 25, 26 26)')).length)
40
+ assert_equal(3, @tree.query(read('LINESTRING(0 0, 100 100)')).length)
41
+ end
42
+
43
+ def test_remove
44
+ setup_tree
45
+
46
+ @tree.remove(read('POINT(5 5)'), @item_1)
47
+
48
+ assert_equal(0, @tree.query(read('LINESTRING(5 5, 6 6)')).length)
49
+ assert_equal(0, @tree.query(read('LINESTRING(20 0, 30 10)')).length)
50
+ assert_equal(2, @tree.query(read('LINESTRING(25 25, 26 26)')).length)
51
+ assert_equal(2, @tree.query(read('LINESTRING(0 0, 100 100)')).length)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,40 @@
1
+
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+
5
+ if ENV['USE_BINARY_GEOS']
6
+ require 'geos'
7
+ else
8
+ require File.join(File.dirname(__FILE__), %w{ .. lib ffi-geos })
9
+ end
10
+
11
+ puts "Ruby version #{RUBY_VERSION} - #{RbConfig::CONFIG['RUBY_INSTALL_NAME']}"
12
+ puts "GEOS version #{Geos::GEOS_VERSION}"
13
+ puts "ffi-geos version #{Geos::VERSION}" if defined?(Geos::VERSION)
14
+ if defined?(Geos::FFIGeos)
15
+ puts "Using #{Geos::FFIGeos.geos_library_paths.join(', ')}"
16
+ end
17
+
18
+ module TestHelper
19
+ TOLERANCE = 0.0000000000001
20
+
21
+ def self.included(base)
22
+ base.class_eval do
23
+ attr_reader :reader, :writer
24
+ end
25
+ end
26
+
27
+ def setup
28
+ GC.start
29
+ @reader = Geos::WktReader.new
30
+ @writer = Geos::WktWriter.new
31
+ end
32
+
33
+ def read(*args)
34
+ reader.read(*args)
35
+ end
36
+
37
+ def write(*args)
38
+ writer.write(*args)
39
+ end
40
+ end
@@ -0,0 +1,333 @@
1
+
2
+ $: << File.dirname(__FILE__)
3
+ require 'test_helper'
4
+
5
+ class UtilsTests < Test::Unit::TestCase
6
+ include TestHelper
7
+
8
+ if defined?(Geos::Utils)
9
+ if Geos::Utils.respond_to?(:orientation_index)
10
+ def test_orientation_index
11
+ assert_equal(0, Geos::Utils.orientation_index(0, 0, 10, 0, 5, 0))
12
+ assert_equal(0, Geos::Utils.orientation_index(0, 0, 10, 0, 10, 0))
13
+ assert_equal(0, Geos::Utils.orientation_index(0, 0, 10, 0, 0, 0))
14
+ assert_equal(0, Geos::Utils.orientation_index(0, 0, 10, 0, -5, 0))
15
+ assert_equal(0, Geos::Utils.orientation_index(0, 0, 10, 0, 20, 0))
16
+ assert_equal(1, Geos::Utils.orientation_index(0, 0, 10, 10, 5, 6))
17
+ assert_equal(1, Geos::Utils.orientation_index(0, 0, 10, 10, 5, 20))
18
+ assert_equal(-1, Geos::Utils.orientation_index(0, 0, 10, 10, 5, 3))
19
+ assert_equal(-1, Geos::Utils.orientation_index(0, 0, 10, 10, 5, -2))
20
+ assert_equal(1, Geos::Utils.orientation_index(0, 0, 10, 10, 1000000, 1000001))
21
+ assert_equal(-1, Geos::Utils.orientation_index(0, 0, 10, 10, 1000000, 999999))
22
+ end
23
+ end
24
+
25
+ if Geos::Utils.respond_to?(:relate_match)
26
+ def test_relate_match
27
+ assert(Geos::Utils.relate_match('0FFFFFFF2', '0FFFFFFF2'))
28
+ assert(Geos::Utils.relate_match('0FFFFFFF2', '0FFFFFFF*'))
29
+ assert(Geos::Utils.relate_match('0FFFFFFF2', 'TFFFFFFF2'))
30
+ assert(!Geos::Utils.relate_match('0FFFFFFF2', '0FFFFFFFF'))
31
+ end
32
+ end
33
+ end
34
+
35
+ def create_method_tester(expected, method, cs, type_id, klass)
36
+ geom = Geos.send(method, cs)
37
+ expected_geom = read(expected)
38
+
39
+ assert(expected_geom.eql_exact?(geom, TOLERANCE))
40
+ assert(geom.valid?)
41
+ assert_instance_of(klass, geom)
42
+ assert_equal(type_id, geom.type_id)
43
+
44
+ yield geom if block_given?
45
+ end
46
+
47
+ def test_create_point
48
+ cs = Geos::CoordinateSequence.new(1, 2)
49
+ cs.set_x(0, 10)
50
+ cs.set_y(0, 20)
51
+
52
+ create_method_tester('POINT(10 20)', :create_point, cs, Geos::GEOS_POINT, Geos::Point)
53
+ end
54
+
55
+ def test_bad_create_point
56
+ cs = Geos::CoordinateSequence.new(0, 0)
57
+ assert_raise(RuntimeError) do
58
+ geom = Geos.create_point(cs)
59
+ end
60
+ end
61
+
62
+ def test_create_line_string
63
+ cs = Geos::CoordinateSequence.new(2, 3)
64
+ cs.set_x(0, 10)
65
+ cs.set_y(0, 20)
66
+ cs.set_z(0, 30)
67
+ cs.set_x(1, 30)
68
+ cs.set_y(1, 20)
69
+ cs.set_z(1, 10)
70
+
71
+ create_method_tester(
72
+ 'LINESTRING (10 20 30, 30 20 10)',
73
+ :create_line_string,
74
+ cs,
75
+ Geos::GEOS_LINESTRING,
76
+ Geos::LineString
77
+ ) do |geom|
78
+ assert(!geom.empty?)
79
+ assert(geom.valid?)
80
+ assert(geom.simple?)
81
+ assert(!geom.ring?)
82
+ assert(geom.has_z?)
83
+ assert_equal(1, geom.num_geometries)
84
+ end
85
+ end
86
+
87
+ def test_create_bad_line_string
88
+ cs = Geos::CoordinateSequence.new(1, 0)
89
+ assert_raise(RuntimeError) do
90
+ geom = Geos::create_line_string(cs)
91
+ end
92
+ end
93
+
94
+ def test_create_linear_ring
95
+ cs = Geos::CoordinateSequence.new(4,3)
96
+ cs.set_x(0, 7)
97
+ cs.set_y(0, 8)
98
+ cs.set_z(0, 9)
99
+ cs.set_x(1, 3)
100
+ cs.set_y(1, 3)
101
+ cs.set_z(1, 3)
102
+ cs.set_x(2, 11)
103
+ cs.set_y(2, 15.2)
104
+ cs.set_z(2, 2)
105
+ cs.set_x(3, 7)
106
+ cs.set_y(3, 8)
107
+ cs.set_z(3, 9)
108
+
109
+ create_method_tester(
110
+ 'LINEARRING (7 8 9, 3 3 3, 11 15.2 2, 7 8 9)',
111
+ :create_linear_ring,
112
+ cs,
113
+ Geos::GEOS_LINEARRING,
114
+ Geos::LinearRing
115
+ ) do |geom|
116
+ assert(!geom.empty?)
117
+ assert(geom.valid?)
118
+ assert(geom.simple?)
119
+ assert(geom.ring?)
120
+ assert(geom.has_z?)
121
+ assert_equal(1, geom.num_geometries)
122
+ end
123
+ end
124
+
125
+ def test_bad_create_linear_ring
126
+ cs = Geos::CoordinateSequence.new(1, 0)
127
+
128
+ assert_raise(RuntimeError) do
129
+ geom = Geos::create_linear_ring(cs)
130
+ end
131
+ end
132
+
133
+ def test_create_polygon
134
+ cs = Geos::CoordinateSequence.new(5, 2)
135
+ cs.set_x(0, 0)
136
+ cs.set_y(0, 0)
137
+
138
+ cs.set_x(1, 0)
139
+ cs.set_y(1, 10)
140
+
141
+ cs.set_x(2, 10)
142
+ cs.set_y(2, 10)
143
+
144
+ cs.set_x(3, 10)
145
+ cs.set_y(3, 0)
146
+
147
+ cs.set_x(4, 0)
148
+ cs.set_y(4, 0)
149
+
150
+ exterior_ring = Geos::create_linear_ring(cs)
151
+
152
+ geom = Geos::create_polygon(exterior_ring)
153
+ assert_instance_of(Geos::Polygon, geom)
154
+ assert_equal('Polygon', geom.geom_type)
155
+ assert_equal(Geos::GEOS_POLYGON, geom.type_id)
156
+ assert(read('POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))').eql_exact?(geom, TOLERANCE))
157
+ end
158
+
159
+ def test_create_polygon_with_holes
160
+ create_ring = lambda { |*points|
161
+ Geos.create_linear_ring(
162
+ Geos::CoordinateSequence.new(points.length, 2).tap { |cs|
163
+ points.each_with_index do |(x, y), i|
164
+ cs.set_x(i, x)
165
+ cs.set_y(i, y)
166
+ end
167
+ }
168
+ )
169
+ }
170
+
171
+ exterior_ring = create_ring[
172
+ [ 0, 0 ],
173
+ [ 0, 10 ],
174
+ [ 10, 10 ],
175
+ [ 10, 0 ],
176
+ [ 0, 0 ]
177
+ ]
178
+
179
+ hole_1 = create_ring[
180
+ [ 2, 2 ],
181
+ [ 2, 4 ],
182
+ [ 4, 4 ],
183
+ [ 4, 2 ],
184
+ [ 2, 2 ]
185
+ ]
186
+
187
+ hole_2 = create_ring[
188
+ [ 6, 6 ],
189
+ [ 6, 8 ],
190
+ [ 8, 8 ],
191
+ [ 8, 6 ],
192
+ [ 6, 6 ]
193
+ ]
194
+
195
+ geom = Geos::create_polygon(exterior_ring, [ hole_1, hole_2 ])
196
+ assert_instance_of(Geos::Polygon, geom)
197
+ assert_equal('Polygon', geom.geom_type)
198
+ assert_equal(Geos::GEOS_POLYGON, geom.type_id)
199
+
200
+ assert(!geom.empty?)
201
+ assert(geom.valid?)
202
+ assert(geom.simple?)
203
+ assert(!geom.ring?)
204
+ assert(!geom.has_z?)
205
+
206
+ assert_equal(1, geom.num_geometries)
207
+ end
208
+
209
+ if ENV['FORCE_TESTS'] || Geos.method_defined?(:create_multi_point)
210
+ def test_create_multi_point
211
+ @writer.rounding_precision = 0
212
+ assert_equal('MULTIPOINT EMPTY', write(Geos.create_multi_point))
213
+ assert_equal('MULTIPOINT (0 0, 10 10)', write(Geos.create_multi_point(
214
+ read('POINT(0 0)'),
215
+ read('POINT(10 10)')
216
+ )))
217
+ end
218
+
219
+ def test_create_bad_multi_point
220
+ assert_raise(TypeError) do
221
+ Geos.create_multi_point(
222
+ read('POINT(0 0)'),
223
+ read('POLYGON((10 10, 10 15, 15 15, 15 10, 10 10))')
224
+ )
225
+ end
226
+ end
227
+ end
228
+
229
+ if ENV['FORCE_TESTS'] || Geos.method_defined?(:create_multi_line_string)
230
+ def test_create_multi_line_string
231
+ @writer.rounding_precision = 0
232
+ assert_equal('MULTILINESTRING EMPTY', write(Geos.create_multi_line_string))
233
+ assert_equal('MULTILINESTRING ((0 0, 10 10), (10 10, 20 20))', write(Geos.create_multi_line_string(
234
+ read('LINESTRING(0 0, 10 10)'),
235
+ read('LINESTRING(10 10, 20 20)')
236
+ )))
237
+ end
238
+
239
+ def test_create_bad_multi_line_string
240
+ assert_raise(TypeError) do
241
+ Geos.create_multi_point(
242
+ read('POINT(0 0)'),
243
+ read('LINESTRING(0 0, 10 0)')
244
+ )
245
+ end
246
+ end
247
+ end
248
+
249
+ if ENV['FORCE_TESTS'] || Geos.method_defined?(:create_multi_polygon)
250
+ def test_create_multi_polygon
251
+ @writer.rounding_precision = 0
252
+ assert_equal('MULTIPOLYGON EMPTY', write(Geos.create_multi_polygon))
253
+ assert_equal('MULTIPOLYGON (((0 0, 0 5, 5 5, 5 0, 0 0)), ((10 10, 10 15, 15 15, 15 10, 10 10)))', write(Geos.create_multi_polygon(
254
+ read('POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))'),
255
+ read('POLYGON((10 10, 10 15, 15 15, 15 10, 10 10))')
256
+ )))
257
+ end
258
+
259
+ def test_create_bad_multi_polygon
260
+ assert_raise(TypeError) do
261
+ Geos.create_multi_polygon(
262
+ read('POINT(0 0)'),
263
+ read('POLYGON((10 10, 10 15, 15 15, 15 10, 10 10))')
264
+ )
265
+ end
266
+ end
267
+ end
268
+
269
+ if ENV['FORCE_TESTS'] || Geos.method_defined?(:create_geometry_collection)
270
+ def test_create_geometry_collection
271
+ @writer.rounding_precision = 0
272
+ assert_equal('GEOMETRYCOLLECTION EMPTY', write(Geos.create_geometry_collection))
273
+ assert_equal('GEOMETRYCOLLECTION (POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0)), POLYGON ((10 10, 10 15, 15 15, 15 10, 10 10)))',
274
+ write(Geos.create_geometry_collection(
275
+ read('POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))'),
276
+ read('POLYGON((10 10, 10 15, 15 15, 15 10, 10 10))')
277
+ ))
278
+ )
279
+ end
280
+
281
+ def test_create_bad_geometry_collection
282
+ assert_raise(TypeError) do
283
+ Geos.create_geometry_collection(
284
+ read('POINT(0 0)'),
285
+ read('POLYGON((10 10, 10 15, 15 15, 15 10, 10 10))'),
286
+ 'gibberish'
287
+ )
288
+ end
289
+ end
290
+ end
291
+
292
+ if ENV['FORCE_TESTS'] || Geos.method_defined?(:create_empty_point)
293
+ def test_create_empty_point
294
+ assert_equal('POINT EMPTY', write(Geos.create_empty_point))
295
+ end
296
+ end
297
+
298
+ if ENV['FORCE_TESTS'] || Geos.method_defined?(:create_empty_line_string)
299
+ def test_create_empty_line_string
300
+ assert_equal('LINESTRING EMPTY', write(Geos.create_empty_line_string))
301
+ end
302
+ end
303
+
304
+ if ENV['FORCE_TESTS'] || Geos.method_defined?(:create_empty_polygon)
305
+ def test_create_empty_polygon
306
+ assert_equal('POLYGON EMPTY', write(Geos.create_empty_polygon))
307
+ end
308
+ end
309
+
310
+ if ENV['FORCE_TESTS'] || Geos.method_defined?(:create_empty_multi_point)
311
+ def test_create_empty_multi_point
312
+ assert_equal('MULTIPOINT EMPTY', write(Geos.create_empty_multi_point))
313
+ end
314
+ end
315
+
316
+ if ENV['FORCE_TESTS'] || Geos.method_defined?(:create_empty_multi_line_string)
317
+ def test_create_empty_multi_line_string
318
+ assert_equal('MULTILINESTRING EMPTY', write(Geos.create_empty_multi_line_string))
319
+ end
320
+ end
321
+
322
+ if ENV['FORCE_TESTS'] || Geos.method_defined?(:create_empty_multi_polygon)
323
+ def test_create_empty_multi_polygon
324
+ assert_equal('MULTIPOLYGON EMPTY', write(Geos.create_empty_multi_polygon))
325
+ end
326
+ end
327
+
328
+ if ENV['FORCE_TESTS'] || Geos.method_defined?(:create_empty_geometry_collection)
329
+ def test_create_empty_geometry_collection
330
+ assert_equal('GEOMETRYCOLLECTION EMPTY', write(Geos.create_empty_geometry_collection))
331
+ end
332
+ end
333
+ end