Skip to content

Commit

Permalink
Merge pull request #554 from peterstace/fix_bug_in_envelope_transform_xy
Browse files Browse the repository at this point in the history
Fix bug in Envelope's TransformXY method
  • Loading branch information
peterstace committed Sep 29, 2023
2 parents 01c8bbe + 415c11d commit b7ef97b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: cmpgeos
run: make cmpgeos
- name: Convert coverage to lcov
uses: jandelgado/gcov2lcov-action@v1.0.2
uses: jandelgado/gcov2lcov-action@v1.0.9
with:
infile: coverage.out
outfile: coverage.lcov
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v0.45.1

2023-09-29

- Fixes a bug in `Envelope`'s `TransformXY` method, where (depending on the
transform) invariants in the newly created `Envelope` value would be
violated.

## v0.45.0

2023-09-29
Expand Down
7 changes: 6 additions & 1 deletion geom/type_envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,12 @@ func (e Envelope) TransformXY(fn func(XY) XY) Envelope {
if !ok {
return Envelope{}
}
return newUncheckedEnvelope(fn(min), fn(max))
u := fn(min)
v := fn(max)
return newUncheckedEnvelope(
XY{fastMin(u.X, v.X), fastMin(u.Y, v.Y)},
XY{fastMax(u.X, v.X), fastMax(u.Y, v.Y)},
)
}

// AsBox converts this Envelope to an rtree.Box.
Expand Down
10 changes: 10 additions & 0 deletions geom/type_envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,16 @@ func TestEnvelopeTransformXY(t *testing.T) {
}
}

func TestEnvelopeTransformBugFix(t *testing.T) {
// Reproduces a bug where a transform that alters which coordinates are min
// and max causes a malformed envelope.
env := twoPtEnv(1, 2, 3, 4)
got := env.TransformXY(func(in XY) XY {
return XY{-in.X, -in.Y}
})
expectEnvEq(t, got, twoPtEnv(-3, -4, -1, -2))
}

func BenchmarkEnvelopeTransformXY(b *testing.B) {
input := twoPtEnv(1, 2, 3, 4)
b.ResetTimer()
Expand Down
6 changes: 1 addition & 5 deletions geom/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,7 @@ func expectEnvEq(t testing.TB, got, want Envelope) {
ExactEquals(got.Max().AsGeometry(), want.Max().AsGeometry()) {
return
}
t.Errorf(
"\ngot: %v\nwant: %v\n",
got.AsGeometry().AsText(),
want.AsGeometry().AsText(),
)
t.Errorf("\ngot: %v\nwant: %v", got, want)
}

func expectSequenceEq(t testing.TB, got, want Sequence) {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ module github.com/peterstace/simplefeatures

go 1.17

retract v0.45.0 // Due to bug: https://github.com/peterstace/simplefeatures/pull/554

require github.com/lib/pq v1.1.1

0 comments on commit b7ef97b

Please sign in to comment.