github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/geo/bbox.go (about) 1 // Copyright 2020 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package geo 12 13 import ( 14 "github.com/cockroachdb/cockroach/pkg/geo/geopb" 15 "github.com/cockroachdb/errors" 16 "github.com/twpayne/go-geom" 17 ) 18 19 // BoundingBoxFromGeom returns a bounding box from a given geom.T. 20 func BoundingBoxFromGeom(g geom.T) (*geopb.BoundingBox, error) { 21 bbox := geopb.NewBoundingBox() 22 if g.Empty() { 23 return nil, nil 24 } 25 switch g := g.(type) { 26 case *geom.Point: 27 bbox.Update(g.X(), g.Y()) 28 case *geom.LineString: 29 for _, coord := range g.Coords() { 30 bbox.Update(coord.X(), coord.Y()) 31 } 32 case *geom.Polygon: 33 for _, coord := range g.LinearRing(0).Coords() { 34 bbox.Update(coord.X(), coord.Y()) 35 } 36 case *geom.MultiPoint: 37 for i := 0; i < g.NumPoints(); i++ { 38 if g.Point(i).Empty() { 39 continue 40 } 41 point := g.Point(i) 42 bbox.Update(point.X(), point.Y()) 43 } 44 case *geom.MultiLineString: 45 for i := 0; i < g.NumLineStrings(); i++ { 46 lineString := g.LineString(i) 47 for _, coord := range lineString.Coords() { 48 bbox.Update(coord.X(), coord.Y()) 49 } 50 } 51 case *geom.MultiPolygon: 52 for i := 0; i < g.NumPolygons(); i++ { 53 polygon := g.Polygon(i) 54 for _, coord := range polygon.LinearRing(0).Coords() { 55 bbox.Update(coord.X(), coord.Y()) 56 } 57 } 58 case *geom.GeometryCollection: 59 for i := 0; i < g.NumGeoms(); i++ { 60 shapeBBox, err := BoundingBoxFromGeom(g.Geom(i)) 61 if err != nil { 62 return nil, err 63 } 64 if shapeBBox == nil { 65 continue 66 } 67 bbox.Update(shapeBBox.MinX, shapeBBox.MinY) 68 bbox.Update(shapeBBox.MaxX, shapeBBox.MaxY) 69 } 70 default: 71 return nil, errors.Newf("unknown geom type: %T", g) 72 } 73 return bbox, nil 74 }