-
Notifications
You must be signed in to change notification settings - Fork 0
/
trackpoint.go
348 lines (315 loc) · 9.62 KB
/
trackpoint.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package catTrackslib
import (
"encoding/json"
"errors"
"math"
"reflect"
"time"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
catnames "github.com/rotblauer/cattracks-names"
)
// TrackPoint Stores a snippet of life, love, and location
type TrackPoint struct {
Uuid string `json:"uuid"`
PushToken string `json:"pushToken"`
Version string `json:"version"`
ID int64 `json:"id"` // either bolt auto id or unixnano //think nano is better cuz can check for dupery
Name string `json:"name"`
Lat float64 `json:"lat"`
Lng float64 `json:"long"`
Accuracy float64 `json:"accuracy"` // horizontal, in meters
VAccuracy float64 `json:"vAccuracy"` // vertical, in meteres
Elevation float64 `json:"elevation"` // in meters
Speed float64 `json:"speed"` // in m/s
SpeedAccuracy float64 `json:"speed_accuracy"` // in meters per second
Tilt float64 `json:"tilt"` // degrees?
Heading float64 `json:"heading"` // in degrees
HeadingAccuracy float64 `json:"heading_accuracy"`
HeartRate float64 `json:"heartrate"` // bpm
Time time.Time `json:"time"`
Floor int `json:"floor"` // building floor if available
Notes string `json:"notes"` // special events of the day
COVerified bool `json:"COVerified"`
RemoteAddr string `json:"remoteaddr"`
}
type TrackPoints []*TrackPoint
// https://stackoverflow.com/questions/18390266/how-can-we-truncate-float64-type-to-a-particular-precision
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func toFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(round(num*output)) / output
}
// TrackToFeature2 (WIP/experimental) is a track->geojson function that uses reflection to
// transfer fields. This might be useful for a more dynamic approach to geojson, but it's
// probably better in the broader scheme to just swap trackpoints for geojson entirely, though
// this would require coordinated changes between the client (cattracker) and server.
func TrackToFeature2(tp *TrackPoint) *geojson.Feature {
if tp == nil {
return nil
}
// config
var timeFormat = time.RFC3339
p := geojson.NewFeature(orb.Point{tp.Lng, tp.Lat})
props := make(map[string]interface{})
tpV := reflect.ValueOf(*tp)
typeOfS := tpV.Type()
stringSliceContains := func(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
skipTrackFields := []string{"Lat", "Lng", "PushToken", "Version", "COVerified", "RemoteAddr", "Notes"}
for i := 0; i < tpV.NumField(); i++ {
fieldName := typeOfS.Field(i).Name
if stringSliceContains(skipTrackFields, fieldName) {
continue
}
switch t := tpV.Field(i).Interface().(type) {
case time.Time:
props[typeOfS.Field(i).Name] = t.Format(timeFormat)
case float64:
props[typeOfS.Field(i).Name] = toFixed(t, 2)
case string:
if t != "" {
props[typeOfS.Field(i).Name] = t
}
default:
props[typeOfS.Field(i).Name] = tpV.Field(i).Interface()
}
}
if ns, e := NotesField(tp.Notes).AsNoteStructured(); e == nil {
tpN := reflect.ValueOf(ns)
typeOfN := tpN.Type()
if ns.HasValidVisit() {
props["Visit"] = ns.Visit
}
skipNoteFields := []string{"Visit", "NetworkInfo"}
for i := 0; i < tpN.NumField(); i++ {
if stringSliceContains(skipNoteFields, typeOfN.Field(i).Name) {
continue
}
switch t := tpN.Field(i).Interface().(type) {
case time.Time:
props[typeOfN.Field(i).Name] = t.Format(timeFormat)
case float64:
props[typeOfN.Field(i).Name] = toFixed(t, 2)
case string:
if t != "" {
props[typeOfN.Field(i).Name] = t
}
default:
props[typeOfN.Field(i).Name] = tpN.Field(i).Interface()
}
}
}
p.Properties = props
return p
}
// TrackToFeature converts a TrackPoint to a GeoJSON feature.
func TrackToFeature(trackPointCurrent *TrackPoint) *geojson.Feature {
p := geojson.NewFeature(orb.Point{trackPointCurrent.Lng, trackPointCurrent.Lat})
// currently need speed, name,time
props := make(map[string]interface{})
defer func() {
p.Properties = props
}()
// If an alias exists for the cat, install it as a property.
if alias := catnames.AliasOrName(trackPointCurrent.Name); alias != trackPointCurrent.Name {
props["Alias"] = alias
}
props["UUID"] = trackPointCurrent.Uuid
props["Name"] = trackPointCurrent.Name
props["Time"] = trackPointCurrent.Time
props["UnixTime"] = trackPointCurrent.Time.Unix()
props["Version"] = trackPointCurrent.Version
props["Speed"] = toFixed(trackPointCurrent.Speed, 3)
props["Elevation"] = toFixed(trackPointCurrent.Elevation, 2)
props["Heading"] = toFixed(trackPointCurrent.Heading, 1)
props["Accuracy"] = toFixed(trackPointCurrent.Accuracy, 2)
if trackPointCurrent.VAccuracy > 0 {
props["vAccuracy"] = trackPointCurrent.VAccuracy
}
if trackPointCurrent.SpeedAccuracy > 0 {
props["speed_accuracy"] = trackPointCurrent.SpeedAccuracy
}
if trackPointCurrent.HeadingAccuracy > 0 {
props["heading_accuracy"] = trackPointCurrent.HeadingAccuracy
}
// not implemented yet
if hr := trackPointCurrent.HeartRate; hr != 0 {
props["HeartRate"] = hr
}
if ns, e := NotesField(trackPointCurrent.Notes).AsNoteStructured(); e == nil {
props["Activity"] = ns.Activity
if v := ns.ActivityConfidence; v != nil {
props["ActivityConfidence"] = *v
}
props["Pressure"] = toFixed(ns.Pressure, 2)
if ns.CustomNote != "" {
props["Notes"] = ns.CustomNote
}
if ns.ImgS3 != "" {
props["imgS3"] = ns.ImgS3
}
if ns.HasRawImage() {
props["imgB64"] = ns.ImgB64
}
if ns.HasValidVisit() {
// TODO: ok to use mappy sub interface here?
props["Visit"] = ns.Visit
}
if trackPointCurrent.HeartRate == 0 {
if i := ns.HeartRateI(); i > 0 {
props["HeartRate"] = toFixed(i, 2)
}
}
// these properties might exist in the track, but we haven't been dumping them to json,
// they're not deal breakers, but nice to have
if ns.NumberOfSteps > 0 {
props["NumberOfSteps"] = ns.NumberOfSteps
}
if ns.AverageActivePace > 0 {
props["AverageActivePace"] = toFixed(ns.AverageActivePace, 2)
}
if ns.CurrentPace > 0 {
props["CurrentPace"] = toFixed(ns.CurrentPace, 2)
}
if ns.CurrentCadence > 0 {
props["CurrentCadence"] = toFixed(ns.CurrentCadence, 2)
}
if ns.CustomNote != "" {
props["CustomNote"] = ns.CustomNote
}
if ns.FloorsAscended > 0 {
props["FloorsAscended"] = ns.FloorsAscended
}
if ns.FloorsDescended > 0 {
props["FloorsDescended"] = ns.FloorsDescended
}
if !ns.CurrentTripStart.IsZero() {
props["CurrentTripStart"] = ns.CurrentTripStart
}
if ns.Distance > 0 {
props["Distance"] = toFixed(ns.Distance, 2)
}
if ns.Lightmeter > 0 {
props["Lightmeter"] = toFixed(ns.Lightmeter, 2)
}
if ns.AmbientTemp > 0 {
props["AmbientTemp"] = toFixed(ns.AmbientTemp, 2)
}
if ns.Humidity > 0 {
props["Humidity"] = toFixed(ns.Humidity, 2)
}
if v := ns.Accelerometer.X; v != nil {
props["AccelerometerX"] = *v
}
if v := ns.Accelerometer.Y; v != nil {
props["AccelerometerY"] = *v
}
if v := ns.Accelerometer.Z; v != nil {
props["AccelerometerZ"] = *v
}
if v := ns.UserAccelerometer.X; v != nil {
props["UserAccelerometerX"] = *v
}
if v := ns.UserAccelerometer.Y; v != nil {
props["UserAccelerometerY"] = *v
}
if v := ns.UserAccelerometer.Z; v != nil {
props["UserAccelerometerZ"] = *v
}
if v := ns.Gyroscope.X; v != nil {
props["GyroscopeX"] = *v
}
if v := ns.Gyroscope.Y; v != nil {
props["GyroscopeY"] = *v
}
if v := ns.Gyroscope.Z; v != nil {
props["GyroscopeZ"] = *v
}
if v := ns.BatteryStatus; v != "" {
bs := BatteryStatus{}
if err := json.Unmarshal([]byte(v), &bs); err == nil {
props["BatteryStatus"] = bs.Status
props["BatteryLevel"] = toFixed(bs.Level, 2)
}
}
if v := ns.NetworkInfo; v != "" {
props["NetworkInfo"] = v
}
// if trackPointCurrent.HeartRate == 0 && ns.HeartRateType != "" {
// props["HeartRateType"] = ns.HeartRateType
// }
} else if _, e := NotesField(trackPointCurrent.Notes).AsFingerprint(); e == nil {
// maybe do something with identity consolidation?
} else {
// NOOP normal
// props["Notes"] = note.NotesField(trackPointCurrent.Notes).AsNoteString()
}
return p
}
func FeatureToTrack(f geojson.Feature) (TrackPoint, error) {
var err error
tp := TrackPoint{}
p, ok := f.Geometry.(orb.Point)
if !ok {
return tp, errors.New("not a point")
}
tp.Lng = p.Lon()
tp.Lat = p.Lat()
if v, ok := f.Properties["UUID"]; ok {
tp.Uuid = v.(string)
}
if v, ok := f.Properties["Name"]; ok {
tp.Name = v.(string)
}
if v, ok := f.Properties["Time"]; ok {
tp.Time, err = time.Parse(time.RFC3339, v.(string))
if err != nil {
return tp, err
}
}
if v, ok := f.Properties["Version"]; ok {
tp.Version = v.(string)
}
if v, ok := f.Properties["Speed"]; ok {
tp.Speed = v.(float64)
}
if v, ok := f.Properties["Elevation"]; ok {
tp.Elevation = v.(float64)
}
if v, ok := f.Properties["Heading"]; ok {
tp.Heading = v.(float64)
}
if v, ok := f.Properties["Accuracy"]; ok {
tp.Accuracy = v.(float64)
}
if v, ok := f.Properties["HeartRate"]; ok {
tp.HeartRate = v.(float64)
}
anyNotes := false
notes := NoteStructured{}
if v, ok := f.Properties["Activity"]; ok {
notes.Activity = v.(string)
anyNotes = true
}
if v, ok := f.Properties["Pressure"]; ok {
notes.Pressure = v.(float64)
anyNotes = true
}
if v, ok := f.Properties["imgS3"]; ok {
notes.ImgS3 = v.(string)
anyNotes = true
}
if anyNotes {
tp.Notes = notes.MustAsString()
}
return tp, nil
}