Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.
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
50 changes: 50 additions & 0 deletions geos/geom.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,56 @@ func (g *Geometry) RelatePat(other *Geometry, pat string) (bool, error) {
return boolFromC("RelatePat", cGEOSRelatePattern(g.g, other.g, cs))
}

type Bounds struct {
MinX float64
MinY float64
MaxX float64
MaxY float64
}

var NilBounds = Bounds{1e20, 1e20, -1e20, -1e20}

// Bounds returns (minx, miny, maxx, maxy) that bounds the object.
func (g *Geometry) Bounds() (Bounds, error) {
geom, err := g.Envelope()
if err != nil {
return NilBounds, Error()
}

s, err := geom.Shell()
if err != nil {
return NilBounds, Error()
}
c, err := s.Coords()
if err != nil {
return NilBounds, Error()
}

minx := 1.e+20
maxx := -1e+20
miny := 1.e+20
maxy := -1e+20

for _, cd := range c {
if cd.X < minx {
minx = cd.X
}
if cd.X > maxx {
maxx = cd.X
}

if cd.Y < miny {
miny = cd.Y
}
if cd.Y > maxy {
maxy = cd.Y
}
}

return Bounds{minx, miny, maxx, maxy}, nil
}


// various wrappers around C API

type unaryTopo func(*C.GEOSGeometry) *C.GEOSGeometry
Expand Down
18 changes: 18 additions & 0 deletions geos/geom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,3 +1063,21 @@ func TestLineInterpolatePoint(t *testing.T) {
}
}
}

func TestBounds(t *testing.T) {
testPolygon := "POLYGON((17672.0337 9338.1706,17685.6852 9298.5111,17749.098 9319.1397,17742.66 9339.25,17738.74 9342.61,17735.94 9342.4,17732.42 9340.1,17731.2 9340.32,17729.03 9341.35,17720.35 9348.95,17716.3509 9352.6934,17672.0337 9338.1706))"
exampleBounds := Bounds{MinX: 17672.0337, MinY: 9298.5111, MaxX: 17749.098, MaxY: 9352.6934}
p, err := FromWKT(testPolygon)
if err != nil {
t.Fatal(err)
}

bt, err := p.Bounds()
if err != nil {
t.Fatal(err)
}

if bt != exampleBounds {
t.Errorf("Bounds are not equal %v != %v", bt, exampleBounds)
}
}