-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.go
138 lines (94 loc) · 2.85 KB
/
geo.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package geo
import (
"errors"
"sync"
"github.com/StefanSchroeder/Golang-Ellipsoid/ellipsoid"
"github.com/dekoch/gouniversal/module/gpsnav/typenav"
)
type Geo struct {
start typenav.Pos
target typenav.Pos
current typenav.Pos
}
var mut sync.RWMutex
func (geo *Geo) SetStartPos(p typenav.Pos) {
mut.Lock()
defer mut.Unlock()
geo.start = p
}
func (geo *Geo) SetTargetPos(p typenav.Pos) {
mut.Lock()
defer mut.Unlock()
geo.target = p
}
func (geo *Geo) SetCurrentPos(p typenav.Pos) {
mut.Lock()
defer mut.Unlock()
geo.current = p
}
func (geo *Geo) GetCurrentPos() typenav.Pos {
mut.RLock()
defer mut.RUnlock()
return geo.current
}
func (geo *Geo) IsStartPosValid() bool {
mut.RLock()
defer mut.RUnlock()
return geo.start.Valid
}
func (geo *Geo) GetBearing() (float64, error) {
mut.RLock()
defer mut.RUnlock()
if geo.start.Valid == false ||
geo.current.Valid == false {
return 0.0, errors.New("position not valid")
}
g := ellipsoid.Init("WGS84", ellipsoid.Degrees, ellipsoid.Meter, ellipsoid.LongitudeIsSymmetric, ellipsoid.BearingIsSymmetric)
_, bearing := g.To(geo.start.Lat, geo.start.Lon, geo.current.Lat, geo.current.Lon)
return bearing, nil
}
// GetTargetBearing returns the bearing relative from current bearing to target bearing
// 0.00 = on way
// -12.34 = target is left
// +12.34 = target is right
func (geo *Geo) GetTargetBearing() (float64, error) {
mut.RLock()
defer mut.RUnlock()
if geo.start.Valid == false ||
geo.target.Valid == false ||
geo.current.Valid == false {
return 0.0, errors.New("position not valid")
}
g := ellipsoid.Init("WGS84", ellipsoid.Degrees, ellipsoid.Meter, ellipsoid.LongitudeIsSymmetric, ellipsoid.BearingIsSymmetric)
_, current := g.To(geo.start.Lat, geo.start.Lon, geo.current.Lat, geo.current.Lon)
_, target := g.To(geo.start.Lat, geo.start.Lon, geo.target.Lat, geo.target.Lon)
rel := current - target
if rel > 180.0 {
rel = rel - 360.0
} else if rel < -180.0 {
rel = rel + 360.0
}
return -rel, nil
}
func (geo *Geo) GetDistance() (float64, error) {
mut.RLock()
defer mut.RUnlock()
if geo.start.Valid == false ||
geo.current.Valid == false {
return 0.0, errors.New("position not valid")
}
g := ellipsoid.Init("WGS84", ellipsoid.Degrees, ellipsoid.Meter, ellipsoid.LongitudeIsSymmetric, ellipsoid.BearingIsSymmetric)
distance, _ := g.To(geo.start.Lat, geo.start.Lon, geo.current.Lat, geo.current.Lon)
return distance, nil
}
func (geo *Geo) GetTargetDistance() (float64, error) {
mut.RLock()
defer mut.RUnlock()
if geo.target.Valid == false ||
geo.current.Valid == false {
return 0.0, errors.New("position not valid")
}
g := ellipsoid.Init("WGS84", ellipsoid.Degrees, ellipsoid.Meter, ellipsoid.LongitudeIsSymmetric, ellipsoid.BearingIsSymmetric)
distance, _ := g.To(geo.current.Lat, geo.current.Lon, geo.target.Lat, geo.target.Lon)
return distance, nil
}