diff --git a/geo/geo.go b/geo/geo.go index 514250f..25d00d8 100644 --- a/geo/geo.go +++ b/geo/geo.go @@ -1,6 +1,9 @@ package geo -import "math" +import ( + "fmt" + "math" +) const ( earthRadius = 6371e3 @@ -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 @@ -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, + } +} diff --git a/random/latlon_test.go b/random/latlon_test.go index 07fd29a..b8ab5c2 100644 --- a/random/latlon_test.go +++ b/random/latlon_test.go @@ -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) +}