From b3a81423e84e94c42729469c26dd9aedd8c442ae Mon Sep 17 00:00:00 2001 From: bigmeng Date: Tue, 24 Nov 2026 12:09:29 +0800 Subject: [PATCH 1/3] Document the closed-line buffer fix --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80a78a45..12d53eef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Fix spurious holes in buffers of closed lines when the inward offset is + fully eroded. + - Fix the bounding box of a `GeometryCollection` in the TWKB output. The sub-geometries of a collection are written by their own writers, so their coordinates never reached the bounding box of the collection, and a From 5bfd89fcac27f72b2cc3f125961444c75a269292 Mon Sep 17 00:00:00 2001 From: bigmeng Date: Tue, 24 Nov 2026 12:10:24 +0800 Subject: [PATCH 2/3] Skip fully eroded inward offsets for closed lines --- ...eration_buffer_buffer_curve_set_builder.go | 47 +++++++------------ 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/internal/jtsport/jts/operation_buffer_buffer_curve_set_builder.go b/internal/jtsport/jts/operation_buffer_buffer_curve_set_builder.go index ba68ce30..b65c02f6 100644 --- a/internal/jtsport/jts/operation_buffer_buffer_curve_set_builder.go +++ b/internal/jtsport/jts/operation_buffer_buffer_curve_set_builder.go @@ -166,7 +166,7 @@ func (bcsb *OperationBuffer_BufferCurveSetBuilder) addPolygon(p *Geom_Polygon) { shellCoord := operationBuffer_bufferCurveSetBuilder_clean(shell.GetCoordinates()) // optimization - don't bother computing buffer // if the polygon would be completely eroded - if bcsb.distance < 0.0 && operationBuffer_bufferCurveSetBuilder_isErodedCompletely(shell, bcsb.distance) { + if bcsb.distance < 0.0 && operationBuffer_bufferCurveSetBuilder_isRingFullyEroded(shell.GetCoordinates(), shell.GetEnvelopeInternal(), -bcsb.distance) { return } // don't attempt to buffer a polygon with too few distinct vertices @@ -187,7 +187,7 @@ func (bcsb *OperationBuffer_BufferCurveSetBuilder) addPolygon(p *Geom_Polygon) { // optimization - don't bother computing buffer for this hole // if the hole would be completely covered - if bcsb.distance > 0.0 && operationBuffer_bufferCurveSetBuilder_isErodedCompletely(hole, -bcsb.distance) { + if bcsb.distance > 0.0 && operationBuffer_bufferCurveSetBuilder_isRingFullyEroded(hole.GetCoordinates(), hole.GetEnvelopeInternal(), bcsb.distance) { continue } @@ -204,13 +204,17 @@ func (bcsb *OperationBuffer_BufferCurveSetBuilder) addPolygon(p *Geom_Polygon) { } func (bcsb *OperationBuffer_BufferCurveSetBuilder) addRingBothSides(coord []*Geom_Coordinate, distance float64) { + // addRingSide interprets sides relative to a clockwise ring, so left is outward. bcsb.addRingSide(coord, distance, Geom_Position_Left, Geom_Location_Exterior, Geom_Location_Interior) - // Add the opposite side of the ring - bcsb.addRingSide(coord, distance, - Geom_Position_Right, - Geom_Location_Interior, Geom_Location_Exterior) + + // Skip the inward offset if erosion would eliminate it (JTS #1117). + if !operationBuffer_bufferCurveSetBuilder_isRingFullyEroded(coord, Geom_CoordinateArrays_Envelope(coord), distance) { + bcsb.addRingSide(coord, distance, + Geom_Position_Right, + Geom_Location_Interior, Geom_Location_Exterior) + } } // addRingSide adds an offset curve for one side of a ring. @@ -323,33 +327,18 @@ func operationBuffer_bufferCurveSetBuilder_hasPointOnBuffer(inputRing []*Geom_Co return false } -// operationBuffer_bufferCurveSetBuilder_isErodedCompletely tests whether a ring buffer is eroded completely (is empty) -// based on simple heuristics. -// -// The ringCoord is assumed to contain no repeated points. -// It may be degenerate (i.e. contain only 1, 2, or 3 points). -// In this case it has no area, and hence has a minimum diameter of 0. -func operationBuffer_bufferCurveSetBuilder_isErodedCompletely(ring *Geom_LinearRing, bufferDistance float64) bool { - ringCoord := ring.GetCoordinates() - // degenerate ring has no area +// operationBuffer_bufferCurveSetBuilder_isRingFullyEroded tests whether an +// inward offset by a positive distance would completely erode the ring. +func operationBuffer_bufferCurveSetBuilder_isRingFullyEroded(ringCoord []*Geom_Coordinate, env *Geom_Envelope, distance float64) bool { + // A degenerate ring has no area. if len(ringCoord) < 4 { - return bufferDistance < 0 + return true } - - // important test to eliminate inverted triangle bug - // also optimizes erosion test for triangles if len(ringCoord) == 4 { - return operationBuffer_bufferCurveSetBuilder_isTriangleErodedCompletely(ringCoord, bufferDistance) + return operationBuffer_bufferCurveSetBuilder_isTriangleErodedCompletely(ringCoord, distance) } - - // if envelope is narrower than twice the buffer distance, ring is eroded - env := ring.GetEnvelopeInternal() - envMinDimension := math.Min(env.GetHeight(), env.GetWidth()) - if bufferDistance < 0.0 && 2*math.Abs(bufferDistance) > envMinDimension { - return true - } - - return false + // The ring is eroded if its envelope is narrower than twice the distance. + return 2*distance > math.Min(env.GetHeight(), env.GetWidth()) } // operationBuffer_bufferCurveSetBuilder_isTriangleErodedCompletely tests whether a triangular ring would be eroded completely by the given From f221003e59514341392610baf81bd7ac23b3ef94 Mon Sep 17 00:00:00 2001 From: bigmeng Date: Tue, 24 Nov 2026 12:10:58 +0800 Subject: [PATCH 3/3] Test closed-line buffer coverage and hole erosion --- geom/alg_buffer_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/geom/alg_buffer_test.go b/geom/alg_buffer_test.go index 60f8d208..29af55a2 100644 --- a/geom/alg_buffer_test.go +++ b/geom/alg_buffer_test.go @@ -165,3 +165,35 @@ func TestBuffer(t *testing.T) { }) } } + +func TestBufferClosedLineHoleErosion(t *testing.T) { + // Regression case from JTS #1117 (GEOS #1223). + line := test.FromWKT(t, "LINESTRING (25 44, 31 44, 32 38, 29 37, 25 37, 25 38, 24 40, 24 44, 25 44)") + for _, tc := range []struct { + name string + input geom.Geometry + distance float64 + holes int + }{ + {"hole eroded", line, 100, 0}, + {"hole eroded in reverse", line.Reverse(), 100, 0}, + {"hole preserved", line, 2, 1}, + } { + t.Run(tc.name, func(t *testing.T) { + got, err := geom.Buffer(tc.input, tc.distance) + test.NoErr(t, err) + test.NoErr(t, got.Validate()) + if !got.IsPolygon() { + t.Fatalf("got %s, want Polygon", got.Type()) + } + if holes := got.MustAsPolygon().NumInteriorRings(); holes != tc.holes { + t.Errorf("got %d interior rings, want %d", holes, tc.holes) + } + covers, err := geom.Covers(got, tc.input) + test.NoErr(t, err) + if !covers { + t.Error("buffer does not cover the input line") + } + }) + } +}