Skip to content

Commit

Permalink
Rename from Box to AsBox and add unit tests
Browse files Browse the repository at this point in the history
The new name has better consistency with other existing conversion
functions (e.g. `AsGeometry` and `AsPolygon`).
  • Loading branch information
peterstace committed Jun 15, 2023
1 parent 6e92ca0 commit c594428
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

- R-tree improvements that reduce the amount of memory they use.

- Exposes a `Box() rtree.Box` method on the `geom.Envelope` type, that converts
the envelope to an `rtree.Box`.
- Exposes an `AsBox() rtree.Box` method on the `geom.Envelope` type, that
converts the envelope to an `rtree.Box`.

## v0.43.0

Expand Down
4 changes: 2 additions & 2 deletions geom/type_envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ func (e Envelope) TransformXY(fn func(XY) XY) (Envelope, error) {
return NewEnvelope([]XY{fn(min), fn(max)})
}

// Box converts this Envelope to an rtree.Box.
func (e Envelope) Box() (rtree.Box, bool) {
// AsBox converts this Envelope to an rtree.Box.
func (e Envelope) AsBox() (rtree.Box, bool) {
return rtree.Box{
MinX: e.minX(),
MinY: e.minY,
Expand Down
13 changes: 13 additions & 0 deletions geom/type_envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

. "github.com/peterstace/simplefeatures/geom"
"github.com/peterstace/simplefeatures/rtree"
)

func onePtEnv(x, y float64) Envelope {
Expand Down Expand Up @@ -609,3 +610,15 @@ func TestBoundingDiagonal(t *testing.T) {
})
}
}

func TestEnvelopeEmptyAsBox(t *testing.T) {
_, ok := Envelope{}.AsBox()
expectFalse(t, ok)
}

func TestEnvelopeNonEmptyAsBox(t *testing.T) {
got, ok := twoPtEnv(1, 2, 3, 4).AsBox()
expectTrue(t, ok)
want := rtree.Box{MinX: 1, MinY: 2, MaxX: 3, MaxY: 4}
expectTrue(t, got == want)
}
2 changes: 1 addition & 1 deletion geom/type_multi_polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func validateMultiPolygon(polys []Polygon, opts ctorOptionSet) error {
boxes := make([]rtree.Box, len(polys))
items := make([]rtree.BulkItem, 0, len(polys))
for i, p := range polys {
if box, ok := p.Envelope().Box(); ok {
if box, ok := p.Envelope().AsBox(); ok {
boxes[i] = box
item := rtree.BulkItem{Box: boxes[i], RecordID: i}
items = append(items, item)
Expand Down
2 changes: 1 addition & 1 deletion geom/type_polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func validatePolygon(rings []LineString, opts ctorOptionSet) error {
boxes := make([]rtree.Box, len(rings))
items := make([]rtree.BulkItem, len(rings))
for i, r := range rings {
box, ok := r.Envelope().Box()
box, ok := r.Envelope().AsBox()
if !ok {
// Cannot occur, because we have already checked to ensure rings
// are closed. Closed rings by definition are non-empty.
Expand Down

0 comments on commit c594428

Please sign in to comment.