Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/transformations/simplify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ function _simplify(alg::DouglasPeucker, points::Vector, preserve_endpoint)
i = 2 # already have first and last point added
start_idx, end_idx = 1, npoints
max_idx, max_dist = _find_max_squared_dist(points, start_idx, end_idx)
while i min(MIN_POINTS + 1, max_points) || (i < max_points && max_dist > max_tol)
while i < min(MIN_POINTS + 1, max_points) || (i < max_points && max_dist > max_tol)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fix, but I'm not sure what it means in terms of the algorithm (should MIN_POINTS + 1 become MIN_POINTS?). There's also preserve_endpoint that extends the length with 1 when true, does that matter here?

# Add next point to results
i += 1
results[i] = max_idx
Expand Down
12 changes: 12 additions & 0 deletions test/transformations/simplify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ datadir = realpath(joinpath(dirname(pathof(GO)), "../test/data"))
end

@testset "DouglasPeucker" begin
# Test for issue #386: BoundsError when simplifying small geometries with low number/ratio
@testset "small geometry simplification (issue #386)" begin
# This would cause a BoundsError before the fix due to indexing bug in the while loop
line = GI.LineString([(rand(), rand()) for _ in 1:4])
@test_nowarn GO.simplify(line; ratio=0.1)
@test_nowarn GO.simplify(line; tol=0.1)
@test_nowarn GO.simplify(line; number=3)
# Verify the output is valid
result = GO.simplify(line; number=3)
@test GI.npoint(result) == 3
end

poly_coords = JLD2.jldopen(joinpath(datadir, "complex_polygons.jld2"))["verts"][1:4]
for c in poly_coords
npoints = length(c[1])
Expand Down
Loading