/
azimuth.go
72 lines (60 loc) · 1.81 KB
/
azimuth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package geogfn
import (
"math"
"github.com/ruiaylin/pgparser/types/geo"
"github.com/cockroachdb/errors"
"github.com/golang/geo/s2"
"github.com/twpayne/go-geom"
)
// Azimuth returns the azimuth in radians of the segment defined by the given point geometries.
// The azimuth is angle is referenced from north, and is positive clockwise.
// North = 0; East = π/2; South = π; West = 3π/2.
// Returns nil if the two points are the same.
// Returns an error if any of the two Geography items are not points.
func Azimuth(a *geo.Geography, b *geo.Geography) (*float64, error) {
if a.SRID() != b.SRID() {
return nil, geo.NewMismatchingSRIDsError(a, b)
}
aGeomT, err := a.AsGeomT()
if err != nil {
return nil, err
}
aPoint, ok := aGeomT.(*geom.Point)
if !ok {
return nil, errors.Newf("arguments must be POINT geometries")
}
bGeomT, err := b.AsGeomT()
if err != nil {
return nil, err
}
bPoint, ok := bGeomT.(*geom.Point)
if !ok {
return nil, errors.Newf("arguments must be POINT geometries")
}
if aPoint.Empty() || bPoint.Empty() {
return nil, errors.Newf("cannot call ST_Azimuth with POINT EMPTY")
}
if aPoint.X() == bPoint.X() && aPoint.Y() == bPoint.Y() {
return nil, nil
}
s, err := a.Spheroid()
if err != nil {
return nil, err
}
_, az1, _ := s.Inverse(
s2.LatLngFromDegrees(aPoint.Y(), aPoint.X()),
s2.LatLngFromDegrees(bPoint.Y(), bPoint.X()),
)
// Convert to radians.
az1 = az1 * math.Pi / 180
return &az1, nil
}