Skip to content

Commit

Permalink
Add Geometry#line_substring.
Browse files Browse the repository at this point in the history
  • Loading branch information
dark-panda committed Sep 22, 2023
1 parent f62c9ae commit 1fa52d9
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/ffi-geos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,11 @@ def self.geos_library_paths
:pointer, :pointer, :pointer
],

GEOSLineSubstring_r: [
# *geom, *handle, *geom, start_fraction, end_fraction
:pointer, :pointer, :pointer, :double, :double
],

GEOSGeom_getXMin_r: [
# 0 on exception, *handle, (double *) value
:int, :pointer, :pointer, :pointer
Expand Down
7 changes: 7 additions & 0 deletions lib/ffi-geos/geometry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ def line_merge
cast_geometry_ptr(FFIGeos.GEOSLineMerge_r(Geos.current_handle_pointer, ptr), srid_copy: srid)
end

if FFIGeos.respond_to?(:GEOSLineSubstring_r)
# Added in GEOS 3.12+
def line_substring(start_fraction, end_fraction)
cast_geometry_ptr(FFIGeos.GEOSLineSubstring_r(Geos.current_handle_pointer, ptr, start_fraction, end_fraction), srid_copy: srid)
end
end

def simplify(tolerance)
cast_geometry_ptr(FFIGeos.GEOSSimplify_r(Geos.current_handle_pointer, ptr, tolerance), srid_copy: srid)
end
Expand Down
66 changes: 65 additions & 1 deletion test/geometry_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ def test_unary_union_with_precision
end

def test_disjoint_subset_union
skip unless ENV['FORCE_TESTS'] || Geos::FFIGeos.respond_to?(:GEOSDisjointSubsetUnion_r)
skip unless ENV['FORCE_TESTS'] || Geos::Geometry.method_defined?(:disjoint_subset_union)

simple_tester(
:disjoint_subset_union,
Expand Down Expand Up @@ -789,6 +789,70 @@ def test_line_merge
)
end

def test_line_substring
skip unless ENV['FORCE_TESTS'] || Geos::Geometry.method_defined?(:line_substring)

simple_tester(
:line_substring,
'LINESTRING (0 0, 1 1)',
'LINESTRING (0 0, 2 2)',
0,
0.5
)

simple_tester(
:line_substring,
'MULTILINESTRING ((0 52.5, 0 100), (0 -5, 0 0))',
'MULTILINESTRING((0 0, 0 100),(0 -5, 0 0))',
0.5,
1
)

simple_tester(
:line_substring,
'LINESTRING (1 1, 1 1)',
'LINESTRING (0 0, 2 2)',
0.5,
0.5
)

simple_tester(
:line_substring,
'LINESTRING (1 1, 1 1)',
'LINESTRING (0 0, 2 2)',
0.5,
0.5
)

assert_raises(Geos::GEOSException, 'IllegalArgumentException: end fraction must be <= 1') do
simple_tester(
:line_substring,
'',
'LINESTRING (0 0, 2 2)',
0.5,
1.5
)
end

assert_raises(Geos::GEOSException, 'IllegalArgumentException: end fraction must be <= 1') do
simple_tester(
:line_substring,
'',
'LINESTRING (0 0, 2 2)',
0.5,
-0.1
)
end

simple_tester(
:line_substring,
'LINESTRING (0.5 0.5, 0 0)',
'LINESTRING (0 0, 1 1)',
0.5,
0
)
end

def test_simplify
simple_tester(
:simplify,
Expand Down

0 comments on commit 1fa52d9

Please sign in to comment.