Skip to content

Commit

Permalink
feat: add center() method for geo.bbox
Browse files Browse the repository at this point in the history
  • Loading branch information
mmadfox committed Sep 11, 2023
1 parent d061a69 commit 9588956
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
17 changes: 16 additions & 1 deletion geo/geo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package geo

import "math"
import (
"fmt"
"math"
)

const (
earthRadius = 6371e3
Expand All @@ -15,6 +18,11 @@ type LatLonPoint struct {
Lon float64
}

func (s LatLonPoint) String() string {
return fmt.Sprintf("Point{Lon: %f, Lat: %f}",
s.Lon, s.Lat)
}

type BBox struct {
MinLon float64
MinLat float64
Expand All @@ -28,3 +36,10 @@ func (b BBox) In(pt LatLonPoint) bool {
b.MaxLon >= pt.Lon &&
b.MaxLat >= pt.Lat
}

func (b BBox) Center() LatLonPoint {
return LatLonPoint{
Lat: (b.MinLat + b.MaxLat) / 2.0,
Lon: (b.MinLon + b.MaxLon) / 2.0,
}
}
15 changes: 15 additions & 0 deletions random/latlon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,18 @@ func TestLatLon(t *testing.T) {
require.True(t, bbox.In(pt))
}
}

func TestBBoxCenter(t *testing.T) {
bbox := geo.BBox{
MinLon: 1.213284,
MinLat: 18.600047,
MaxLon: 11.587763,
MaxLat: 25.518278,
}
expected := geo.LatLonPoint{
Lat: 22.0591625,
Lon: 6.4005235,
}
center := bbox.Center()
require.Equal(t, expected, center)
}

0 comments on commit 9588956

Please sign in to comment.