ffi-geos 0.0.6 → 0.1.0

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.
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Geos
3
- VERSION = "0.0.6"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
 
@@ -18,12 +18,16 @@ module Geos
18
18
  )
19
19
  end
20
20
 
21
- def read(wkb)
22
- cast_geometry_ptr(FFIGeos.GEOSWKBReader_read_r(Geos.current_handle, self.ptr, wkb, wkb.bytesize))
21
+ def read(wkb, options = {})
22
+ cast_geometry_ptr(FFIGeos.GEOSWKBReader_read_r(Geos.current_handle, self.ptr, wkb, wkb.bytesize), {
23
+ :srid => options[:srid]
24
+ })
23
25
  end
24
26
 
25
- def read_hex(wkb)
26
- cast_geometry_ptr(FFIGeos.GEOSWKBReader_readHEX_r(Geos.current_handle, self.ptr, wkb, wkb.bytesize))
27
+ def read_hex(wkb, options = {})
28
+ cast_geometry_ptr(FFIGeos.GEOSWKBReader_readHEX_r(Geos.current_handle, self.ptr, wkb, wkb.bytesize), {
29
+ :srid => options[:srid]
30
+ })
27
31
  end
28
32
 
29
33
  def self.release(ptr) #:nodoc:
@@ -18,8 +18,10 @@ module Geos
18
18
  )
19
19
  end
20
20
 
21
- def read(wkt)
22
- cast_geometry_ptr(FFIGeos.GEOSWKTReader_read_r(Geos.current_handle, self.ptr, wkt))
21
+ def read(wkt, options = {})
22
+ cast_geometry_ptr(FFIGeos.GEOSWKTReader_read_r(Geos.current_handle, self.ptr, wkt), {
23
+ :srid => options[:srid]
24
+ })
23
25
  end
24
26
 
25
27
  def self.release(ptr) #:nodoc:
@@ -2,7 +2,7 @@
2
2
  $: << File.dirname(__FILE__)
3
3
  require 'test_helper'
4
4
 
5
- class CoordinateSequenceTests < Test::Unit::TestCase
5
+ class CoordinateSequenceTests < MiniTest::Unit::TestCase
6
6
  include TestHelper
7
7
 
8
8
  def setup
@@ -44,29 +44,29 @@ class CoordinateSequenceTests < Test::Unit::TestCase
44
44
  end
45
45
 
46
46
  def test_check_bounds
47
- assert_raise(RuntimeError) { @cs.set_x(10, 0.1) }
48
- assert_raise(RuntimeError) { @cs.set_x(-1, 0.1) }
47
+ assert_raises(RuntimeError) { @cs.set_x(10, 0.1) }
48
+ assert_raises(RuntimeError) { @cs.set_x(-1, 0.1) }
49
49
 
50
- assert_raise(RuntimeError) { @cs.set_y(10, 0.1) }
51
- assert_raise(RuntimeError) { @cs.set_y(-1, 0.1) }
50
+ assert_raises(RuntimeError) { @cs.set_y(10, 0.1) }
51
+ assert_raises(RuntimeError) { @cs.set_y(-1, 0.1) }
52
52
 
53
- assert_raise(RuntimeError) { @cs.set_z(10, 0.1) }
54
- assert_raise(RuntimeError) { @cs.set_z(-1, 0.1) }
53
+ assert_raises(RuntimeError) { @cs.set_z(10, 0.1) }
54
+ assert_raises(RuntimeError) { @cs.set_z(-1, 0.1) }
55
55
 
56
- assert_raise(RuntimeError) { @cs.set_ordinate(10, 0, 0.1) }
57
- assert_raise(RuntimeError) { @cs.set_ordinate(-1, 0, 0.1) }
56
+ assert_raises(RuntimeError) { @cs.set_ordinate(10, 0, 0.1) }
57
+ assert_raises(RuntimeError) { @cs.set_ordinate(-1, 0, 0.1) }
58
58
 
59
- assert_raise(RuntimeError) { @cs.get_x(10) }
60
- assert_raise(RuntimeError) { @cs.get_x(-1) }
59
+ assert_raises(RuntimeError) { @cs.get_x(10) }
60
+ assert_raises(RuntimeError) { @cs.get_x(-1) }
61
61
 
62
- assert_raise(RuntimeError) { @cs.get_y(10) }
63
- assert_raise(RuntimeError) { @cs.get_y(-1) }
62
+ assert_raises(RuntimeError) { @cs.get_y(10) }
63
+ assert_raises(RuntimeError) { @cs.get_y(-1) }
64
64
 
65
- assert_raise(RuntimeError) { @cs.get_z(10) }
66
- assert_raise(RuntimeError) { @cs.get_z(-1) }
65
+ assert_raises(RuntimeError) { @cs.get_z(10) }
66
+ assert_raises(RuntimeError) { @cs.get_z(-1) }
67
67
 
68
- assert_raise(RuntimeError) { @cs.get_ordinate(10, 0) }
69
- assert_raise(RuntimeError) { @cs.get_ordinate(-1, 0) }
68
+ assert_raises(RuntimeError) { @cs.get_ordinate(10, 0) }
69
+ assert_raises(RuntimeError) { @cs.get_ordinate(-1, 0) }
70
70
  end
71
71
 
72
72
  def test_clone
@@ -109,14 +109,14 @@ class CoordinateSequenceTests < Test::Unit::TestCase
109
109
  assert_equal(2, cs.dimensions)
110
110
  assert_equal(5, cs.length)
111
111
 
112
- assert_raise(Geos::CoordinateSequence::ParseError) do
112
+ assert_raises(Geos::CoordinateSequence::ParseError) do
113
113
  cs = Geos::CoordinateSequence.new([
114
114
  [ 1, 2 ],
115
115
  [ 1, 2, 3 ]
116
116
  ])
117
117
  end
118
118
 
119
- assert_raise(Geos::CoordinateSequence::ParseError) do
119
+ assert_raises(Geos::CoordinateSequence::ParseError) do
120
120
  cs = Geos::CoordinateSequence.new([
121
121
  [ 1, 2, 3, 4 ]
122
122
  ])
@@ -142,10 +142,10 @@ class CoordinateSequenceTests < Test::Unit::TestCase
142
142
 
143
143
  def test_empty
144
144
  cs = Geos::CoordinateSequence.new
145
- assert(cs.empty?)
145
+ assert_geom_empty(cs)
146
146
 
147
147
  cs = Geos::CoordinateSequence.new([4,1])
148
- assert(!cs.empty?)
148
+ refute_geom_empty(cs)
149
149
  end
150
150
 
151
151
  def test_to_empty_linear_ring
@@ -209,15 +209,15 @@ class CoordinateSequenceTests < Test::Unit::TestCase
209
209
  assert_equal('NaN', cs.z[0].to_s)
210
210
  assert_equal('NaN', cs.z[1].to_s)
211
211
 
212
- assert_raise(RuntimeError) do
212
+ assert_raises(RuntimeError) do
213
213
  cs.x[100]
214
214
  end
215
215
 
216
- assert_raise(RuntimeError) do
216
+ assert_raises(RuntimeError) do
217
217
  cs.y[100]
218
218
  end
219
219
 
220
- assert_raise(RuntimeError) do
220
+ assert_raises(RuntimeError) do
221
221
  cs.z[100]
222
222
  end
223
223
  end
@@ -236,15 +236,15 @@ class CoordinateSequenceTests < Test::Unit::TestCase
236
236
  assert_equal(2, cs.get_y(0))
237
237
  assert_equal(11, cs.get_y(1))
238
238
 
239
- assert_raise(RuntimeError) do
239
+ assert_raises(RuntimeError) do
240
240
  cs.x[100] = 10
241
241
  end
242
242
 
243
- assert_raise(RuntimeError) do
243
+ assert_raises(RuntimeError) do
244
244
  cs.y[100] = 10
245
245
  end
246
246
 
247
- assert_raise(RuntimeError) do
247
+ assert_raises(RuntimeError) do
248
248
  cs.z[100] = 10
249
249
  end
250
250
  end
@@ -271,4 +271,58 @@ class CoordinateSequenceTests < Test::Unit::TestCase
271
271
  assert_kind_of(Enumerable, cs.to_enum)
272
272
  assert_equal(cs, cs.each {})
273
273
  end
274
+
275
+ def test_array_like_access
276
+ cs = Geos::CoordinateSequence.new([
277
+ [ 0, 1 ],
278
+ [ 2, 3 ]
279
+ ])
280
+
281
+ assert_equal(0, cs[0][0])
282
+ assert_equal(1, cs[0][1])
283
+ assert_equal(2, cs[1][0])
284
+ assert_equal(3, cs[1][1])
285
+
286
+ cs = Geos::CoordinateSequence.new([
287
+ [ 4, 5, 6 ]
288
+ ])
289
+
290
+ assert_equal(4, cs[0][0])
291
+ assert_equal(5, cs[0][1])
292
+ assert_equal(6, cs[0][2])
293
+ end
294
+
295
+ def test_slice
296
+ cs = Geos::CoordinateSequence.new([
297
+ [ 0, 1 ],
298
+ [ 2, 3 ],
299
+ [ 4, 5 ]
300
+ ])
301
+
302
+ assert_equal([[0, 1], [2, 3]], cs.slice(0..1))
303
+ assert_equal([4, 5], cs.slice(-1))
304
+ assert_equal([[0, 1], [2, 3]], cs.slice(0, 2))
305
+ end
306
+
307
+ def test_proxy_clone
308
+ cs = Geos::CoordinateSequence.new([ 10, 20 ])
309
+ cs2 = cs.clone
310
+
311
+ cs.x[0] = 100
312
+
313
+ assert_equal(100, cs.x[0])
314
+ assert_equal(10, cs2.x[0])
315
+
316
+ refute_equal(cs.x, cs2.x)
317
+ refute_equal(cs.y, cs2.y)
318
+ end
319
+
320
+ def test_has_z
321
+ assert_geom_has_z(Geos::CoordinateSequence.new([ 0, 1, 2 ]))
322
+ refute_geom_has_z(Geos::CoordinateSequence.new([ 0, 1 ]))
323
+ refute_geom_has_z(Geos::CoordinateSequence.new(1, 2))
324
+ assert_geom_has_z(Geos::CoordinateSequence.new(1, 3))
325
+ assert_geom_has_z(read('POINT (0 0 0)').coord_seq)
326
+ refute_geom_has_z(read('POINT (0 0)').coord_seq)
327
+ end
274
328
  end
@@ -0,0 +1,79 @@
1
+
2
+ $: << File.dirname(__FILE__)
3
+ require 'test_helper'
4
+
5
+ class GeometryCollectionTests < MiniTest::Unit::TestCase
6
+ include TestHelper
7
+
8
+ if ENV['FORCE_TESTS'] || Geos::GeometryCollection.method_defined?(:[])
9
+ def test_geometry_collection_enumerator
10
+ geom = read('GEOMETRYCOLLECTION(POINT(0 0))')
11
+ assert_kind_of(Enumerable, geom.each)
12
+ assert_kind_of(Enumerable, geom.to_enum)
13
+ assert_equal(geom, geom.each {})
14
+ end
15
+
16
+ def test_geometry_collection_array
17
+ writer.trim = true
18
+ geom = read('GEOMETRYCOLLECTION(
19
+ LINESTRING(0 0, 1 1, 2 2, 3 3),
20
+ POINT(10 20),
21
+ POLYGON((0 0, 0 5, 5 5, 5 0, 0 0)),
22
+ POINT(10 20)
23
+ )')
24
+
25
+ assert_equal('LINESTRING (0 0, 1 1, 2 2, 3 3)', write(geom[0]))
26
+ assert_equal('POINT (10 20)', write(geom[-1]))
27
+
28
+ assert_equal([
29
+ 'LINESTRING (0 0, 1 1, 2 2, 3 3)',
30
+ 'POINT (10 20)'
31
+ ], geom[0, 2].collect { |g| write(g) })
32
+
33
+ assert_equal(nil, geom[0, -1])
34
+ assert_equal([], geom[-1, 0])
35
+ assert_equal([
36
+ 'POINT (10 20)',
37
+ 'POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))'
38
+ ], geom[1..2].collect { |g| write(g) })
39
+ end
40
+ end
41
+
42
+ if ENV['FORCE_TESTS'] || Geos::GeometryCollection.method_defined?(:detect)
43
+ def test_geometry_collection_enumerable
44
+ writer.trim = true
45
+ geom = read('GEOMETRYCOLLECTION(
46
+ LINESTRING(0 0, 1 1, 2 2, 3 3, 10 0, 2 2),
47
+ POINT(10 20),
48
+ POLYGON((0 0, 0 5, 5 5, 5 0, 0 0)),
49
+ POINT(10 20)
50
+ )')
51
+
52
+ assert_equal(2, geom.select { |point| point == read('POINT(10 20)') }.length)
53
+ end
54
+ end
55
+
56
+ def test_default_srid
57
+ geom = read('GEOMETRYCOLLECTION (POINT(0 0))')
58
+ assert_equal(0, geom.srid)
59
+ end
60
+
61
+ def test_setting_srid_manually
62
+ geom = read('GEOMETRYCOLLECTION (POINT(0 0))')
63
+ geom.srid = 4326
64
+ assert_equal(4326, geom.srid)
65
+ end
66
+
67
+ def test_dimensions
68
+ geom = read('GEOMETRYCOLLECTION (POINT(0 0))')
69
+ assert_equal(0, geom.dimensions)
70
+
71
+ geom = read('GEOMETRYCOLLECTION (LINESTRING(1 2, 3 4))')
72
+ assert_equal(1, geom.dimensions)
73
+ end
74
+
75
+ def test_num_geometries
76
+ geom = read('GEOMETRYCOLLECTION (POINT(1 2), LINESTRING(1 2, 3 4))')
77
+ assert_equal(2, geom.num_geometries)
78
+ end
79
+ end
@@ -2,7 +2,7 @@
2
2
  $: << File.dirname(__FILE__)
3
3
  require 'test_helper'
4
4
 
5
- class GeometryTests < Test::Unit::TestCase
5
+ class GeometryTests < MiniTest::Unit::TestCase
6
6
  include TestHelper
7
7
 
8
8
  def comparison_tester(method_with_args, geom_a, geom_b, expected)
@@ -13,14 +13,14 @@ class GeometryTests < Test::Unit::TestCase
13
13
  geom_1 = read(geom_a)
14
14
  geom_b = read(geom_b)
15
15
  result = geom_1.send(method, geom_b, *args)
16
- assert(read(expected).eql_exact?(result, TOLERANCE))
16
+ assert_geom_eql_exact(read(expected), result)
17
17
  end
18
18
 
19
19
  def self_tester(method_with_args, g, expected)
20
20
  method_with_args = Array(method_with_args)
21
21
  geom = read(g)
22
22
  result = geom.send(*method_with_args)
23
- assert(read(expected).eql_exact?(result, TOLERANCE))
23
+ assert_geom_eql_exact(read(expected), result)
24
24
  end
25
25
 
26
26
  def test_intersection
@@ -38,7 +38,7 @@ class GeometryTests < Test::Unit::TestCase
38
38
  assert_equal(expected, write(read(geom).buffer(width, params)))
39
39
  }
40
40
 
41
- @writer.rounding_precision = 0
41
+ writer.rounding_precision = 0
42
42
 
43
43
  tester['POLYGON EMPTY', 'POINT(0 0)', 0]
44
44
 
@@ -123,72 +123,15 @@ class GeometryTests < Test::Unit::TestCase
123
123
  ]
124
124
  end
125
125
 
126
- if ENV['FORCE_TESTS'] || Geos::LineString.method_defined?(:offset_curve)
127
- def test_offset_curve
128
- tester = lambda { |expected, g, width, style|
129
- geom = read(g)
130
- buffered = geom.offset_curve(width, style)
131
-
132
- assert_equal(expected, write(buffered))
133
- }
134
-
135
- @writer.rounding_precision = 0
136
-
137
- # straight left
138
- tester[
139
- 'LINESTRING (0 2, 10 2)',
140
- 'LINESTRING (0 0, 10 0)',
141
- 2, {
142
- :quad_segs => 0,
143
- :join => :round,
144
- :mitre_limit => 2
145
- }
146
- ]
147
-
148
- # straight right
149
- tester[
150
- 'LINESTRING (10 -2, 0 -2)',
151
- 'LINESTRING (0 0, 10 0)',
152
- -2, {
153
- :quad_segs => 0,
154
- :join => :round,
155
- :mitre_limit => 2
156
- }
157
- ]
158
-
159
- # outside curve
160
- tester[
161
- 'LINESTRING (12 10, 12 0, 10 -2, 0 -2)',
162
- 'LINESTRING (0 0, 10 0, 10 10)',
163
- -2, {
164
- :quad_segs => 1,
165
- :join => :round,
166
- :mitre_limit => 2
167
- }
168
- ]
169
-
170
- # inside curve
171
- tester[
172
- 'LINESTRING (0 2, 8 2, 8 10)',
173
- 'LINESTRING (0 0, 10 0, 10 10)',
174
- 2, {
175
- :quad_segs => 1,
176
- :join => :round,
177
- :mitre_limit => 2
178
- }
179
- ]
180
- end
181
- end
182
-
183
126
  def test_convex_hull
184
127
  geom = read('POINT(0 0)')
185
- assert(read('POINT(0 0)').eql_exact?(geom.convex_hull, TOLERANCE))
128
+ assert_geom_eql_exact(read('POINT(0 0)'), geom.convex_hull)
186
129
 
187
130
  geom = read('LINESTRING(0 0, 10 10)')
188
- assert(read('LINESTRING(0 0, 10 10)').eql_exact?(geom.convex_hull, TOLERANCE))
131
+ assert_geom_eql_exact(read('LINESTRING(0 0, 10 10)'), geom.convex_hull)
189
132
 
190
133
  geom = read('POLYGON((0 0, 0 10, 5 5, 10 10, 10 0, 0 0))')
191
- assert(read('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))').eql_exact?(geom.convex_hull, TOLERANCE))
134
+ assert_geom_eql_exact(read('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))'), geom.convex_hull)
192
135
  end
193
136
 
194
137
  def test_difference
@@ -335,6 +278,78 @@ class GeometryTests < Test::Unit::TestCase
335
278
  )
336
279
  end
337
280
 
281
+ def test_symmetric_difference
282
+ comparison_tester(
283
+ :symmetric_difference,
284
+ 'POINT(0 0)',
285
+ 'POINT(0 0)',
286
+ 'GEOMETRYCOLLECTION EMPTY'
287
+ )
288
+
289
+ comparison_tester(
290
+ :symmetric_difference,
291
+ 'POINT(0 0)',
292
+ 'POINT(1 0)',
293
+ 'MULTIPOINT (0 0, 1 0)'
294
+ )
295
+
296
+ comparison_tester(
297
+ :symmetric_difference,
298
+ 'LINESTRING(0 0, 10 0)',
299
+ 'POINT(5 0)',
300
+ 'LINESTRING (0 0, 10 0)'
301
+ )
302
+
303
+ comparison_tester(
304
+ :symmetric_difference,
305
+ 'POINT(5 0)',
306
+ 'LINESTRING(0 0, 10 0)',
307
+ 'LINESTRING (0 0, 10 0)'
308
+ )
309
+
310
+ comparison_tester(
311
+ :symmetric_difference,
312
+ 'POINT(5 0)',
313
+ 'LINESTRING(0 1, 10 1)',
314
+ 'GEOMETRYCOLLECTION (POINT (5 0), LINESTRING (0 1, 10 1))'
315
+ )
316
+
317
+ comparison_tester(
318
+ :symmetric_difference,
319
+ 'LINESTRING(0 0, 10 0)',
320
+ 'LINESTRING(5 -10, 5 10)',
321
+ 'MULTILINESTRING ((0 0, 5 0), (5 0, 10 0), (5 -10, 5 0), (5 0, 5 10))'
322
+ )
323
+
324
+ comparison_tester(
325
+ :symmetric_difference,
326
+ 'LINESTRING(0 0, 10 0)',
327
+ 'LINESTRING(5 0, 20 0)',
328
+ 'MULTILINESTRING ((0 0, 5 0), (10 0, 20 0))'
329
+ )
330
+
331
+ comparison_tester(
332
+ :symmetric_difference,
333
+ 'POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))',
334
+ 'LINESTRING(5 -10, 5 10)',
335
+ 'GEOMETRYCOLLECTION (LINESTRING (5 -10, 5 0), POLYGON ((5 0, 0 0, 0 10, 5 10, 10 10, 10 0, 5 0)))'
336
+ )
337
+
338
+ comparison_tester(
339
+ :symmetric_difference,
340
+ 'POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))',
341
+ 'LINESTRING(10 0, 20 0)',
342
+ 'GEOMETRYCOLLECTION (LINESTRING (10 0, 20 0), POLYGON ((10 0, 0 0, 0 10, 10 10, 10 0)))'
343
+ )
344
+
345
+ comparison_tester(
346
+ :symmetric_difference,
347
+ 'POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))',
348
+ 'POLYGON((5 -5, 5 5, 15 5, 15 -5, 5 -5))',
349
+ 'MULTIPOLYGON (((5 0, 0 0, 0 10, 10 10, 10 5, 5 5, 5 0)), ((5 0, 10 0, 10 5, 15 5, 15 -5, 5 -5, 5 0)))'
350
+ )
351
+ end
352
+
338
353
  def test_boundary
339
354
  self_tester(
340
355
  :boundary,
@@ -502,6 +517,26 @@ class GeometryTests < Test::Unit::TestCase
502
517
  )
503
518
  end
504
519
 
520
+ def test_representative_point
521
+ self_tester(
522
+ :representative_point,
523
+ 'POINT(0 0)',
524
+ 'POINT(0 0)'
525
+ )
526
+
527
+ self_tester(
528
+ :representative_point,
529
+ 'LINESTRING(0 0, 5 5, 10 10)',
530
+ 'POINT (5 5)'
531
+ )
532
+
533
+ self_tester(
534
+ :representative_point,
535
+ 'POLYGON((0 0, 0 10, 5 5, 10 10, 10 0, 0 0))',
536
+ 'POINT (2.5 5)'
537
+ )
538
+ end
539
+
505
540
  def test_centroid
506
541
  self_tester(
507
542
  :centroid,
@@ -589,7 +624,7 @@ class GeometryTests < Test::Unit::TestCase
589
624
  ret = geom_a.relate_boundary_node_rule(geom_b, :endpoint)
590
625
  assert_equal('FF10FFFF2', ret)
591
626
 
592
- assert_raise(TypeError) do
627
+ assert_raises(TypeError) do
593
628
  geom_a.relate_boundary_node_rule(geom_b, :gibberish)
594
629
  end
595
630
  end
@@ -775,20 +810,20 @@ class GeometryTests < Test::Unit::TestCase
775
810
  end
776
811
 
777
812
  def test_empty
778
- assert(!read('POINT(0 0)').empty?)
779
- assert(read('POINT EMPTY').empty?)
780
- assert(!read('LINESTRING(0 0, 10 0)').empty?)
781
- assert(read('LINESTRING EMPTY').empty?)
782
- assert(!read('POLYGON((0 0, 10 0, 10 10, 0 0))').empty?)
783
- assert(read('POLYGON EMPTY').empty?)
784
- assert(!read('GEOMETRYCOLLECTION(POINT(0 0))').empty?)
785
- assert(read('GEOMETRYCOLLECTION EMPTY').empty?)
813
+ refute_geom_empty(read('POINT(0 0)'))
814
+ assert_geom_empty(read('POINT EMPTY'))
815
+ refute_geom_empty(read('LINESTRING(0 0, 10 0)'))
816
+ assert_geom_empty(read('LINESTRING EMPTY'))
817
+ refute_geom_empty(read('POLYGON((0 0, 10 0, 10 10, 0 0))'))
818
+ assert_geom_empty(read('POLYGON EMPTY'))
819
+ refute_geom_empty(read('GEOMETRYCOLLECTION(POINT(0 0))'))
820
+ assert_geom_empty(read('GEOMETRYCOLLECTION EMPTY'))
786
821
  end
787
822
 
788
823
  def test_valid
789
- assert(read('POINT(0 0)').valid?)
790
- assert(!read('POINT(0 NaN)').valid?)
791
- assert(!read('POINT(0 nan)').valid?)
824
+ assert_geom_valid(read('POINT(0 0)'))
825
+ refute_geom_valid(read('POINT(0 NaN)'))
826
+ refute_geom_valid(read('POINT(0 nan)'))
792
827
  end
793
828
 
794
829
  if ENV['FORCE_TESTS'] || Geos::Geometry.method_defined?(:valid_reason)
@@ -823,28 +858,20 @@ class GeometryTests < Test::Unit::TestCase
823
858
  end
824
859
 
825
860
  def test_simple
826
- assert(read('POINT(0 0)').simple?)
827
- assert(read('LINESTRING(0 0, 10 0)').simple?)
828
- assert(!read('LINESTRING(0 0, 10 0, 5 5, 5 -5)').simple?)
861
+ assert_geom_simple(read('POINT(0 0)'))
862
+ assert_geom_simple(read('LINESTRING(0 0, 10 0)'))
863
+ refute_geom_simple(read('LINESTRING(0 0, 10 0, 5 5, 5 -5)'))
829
864
  end
830
865
 
831
866
  def test_ring
832
- assert(!read('POINT(0 0)').ring?)
833
- assert(!read('LINESTRING(0 0, 10 0, 5 5, 5 -5)').ring?)
834
- assert(read('LINESTRING(0 0, 10 0, 5 5, 0 0)').ring?)
867
+ refute_geom_ring(read('POINT(0 0)'))
868
+ refute_geom_ring(read('LINESTRING(0 0, 10 0, 5 5, 5 -5)'))
869
+ assert_geom_ring(read('LINESTRING(0 0, 10 0, 5 5, 0 0)'))
835
870
  end
836
871
 
837
872
  def test_has_z
838
- assert(!read('POINT(0 0)').has_z?)
839
- assert(read('POINT(0 0 0)').has_z?)
840
- end
841
-
842
- if ENV['FORCE_TESTS'] || Geos::LineString.method_defined?(:closed?)
843
- def test_closed
844
- assert(read('LINESTRING(0 0, 1 1, 2 2, 0 0)').closed?)
845
- assert(!read('LINESTRING(0 0, 1 1, 2 2)').closed?)
846
- assert(read('LINEARRING(0 0, 1 1, 2 2, 0 0)').closed?)
847
- end
873
+ refute_geom_has_z(read('POINT(0 0)'))
874
+ assert_geom_has_z(read('POINT(0 0 0)'))
848
875
  end
849
876
 
850
877
  def test_num_geometries
@@ -887,7 +914,7 @@ class GeometryTests < Test::Unit::TestCase
887
914
  if expected.nil?
888
915
  assert_nil(result)
889
916
  else
890
- assert(result.eql_exact?(read(expected), TOLERANCE))
917
+ assert_geom_eql_exact(result, read(expected))
891
918
  end
892
919
  }
893
920
 
@@ -914,7 +941,7 @@ class GeometryTests < Test::Unit::TestCase
914
941
  (13 11, 13 12, 13.5 12, 13.5 11, 13 11))'
915
942
  ]
916
943
 
917
- assert_raise(NoMethodError) do
944
+ assert_raises(NoMethodError) do
918
945
  tester[0, 'POINT (0 0)']
919
946
  end
920
947
  end
@@ -927,7 +954,7 @@ class GeometryTests < Test::Unit::TestCase
927
954
  if expected.nil?
928
955
  assert_nil(result)
929
956
  else
930
- assert(result.eql_exact?(read(expected), TOLERANCE))
957
+ assert_geom_eql_exact(result, read(expected))
931
958
  end
932
959
  }
933
960
 
@@ -960,7 +987,7 @@ class GeometryTests < Test::Unit::TestCase
960
987
  1
961
988
  ]
962
989
 
963
- assert_raise(RuntimeError) do
990
+ assert_raises(RuntimeError) do
964
991
  tester[
965
992
  nil,
966
993
  'POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))',
@@ -968,7 +995,7 @@ class GeometryTests < Test::Unit::TestCase
968
995
  ]
969
996
  end
970
997
 
971
- assert_raise(NoMethodError) do
998
+ assert_raises(NoMethodError) do
972
999
  tester[
973
1000
  nil,
974
1001
  'POINT (0 0)',
@@ -985,7 +1012,7 @@ class GeometryTests < Test::Unit::TestCase
985
1012
  if expected.nil?
986
1013
  assert_nil(result)
987
1014
  else
988
- assert(result.eql_exact?(read(expected), TOLERANCE))
1015
+ assert_geom_eql_exact(result, read(expected))
989
1016
  end
990
1017
  }
991
1018
 
@@ -997,7 +1024,7 @@ class GeometryTests < Test::Unit::TestCase
997
1024
  )'
998
1025
  ]
999
1026
 
1000
- assert_raise(NoMethodError) do
1027
+ assert_raises(NoMethodError) do
1001
1028
  tester[
1002
1029
  nil,
1003
1030
  'POINT (0 0)'
@@ -1089,31 +1116,6 @@ class GeometryTests < Test::Unit::TestCase
1089
1116
  tester[[[0, 0], [0, 5], [5, 5], [5, 0], [0, 0]], 'LINEARRING(0 0, 0 5, 5 5, 5 0, 0 0)']
1090
1117
  end
1091
1118
 
1092
- if ENV['FORCE_TESTS'] || Geos::LineString.method_defined?(:num_points)
1093
- def test_num_points
1094
- assert_equal(4, read('LINESTRING (0 0, 1 0, 1 1, 0 1)').num_points)
1095
-
1096
- assert_raise(NoMethodError) do
1097
- read('POINT (0 0)').num_points
1098
- end
1099
- end
1100
- end
1101
-
1102
- if ENV['FORCE_TESTS'] || Geos::Point.method_defined?(:get_x)
1103
- def test_get_x_and_get_y
1104
- geom = read('POINT (1 2)')
1105
- assert_equal(1, geom.get_x)
1106
- assert_equal(2, geom.get_y)
1107
-
1108
- assert_equal(1, geom.x)
1109
- assert_equal(2, geom.y)
1110
-
1111
- assert_raise(NoMethodError) do
1112
- read('LINESTRING (0 0, 1 1)').get_x
1113
- end
1114
- end
1115
- end
1116
-
1117
1119
  def test_dimensions
1118
1120
  tester = lambda { |expected, g|
1119
1121
  geom = read(g)
@@ -1161,7 +1163,7 @@ class GeometryTests < Test::Unit::TestCase
1161
1163
  geom_b = read('POINT(3 4)')
1162
1164
 
1163
1165
  # The method only accept lineal geometries
1164
- assert_raise(RuntimeError) do
1166
+ assert_raises(RuntimeError) do
1165
1167
  geom_a.project(geom_b)
1166
1168
  end
1167
1169
 
@@ -1207,42 +1209,12 @@ class GeometryTests < Test::Unit::TestCase
1207
1209
  tester['POINT (10 0)', 'LINESTRING(0 0, 10 0)', 20, false]
1208
1210
  tester['POINT (10 0)', 'LINESTRING(0 0, 10 0)', 2, true]
1209
1211
 
1210
- assert_raise(RuntimeError) do
1212
+ assert_raises(RuntimeError) do
1211
1213
  read('POINT(1 2)').interpolate(0)
1212
1214
  end
1213
1215
  end
1214
1216
  end
1215
1217
 
1216
- if ENV['FORCE_TESTS'] || Geos::LineString.method_defined?(:point_n)
1217
- def test_point_n
1218
- writer.rounding_precision = 0
1219
-
1220
- tester = lambda { |expected, geom, n|
1221
- assert_equal(expected, write(geom.point_n(n)))
1222
- }
1223
-
1224
- geom = read('LINESTRING (10 10, 10 14, 14 14, 14 10)')
1225
- tester['POINT (10 10)', geom, 0]
1226
- tester['POINT (10 14)', geom, 1]
1227
- tester['POINT (14 14)', geom, 2]
1228
- tester['POINT (14 10)', geom, 3]
1229
-
1230
- assert_raise(RuntimeError) do
1231
- tester['POINT (0 0)', geom, 4]
1232
- end
1233
-
1234
- geom = read('LINEARRING (11 11, 11 12, 12 11, 11 11)')
1235
- tester['POINT (11 11)', geom, 0]
1236
- tester['POINT (11 12)', geom, 1]
1237
- tester['POINT (12 11)', geom, 2]
1238
- tester['POINT (11 11)', geom, 3]
1239
-
1240
- assert_raise(NoMethodError) do
1241
- tester[nil, read('POINT (0 0)'), 0]
1242
- end
1243
- end
1244
- end
1245
-
1246
1218
  if ENV['FORCE_TESTS'] || Geos::Geometry.method_defined?(:start_point)
1247
1219
  def test_start_and_end_points
1248
1220
  writer.rounding_precision = 0
@@ -1329,7 +1301,7 @@ class GeometryTests < Test::Unit::TestCase
1329
1301
  tester = lambda { |expected, g1, g2, tolerance|
1330
1302
  geom_a = read(g1)
1331
1303
  geom_b = read(g2)
1332
- assert(read(expected).eql_exact?(geom_a.snap(geom_b, tolerance), TOLERANCE))
1304
+ assert_geom_eql_exact(read(expected), geom_a.snap(geom_b, tolerance))
1333
1305
  }
1334
1306
 
1335
1307
  writer.trim = true
@@ -1404,10 +1376,10 @@ class GeometryTests < Test::Unit::TestCase
1404
1376
 
1405
1377
  polygonized = geom_a.polygonize_full
1406
1378
 
1407
- assert(polygonized[:rings].is_a?(Array))
1408
- assert(polygonized[:cuts].is_a?(Array))
1409
- assert(polygonized[:dangles].is_a?(Array))
1410
- assert(polygonized[:invalid_rings].is_a?(Array))
1379
+ assert_kind_of(Array, polygonized[:rings])
1380
+ assert_kind_of(Array, polygonized[:cuts])
1381
+ assert_kind_of(Array, polygonized[:dangles])
1382
+ assert_kind_of(Array, polygonized[:invalid_rings])
1411
1383
 
1412
1384
  assert_equal(2, polygonized[:rings].length)
1413
1385
  assert_equal(0, polygonized[:cuts].length)
@@ -1452,7 +1424,7 @@ class GeometryTests < Test::Unit::TestCase
1452
1424
  end
1453
1425
 
1454
1426
  def test_polygonize_with_bad_arguments
1455
- assert_raise(ArgumentError) do
1427
+ assert_raises(ArgumentError) do
1456
1428
  geom = read('POINT(0 0)')
1457
1429
 
1458
1430
  geom.polygonize(geom, 'gibberish')
@@ -1480,83 +1452,11 @@ class GeometryTests < Test::Unit::TestCase
1480
1452
  end
1481
1453
  end
1482
1454
 
1483
- if ENV['FORCE_TESTS'] || Geos::LineString.method_defined?(:select)
1484
- def test_line_string_enumerable
1485
- @writer.trim = true
1486
- geom = read('LINESTRING(0 0, 1 1, 2 2, 3 3, 10 0, 2 2)')
1487
-
1488
- assert_equal(2, geom.select { |point| point == read('POINT(2 2)') }.length)
1489
- end
1490
- end
1491
-
1492
- if ENV['FORCE_TESTS'] || Geos::GeometryCollection.method_defined?(:detect)
1493
- def test_geometry_collection_enumerable
1494
- @writer.trim = true
1495
- geom = read('GEOMETRYCOLLECTION(
1496
- LINESTRING(0 0, 1 1, 2 2, 3 3, 10 0, 2 2),
1497
- POINT(10 20),
1498
- POLYGON((0 0, 0 5, 5 5, 5 0, 0 0)),
1499
- POINT(10 20)
1500
- )')
1501
-
1502
- assert_equal(2, geom.select { |point| point == read('POINT(10 20)') }.length)
1503
- end
1504
- end
1505
-
1506
- if ENV['FORCE_TESTS'] || Geos::LineString.method_defined?(:[])
1507
- def test_line_string_array
1508
- @writer.trim = true
1509
- geom = read('LINESTRING(0 0, 1 1, 2 2, 3 3, 4 4)')
1510
-
1511
- assert_equal('POINT (0 0)', write(geom[0]))
1512
- assert_equal('POINT (4 4)', write(geom[-1]))
1513
-
1514
- assert_equal([
1515
- 'POINT (0 0)',
1516
- 'POINT (1 1)'
1517
- ], geom[0, 2].collect { |g| write(g) })
1518
-
1519
- assert_equal(nil, geom[0, -1])
1520
- assert_equal([], geom[-1, 0])
1521
- assert_equal([
1522
- 'POINT (1 1)',
1523
- 'POINT (2 2)'
1524
- ], geom[1..2].collect { |g| write(g) })
1525
- end
1526
- end
1527
-
1528
- if ENV['FORCE_TESTS'] || Geos::GeometryCollection.method_defined?(:[])
1529
- def test_geometry_collection_array
1530
- @writer.trim = true
1531
- geom = read('GEOMETRYCOLLECTION(
1532
- LINESTRING(0 0, 1 1, 2 2, 3 3),
1533
- POINT(10 20),
1534
- POLYGON((0 0, 0 5, 5 5, 5 0, 0 0)),
1535
- POINT(10 20)
1536
- )')
1537
-
1538
- assert_equal('LINESTRING (0 0, 1 1, 2 2, 3 3)', write(geom[0]))
1539
- assert_equal('POINT (10 20)', write(geom[-1]))
1540
-
1541
- assert_equal([
1542
- 'LINESTRING (0 0, 1 1, 2 2, 3 3)',
1543
- 'POINT (10 20)'
1544
- ], geom[0, 2].collect { |g| write(g) })
1545
-
1546
- assert_equal(nil, geom[0, -1])
1547
- assert_equal([], geom[-1, 0])
1548
- assert_equal([
1549
- 'POINT (10 20)',
1550
- 'POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))'
1551
- ], geom[1..2].collect { |g| write(g) })
1552
- end
1553
- end
1554
-
1555
1455
  def test_clone
1556
1456
  geom_a = read('POINT(0 0)')
1557
1457
  geom_b = geom_a.clone
1558
1458
 
1559
- assert(geom_a.eql?(geom_b))
1459
+ assert_equal(geom_a, geom_b)
1560
1460
  end
1561
1461
 
1562
1462
  def test_clone_srid
@@ -1565,7 +1465,7 @@ class GeometryTests < Test::Unit::TestCase
1565
1465
  geom_a.srid = srid
1566
1466
  geom_b = geom_a.clone
1567
1467
 
1568
- assert(geom_a.eql?(geom_b))
1468
+ assert_equal(geom_a, geom_b)
1569
1469
  assert_equal(srid, geom_b.srid)
1570
1470
  end
1571
1471
 
@@ -1573,7 +1473,7 @@ class GeometryTests < Test::Unit::TestCase
1573
1473
  geom_a = read('POINT(0 0)')
1574
1474
  geom_b = geom_a.dup
1575
1475
 
1576
- assert(geom_a.eql?(geom_b))
1476
+ assert_equal(geom_a, geom_b)
1577
1477
  end
1578
1478
 
1579
1479
  def test_dup_srid
@@ -1581,17 +1481,10 @@ class GeometryTests < Test::Unit::TestCase
1581
1481
  geom_a = read('POINT(0 0)')
1582
1482
  geom_a.srid = srid
1583
1483
  geom_b = geom_a.dup
1584
- assert(geom_a.eql?(geom_b))
1484
+ assert_equal(geom_a, geom_b)
1585
1485
  assert_equal(srid, geom_b.srid)
1586
1486
  end
1587
1487
 
1588
- def test_geometry_collection_enumerator
1589
- geom = read('GEOMETRYCOLLECTION(POINT(0 0))')
1590
- assert_kind_of(Enumerable, geom.each)
1591
- assert_kind_of(Enumerable, geom.to_enum)
1592
- assert_equal(geom, geom.each {})
1593
- end
1594
-
1595
1488
  def test_line_string_enumerator
1596
1489
  geom = read('LINESTRING(0 0, 10 10))')
1597
1490
  assert_kind_of(Enumerable, geom.each)
@@ -1600,20 +1493,113 @@ class GeometryTests < Test::Unit::TestCase
1600
1493
  end
1601
1494
 
1602
1495
  def test_normalize
1496
+ writer.trim = true
1497
+
1603
1498
  geom = read('POLYGON((0 0, 5 0, 5 5, 0 5, 0 0))')
1604
1499
  geom.normalize
1605
- assert_equal('POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))', write(geom, :trim => true))
1500
+ assert_equal('POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))', write(geom))
1606
1501
 
1607
1502
  geom = read('POLYGON((0 0, 5 0, 5 5, 0 5, 0 0))').normalize
1608
- assert_equal('POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))', write(geom, :trim => true))
1503
+ assert_equal('POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))', write(geom))
1609
1504
  end
1610
1505
 
1611
1506
  def test_normalize_bang
1507
+ writer.trim = true
1508
+
1612
1509
  geom = read('POLYGON((0 0, 5 0, 5 5, 0 5, 0 0))')
1613
1510
  geom.normalize!
1614
- assert_equal('POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))', write(geom, :trim => true))
1511
+ assert_equal('POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))', write(geom))
1615
1512
 
1616
1513
  geom = read('POLYGON((0 0, 5 0, 5 5, 0 5, 0 0))').normalize!
1617
- assert_equal('POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))', write(geom, :trim => true))
1514
+ assert_equal('POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))', write(geom))
1515
+ end
1516
+
1517
+ def test_eql
1518
+ geom_a = read('POINT(1.0 1.0)')
1519
+ geom_b = read('POINT(2.0 2.0)')
1520
+
1521
+ %w{ eql? equals? == }.each do |method|
1522
+ refute(geom_a.send(method, geom_b), "Expected geoms to not be equal using #{method}")
1523
+ end
1524
+ end
1525
+
1526
+ def test_eql_exact
1527
+ geom_a = read('POINT(1.0 1.0)')
1528
+ geom_b = read('POINT(2.0 2.0)')
1529
+
1530
+ %w{ eql_exact? equals_exact? exactly_equals? }.each do |method|
1531
+ refute(geom_a.send(method, geom_b, 0.001), "Expected geoms to not be equal using #{method}")
1532
+ end
1533
+ end
1534
+
1535
+ def test_eql_almost_default
1536
+ geom = read('POINT (1 1)')
1537
+ geom_a = read('POINT (1.0000001 1.0000001)')
1538
+ geom_b = read('POINT (1.000001 1.000001)')
1539
+
1540
+ %w{ eql_almost? equals_almost? almost_equals? }.each do |method|
1541
+ assert(geom.send(method, geom_a), "Expected geoms to be equal using #{method}")
1542
+ refute(geom.send(method, geom_b), "Expected geoms to not be equal using #{method}")
1543
+ end
1544
+ end
1545
+
1546
+ def test_eql_almost
1547
+ geom_a = read('POINT(1.0 1.0)')
1548
+ geom_b = read('POINT(1.1 1.1)')
1549
+
1550
+ refute_equal(geom_a, geom_b)
1551
+
1552
+ %w{ eql_almost? equals_almost? almost_equals? }.each do |method|
1553
+ assert(geom_a.send(method, geom_b, 0), "Expected geoms to be equal using #{method}")
1554
+ refute(geom_a.send(method, geom_b, 1), "Expected geoms to not be equal using #{method}")
1555
+ end
1556
+ end
1557
+
1558
+ def test_srid_copy_policy
1559
+ geom = read('POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))')
1560
+ geom.srid = 4326
1561
+
1562
+ Geos.srid_copy_policy = :zero
1563
+ cloned = geom.clone
1564
+ assert_equal(4326, cloned.srid)
1565
+
1566
+ Geos.srid_copy_policy = :lenient
1567
+ cloned = geom.clone
1568
+ assert_equal(4326, cloned.srid)
1569
+
1570
+ Geos.srid_copy_policy = :strict
1571
+ cloned = geom.clone
1572
+ assert_equal(4326, cloned.srid)
1573
+
1574
+ Geos.srid_copy_policy = :zero
1575
+ geom_b = geom.convex_hull
1576
+ assert_equal(0, geom_b.srid)
1577
+
1578
+ Geos.srid_copy_policy = :lenient
1579
+ geom_b = geom.convex_hull
1580
+ assert_equal(4326, geom_b.srid)
1581
+
1582
+ Geos.srid_copy_policy = :strict
1583
+ geom_b = geom.convex_hull
1584
+ assert_equal(4326, geom_b.srid)
1585
+
1586
+ geom_b = read('POLYGON ((3 3, 3 8, 8 8, 8 3, 3 3))')
1587
+ geom_b.srid = 3875
1588
+
1589
+ Geos.srid_copy_policy = :zero
1590
+ geom_c = geom.intersection(geom_b)
1591
+ assert_equal(0, geom_c.srid)
1592
+
1593
+ Geos.srid_copy_policy = :lenient
1594
+ geom_c = geom.intersection(geom_b)
1595
+ assert_equal(4326, geom_c.srid)
1596
+
1597
+ assert_raises(Geos::MixedSRIDsError) do
1598
+ Geos.srid_copy_policy = :strict
1599
+ geom_c = geom.intersection(geom_b)
1600
+ assert_equal(231231, geom_c.srid)
1601
+ end
1602
+ ensure
1603
+ Geos.srid_copy_policy = :default
1618
1604
  end
1619
1605
  end