-
Notifications
You must be signed in to change notification settings - Fork 17
/
maps.go
203 lines (169 loc) · 4.31 KB
/
maps.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*************************************************************************
* Copyright 2021 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package types
import (
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"math"
"strconv"
)
var (
ErrInvalidGeofence = errors.New("Invalid geofence")
)
type PointmapKV struct {
Key string
Value string
}
type Location struct {
Lat float64
Long float64
}
type PointmapValue struct {
Loc Location
Metadata []PointmapKV `json:",omitempty"`
}
type PointmapRequest struct {
BaseRequest
Fence Geofence `json:",omitempty"`
}
type PointmapResponse struct {
BaseResponse
Entries []PointmapValue `json:",omitempty"`
}
type HeatmapRequest struct {
BaseRequest
Fence Geofence `json:",omitempty"`
}
type HeatmapResponse struct {
BaseResponse
Entries []HeatmapValue `json:",omitempty"`
}
type HeatmapValue struct {
Location
Magnitude float64 `json:",omitempty"`
}
type P2PValue struct {
Src Location
Dst Location
Magnitude float64 `json:",omitempty"`
Values []string `json:",omitempty"`
}
type P2PRequest struct {
BaseRequest
Fence Geofence `json:",omitempty"`
}
type P2PResponse struct {
BaseResponse
ValueNames []string
Entries []P2PValue `json:",omitempty"`
}
type Geofence struct {
SouthWest Location `json:",omitempty"`
NorthEast Location `json:",omitempty"`
enabled bool
}
func (pkv PointmapKV) IsEmpty() bool {
return len(pkv.Key) == 0 || len(pkv.Value) == 0
}
func (gf *Geofence) CrossesAntimeridian() bool {
return gf.SouthWest.Long > gf.NorthEast.Long
}
func (gf *Geofence) Validate() error {
if gf.SouthWest.Lat == 0.0 && gf.SouthWest.Long == 0.0 && gf.NorthEast.Lat == 0.0 && gf.NorthEast.Long == 0.0 {
return nil
}
// Check bounds of Lat / Long
if !gf.SouthWest.Valid() || !gf.NorthEast.Valid() {
return ErrInvalidGeofence
}
// Ensure that "SouthWest" is not North of "NorthEast"
if gf.SouthWest.Lat > gf.NorthEast.Lat {
return ErrInvalidGeofence
}
// SouthWest.Long can be greater than NorthEast.Long (A bounds that crosses the antimeridian)
// Or
// SouthWest.Long can be less than NorthEast.Long (A bounds that DOES NOT cross the antimeridian)
gf.enabled = true
return nil
}
func (gf *Geofence) InFence(loc Location) bool {
if !loc.Valid() {
return false
}
if !gf.enabled {
return true //if its not enabled, everything is always in the fence
}
if loc.Lat > gf.NorthEast.Lat || loc.Lat < gf.SouthWest.Lat {
return false
}
if gf.CrossesAntimeridian() {
if loc.Long > gf.NorthEast.Long && loc.Long < gf.SouthWest.Long {
return false
}
} else {
if loc.Long > gf.NorthEast.Long || loc.Long < gf.SouthWest.Long {
return false
}
}
return true
}
func (loc Location) Encode() (v []byte) {
v = make([]byte, 16)
binary.LittleEndian.PutUint64(v, math.Float64bits(loc.Lat))
binary.LittleEndian.PutUint64(v[8:], math.Float64bits(loc.Long))
return
}
func (loc *Location) Decode(v []byte) bool {
if len(v) < 16 {
return false
}
loc.Lat = math.Float64frombits(binary.LittleEndian.Uint64(v))
loc.Long = math.Float64frombits(binary.LittleEndian.Uint64(v[8:]))
return true
}
func (loc Location) String() string {
return fmt.Sprintf("%f %f", loc.Lat, loc.Long)
}
func (loc Location) Valid() bool {
// Latitude is bound -90 to 90
if loc.Lat > 90.0 || loc.Lat < -90.0 {
return false
}
// Longitude is bound -180 to 180
if loc.Long > 180.0 || loc.Long < -180.0 {
return false
}
return true
}
func (pkv PointmapKV) MarshalJSON() (r []byte, err error) {
r = []byte(fmt.Sprintf(`{"%s":%s}`, pkv.Key, strconv.Quote(pkv.Value)))
return
}
func (hv HeatmapValue) MarshalJSON() ([]byte, error) {
if hv.Magnitude == 0.0 {
return []byte(fmt.Sprintf(`[%f, %f]`, hv.Lat, hv.Long)), nil
}
return []byte(fmt.Sprintf(`[%f, %f, %f]`, hv.Lat, hv.Long, hv.Magnitude)), nil
}
func (hv *HeatmapValue) UnmarshalJSON(data []byte) error {
var a []float64
if err := json.Unmarshal(data, &a); err != nil {
return err
}
if len(a) != 3 && len(a) != 2 {
return fmt.Errorf("Expected 2 or 3 values, got %v", len(a))
}
hv.Lat = a[0]
hv.Long = a[1]
if len(a) == 3 {
hv.Magnitude = a[2]
}
return nil
}