From 164bb20865414c38fb3bf80f0f2aba77327e5cd5 Mon Sep 17 00:00:00 2001 From: sulaiman <109396075+sulaimwn@users.noreply.github.com> Date: Sat, 28 Nov 2026 10:51:55 -0500 Subject: [PATCH] measure: stabilize spherical distances between points Compute the central angle using the spherical Vincenty formula. This keeps coincident points at zero and resolves small separations near either end of the distance range. --- algorithm/measure/distance.go | 21 ++++++++++++-------- algorithm/measure/distance_test.go | 32 ++++++++++++++++-------------- grid/hexagon_test.go | 6 ++---- grid/square_test.go | 24 ++++++++++++++++++---- planar/megrez_measure_test.go | 12 +++++++---- 5 files changed, 60 insertions(+), 35 deletions(-) diff --git a/algorithm/measure/distance.go b/algorithm/measure/distance.go index b92617b..434cd04 100644 --- a/algorithm/measure/distance.go +++ b/algorithm/measure/distance.go @@ -40,14 +40,19 @@ func MercatorFctor(lat float64) (float64, float64) { func SpheroidDistance(fromSteric, toSteric matrix.Steric) float64 { if to, ok := toSteric.(matrix.Matrix); ok { if from, ok := fromSteric.(matrix.Matrix); ok { - rad := math.Pi / 180.0 - lat0 := from[1] * rad - lng0 := from[0] * rad - lat1 := to[1] * rad - lng1 := to[0] * rad - theta := lng1 - lng0 - dist := math.Acos(math.Sin(lat0)*math.Sin(lat1) + math.Cos(lat0)*math.Cos(lat1)*math.Cos(theta)) - return dist * R + if from[0] == to[0] && from[1] == to[1] { + return 0 + } + const rad = math.Pi / 180 + sinFrom, cosFrom := math.Sincos(from[1] * rad) + sinTo, cosTo := math.Sincos(to[1] * rad) + sinLon, cosLon := math.Sincos((to[0] - from[0]) * rad) + + // Use the spherical Vincenty formula to retain precision near zero and pi. + east := cosTo * sinLon + north := cosFrom*sinTo - sinFrom*cosTo*cosLon + dot := sinFrom*sinTo + cosFrom*cosTo*cosLon + return R * math.Atan2(math.Hypot(east, north), dot) } } trans := coordtransform.NewTransformer(coordtransform.LLTOMERCATOR) diff --git a/algorithm/measure/distance_test.go b/algorithm/measure/distance_test.go index 877ec64..32a9781 100644 --- a/algorithm/measure/distance_test.go +++ b/algorithm/measure/distance_test.go @@ -1,39 +1,41 @@ package measure import ( + "math" "testing" "github.com/spatial-go/geoos/algorithm/matrix" ) func TestSpheroidDistance(t *testing.T) { - fromPoint := matrix.Matrix{12, 15} - toPoint := matrix.Matrix{13, 15} + const metresPerDegree = R * math.Pi / 180 + point := matrix.Matrix{37.622326727937677, 55.75513948864943} line0 := matrix.LineMatrix{{116.40495300292967, 39.926785883895654}, {116.3975715637207, 39.9295502919}} line1 := matrix.LineMatrix{{116.37310981750488, 39.92099342895789}, {116.39928817749023, 39.9174387253541}} - wantResult := 107405.96007592858 - wantResult1 := 1147.420777283722 - type args struct { - from matrix.Steric - to matrix.Steric - } tests := []struct { - name string - args args - want float64 + name string + from, to matrix.Steric + want float64 + tolerance float64 // metres }{ - {name: "testDistance", args: args{from: fromPoint, to: toPoint}, want: wantResult}, - {name: "testDistanceLine", args: args{from: line0, to: line1}, want: wantResult1}, + {"points", matrix.Matrix{12, 15}, matrix.Matrix{13, 15}, 107405.96007597733, 1e-6}, + {"lines", line0, line1, 1147.420777283722, 0}, + {"coincident", point, point, 0, 0}, + {"nearby", matrix.Matrix{0, 0}, matrix.Matrix{1e-8, 0}, 1e-8 * metresPerDegree, 1e-9}, + {"antipodal", point, matrix.Matrix{point[0] - 180, -point[1]}, math.Pi * R, 1e-6}, + {"nearly antipodal", matrix.Matrix{0, 0}, matrix.Matrix{180 - 1e-8, 0}, (180 - 1e-8) * metresPerDegree, 1e-6}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := SpheroidDistance(tt.args.from, tt.args.to); got != tt.want { - t.Errorf("Distance() = %v, want %v", got, tt.want) + got := SpheroidDistance(tt.from, tt.to) + if math.IsNaN(got) || math.Abs(got-tt.want) > tt.tolerance { + t.Errorf("SpheroidDistance() = %v, want %v ± %v metres", got, tt.want, tt.tolerance) } }) } } + func TestMercatorDistance(t *testing.T) { type args struct { dis float64 diff --git a/grid/hexagon_test.go b/grid/hexagon_test.go index 3267328..cdffbf1 100644 --- a/grid/hexagon_test.go +++ b/grid/hexagon_test.go @@ -1,7 +1,6 @@ package grid import ( - "reflect" "testing" "github.com/spatial-go/geoos/space" @@ -81,9 +80,8 @@ func TestHexagonGrid(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotGridGeoms := HexagonGrid(tt.args.bound, tt.args.cellSize); !reflect.DeepEqual(gotGridGeoms, tt.wantGridGeoms) { - t.Errorf("HexagonGrid() = %v, want %v", gotGridGeoms, tt.wantGridGeoms) - } + got := HexagonGrid(tt.args.bound, tt.args.cellSize) + assertGridEqual(t, got, tt.wantGridGeoms) }) } } diff --git a/grid/square_test.go b/grid/square_test.go index b2680fe..63ecd30 100644 --- a/grid/square_test.go +++ b/grid/square_test.go @@ -1,7 +1,6 @@ package grid import ( - "reflect" "testing" "github.com/spatial-go/geoos/space" @@ -76,9 +75,26 @@ func TestSquareGrid(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotGrids := SquareGrid(tt.args.bound, tt.args.cellSize) - if !reflect.DeepEqual(gotGrids, tt.wantGrids) { - t.Errorf("SquareGrid() gotGrids = %v, want %v", gotGrids, tt.wantGrids) - } + assertGridEqual(t, gotGrids, tt.wantGrids) }) } } + +func assertGridEqual(t *testing.T, got, want [][]Grid) { + t.Helper() + const tolerance = 1e-11 // degrees + if len(got) != len(want) { + t.Fatalf("got %d columns, want %d", len(got), len(want)) + } + for col, rows := range want { + if len(got[col]) != len(rows) { + t.Fatalf("column %d has %d rows, want %d", col, len(got[col]), len(rows)) + } + for row, cell := range rows { + geometry := got[col][row].Geometry + if geometry == nil || !cell.Geometry.EqualsExact(geometry, tolerance) { + t.Errorf("grid[%d][%d] = %v, want %v", col, row, geometry, cell.Geometry) + } + } + } +} diff --git a/planar/megrez_measure_test.go b/planar/megrez_measure_test.go index 390e8f2..de25d5c 100644 --- a/planar/megrez_measure_test.go +++ b/planar/megrez_measure_test.go @@ -1,6 +1,7 @@ package planar import ( + "math" "testing" "github.com/spatial-go/geoos/geoencoding/wkt" @@ -110,14 +111,17 @@ func TestAlgorithm_SphericalDistance(t *testing.T) { want float64 wantErr bool }{ - {name: "SphericalDistance", args: args{p1: point01, p2: point02}, want: 678.5053586786567, wantErr: false}, - {name: "SphericalDistance", args: args{p1: point01, p2: point03}, want: 153953.98145619757, wantErr: false}, + {name: "SphericalDistance", args: args{p1: point01, p2: point02}, want: 678.505354164715, wantErr: false}, + {name: "SphericalDistance", args: args{p1: point01, p2: point03}, want: 153953.98145618572, wantErr: false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { G := NormalStrategy() - got, _ := G.SphericalDistance(tt.args.p1, tt.args.p2) - if got != tt.want { + got, err := G.SphericalDistance(tt.args.p1, tt.args.p2) + if (err != nil) != tt.wantErr { + t.Fatalf("SphericalDistance() error = %v, wantErr %v", err, tt.wantErr) + } + if math.IsNaN(got) || math.Abs(got-tt.want) > 1e-6 { t.Errorf("SphericalDistance() got = %v, want %v", got, tt.want) } })