From 9588956fd2c94bf45341265433a6a5c6f18321db Mon Sep 17 00:00:00 2001 From: mmadfox Date: Mon, 11 Sep 2023 05:47:25 +0300 Subject: [PATCH] feat: add center() method for geo.bbox --- geo/geo.go | 17 ++++++++++++++++- random/latlon_test.go | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) 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) +}