aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/paulmach/orb/geojson/bbox.go
blob: cd2c45e8492424517a5c6ff88dbfa04eab652984 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package geojson

import "github.com/paulmach/orb"

// BBox is for the geojson bbox attribute which is an array with all axes
// of the most southwesterly point followed by all axes of the more northeasterly point.
type BBox []float64

// NewBBox creates a bbox from a a bound.
func NewBBox(b orb.Bound) BBox {
	return []float64{
		b.Min[0], b.Min[1],
		b.Max[0], b.Max[1],
	}
}

// Valid checks if the bbox is present and has at least 4 elements.
func (bb BBox) Valid() bool {
	if bb == nil {
		return false
	}

	return len(bb) >= 4 && len(bb)%2 == 0
}

// Bound returns the orb.Bound for the BBox.
func (bb BBox) Bound() orb.Bound {
	if !bb.Valid() {
		return orb.Bound{}
	}

	mid := len(bb) / 2

	return orb.Bound{
		Min: orb.Point{bb[0], bb[1]},
		Max: orb.Point{bb[mid], bb[mid+1]},
	}
}