Skip to content

Commit

Permalink
Use a proper initialize_copy method to clone the internal GEOS pointer.
Browse files Browse the repository at this point in the history
This should make clone and dup act sensibly.
  • Loading branch information
dark-panda committed May 18, 2011
1 parent 2955bde commit 13889a5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
11 changes: 7 additions & 4 deletions lib/coordinate_sequence.rb
Expand Up @@ -31,17 +31,20 @@ def initialize(*args)
)
end

def initialize_copy(source)
@ptr = FFI::AutoPointer.new(
FFIGeos.GEOSCoordSeq_clone_r(Geos.current_handle, source.ptr),
self.class.method(:release)
)
end

def self.no_release(ptr) #:nodoc:
end

def self.release(ptr) #:nodoc:
FFIGeos.GEOSCoordSeq_destroy_r(Geos.current_handle, ptr)
end

def clone
self.class.new(FFIGeos.GEOSCoordSeq_clone_r(Geos.current_handle, self.ptr))
end

# Yields coordinates as [ x, y, z ]. The z coordinate may be omitted for
# 2-dimensional CoordinateSequences.
def each
Expand Down
11 changes: 7 additions & 4 deletions lib/geometry.rb
Expand Up @@ -14,17 +14,20 @@ def initialize(ptr, auto_free = true)
)
end

def initialize_copy(source)
@ptr = FFI::AutoPointer.new(
FFIGeos.GEOSGeom_clone_r(Geos.current_handle, source.ptr),
self.class.method(:release)
)
end

def self.no_release(ptr) #:nodoc:
end

def self.release(ptr) #:nodoc:
FFIGeos.GEOSGeom_destroy_r(Geos.current_handle, ptr)
end

def clone
cast_geometry_ptr(FFIGeos.GEOSGeom_clone_r(Geos.current_handle, ptr))
end

# Returns the name of the Geometry type, i.e. "Point", "Polygon", etc.
def geom_type
FFIGeos.GEOSGeomType_r(Geos.current_handle, self.ptr)
Expand Down
22 changes: 22 additions & 0 deletions test/coordinate_sequence_tests.rb
Expand Up @@ -67,4 +67,26 @@ def test_check_bounds
assert_raise(RuntimeError) { @cs.get_ordinate(10, 0) }
assert_raise(RuntimeError) { @cs.get_ordinate(-1, 0) }
end

def test_clone
@cs.set_x(0, 1)
@cs.set_y(0, 2)

cs_b = @cs.clone

assert_equal(@cs.get_x(0), cs_b.get_x(0))
assert_equal(@cs.get_y(0), cs_b.get_y(0))
assert_equal(@cs.dimensions, cs_b.dimensions)
end

def test_dup
@cs.set_x(0, 1)
@cs.set_y(0, 2)

cs_b = @cs.dup

assert_equal(@cs.get_x(0), cs_b.get_x(0))
assert_equal(@cs.get_y(0), cs_b.get_y(0))
assert_equal(@cs.dimensions, cs_b.dimensions)
end
end
14 changes: 14 additions & 0 deletions test/geometry_tests.rb
Expand Up @@ -1516,4 +1516,18 @@ def test_geometry_collection_array
], geom[1..2].collect { |g| write(g) })
end
end

def test_clone
geom_a = read('POINT(0 0)')
geom_b = geom_a.clone

assert(geom_a.eql?(geom_b))
end

def test_dup
geom_a = read('POINT(0 0)')
geom_b = geom_a.dup

assert(geom_a.eql?(geom_b))
end
end

0 comments on commit 13889a5

Please sign in to comment.