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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions geom/alg_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
})
}
}
47 changes: 18 additions & 29 deletions internal/jtsport/jts/operation_buffer_buffer_curve_set_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down