Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions algorithm/measure/distance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 17 additions & 15 deletions algorithm/measure/distance_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 2 additions & 4 deletions grid/hexagon_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package grid

import (
"reflect"
"testing"

"github.com/spatial-go/geoos/space"
Expand Down Expand Up @@ -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)
})
}
}
24 changes: 20 additions & 4 deletions grid/square_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package grid

import (
"reflect"
"testing"

"github.com/spatial-go/geoos/space"
Expand Down Expand Up @@ -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)
}
}
}
}
12 changes: 8 additions & 4 deletions planar/megrez_measure_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package planar

import (
"math"
"testing"

"github.com/spatial-go/geoos/geoencoding/wkt"
Expand Down Expand Up @@ -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)
}
})
Expand Down