-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
283 lines (262 loc) · 8.11 KB
/
main.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
package main
import (
"errors"
"fmt"
"log"
"os"
"time"
_ "embed"
"github.com/jamesjarvis/mappyboi/v2/pkg/base"
"github.com/jamesjarvis/mappyboi/v2/pkg/input/fit"
"github.com/jamesjarvis/mappyboi/v2/pkg/input/google"
"github.com/jamesjarvis/mappyboi/v2/pkg/input/gpx"
"github.com/jamesjarvis/mappyboi/v2/pkg/input/polarsteps"
"github.com/jamesjarvis/mappyboi/v2/pkg/maptemplate"
"github.com/jamesjarvis/mappyboi/v2/pkg/parser"
"github.com/jamesjarvis/mappyboi/v2/pkg/transform"
"github.com/urfave/cli/v2"
)
//go:embed VERSION
var version string
var (
baseFileFlag = "base_file"
// Input
googleLocationHistoryFlag = "google_location_history"
gpxDirectoryFlag = "gpx_directory"
fitDirectoryFlag = "fit_directory"
polarstepDirectoryFlag = "polarstep_directory"
// Output
outputTypeFlag = "output_type"
outputFileFlag = "output_file"
// Transformations
outputTransformReducePointsFlag = "output_reduce_points"
outputRandomisePoints = "output_randomise_points"
outputFilterStartDate = "output_filter_start_date"
outputFilterEndDate = "output_filter_end_date"
)
type output string
const (
output_UNKNOWN output = "UNKNOWN"
output_MAP output = "MAP"
)
func mustCreateFileIfNotExists(filePath string) {
_, err := os.Stat(filePath)
if err == nil {
return
}
if !errors.Is(err, os.ErrNotExist) {
panic(err)
}
f, err := os.Create(filePath)
if err != nil {
panic(err)
}
defer f.Close()
}
func mustConvertOutputType(o string) output {
switch o {
case string(output_MAP):
return output_MAP
}
return output_UNKNOWN
}
func app(c *cli.Context) error {
log.Println("Mappyboi " + c.App.Version)
mustCreateFileIfNotExists(c.Path(baseFileFlag))
// Setup base.
log.Printf("Loading Base file from %s...\n", c.Path(baseFileFlag))
baseLocationHistory, err := base.ReadBase(c.Path(baseFileFlag))
if err != nil {
return err
}
originalLength := len(baseLocationHistory.Data)
log.Printf("Loaded Base file from %s, %d entries\n", c.Path(baseFileFlag), originalLength)
// Parse additional files and fold back into base.
var parsers []parser.Parser
if c.IsSet(googleLocationHistoryFlag) {
parsers = append(parsers, &google.LocationHistory{
Filepath: c.Path(googleLocationHistoryFlag),
})
}
if c.IsSet(gpxDirectoryFlag) {
gpxs, err := gpx.FindGPXFiles(c.Path(gpxDirectoryFlag))
if err != nil {
return err
}
for _, p := range gpxs {
parsers = append(parsers, p)
}
}
if c.IsSet(fitDirectoryFlag) {
fits, err := fit.FindFitFiles(c.Path(fitDirectoryFlag))
if err != nil {
return err
}
for _, p := range fits {
parsers = append(parsers, p)
}
}
if c.IsSet(polarstepDirectoryFlag) {
psteps, err := polarsteps.FindPolarstepsFiles(c.Path(polarstepDirectoryFlag))
if err != nil {
return err
}
for _, p := range psteps {
parsers = append(parsers, p)
}
}
if len(parsers) > 0 {
log.Printf("Parsing supplied location files...\n")
parsedLocationHistory, err := parser.ParseAll(parsers...)
if err != nil {
return err
}
log.Printf("Parsed %d entries from supplied location files\n", len(parsedLocationHistory.Data))
baseLocationHistory.Insert(parsedLocationHistory.Data...)
log.Printf("Combined all locations into %d entries (%d new)\n", len(baseLocationHistory.Data), len(baseLocationHistory.Data)-originalLength)
}
// Cleanup location history.
err = baseLocationHistory.Cleanup()
if err != nil {
return err
}
// Write to base.
if len(baseLocationHistory.Data) > originalLength {
log.Printf("Writing %d entries to Base file %s...", len(baseLocationHistory.Data), c.Path(baseFileFlag))
err = base.WriteBase(c.Path(baseFileFlag), baseLocationHistory)
if err != nil {
return err
}
log.Printf("Completed writing to Base file %s", c.Path(baseFileFlag))
} else {
log.Printf("Skipping write, as data is unchanged\n")
}
// Run output transformations.
var transformers []transform.Transformer
// Simplify routes to only include points on or after the provided date.
if c.IsSet(outputFilterStartDate) {
t := c.Timestamp(outputFilterStartDate)
transformers = append(transformers, transform.WithStartDate(*t))
}
// Simplify routes to only include points on or before the provided date.
if c.IsSet(outputFilterEndDate) {
t := c.Timestamp(outputFilterEndDate)
transformers = append(transformers, transform.WithEndDate(*t))
}
// Simplify routes to minimise number of points.
// Unfortunately leaflet will stack overflow after around 600k points :'(
if c.IsSet(outputTransformReducePointsFlag) {
minDistance := c.Float64(outputTransformReducePointsFlag)
transformers = append(transformers, transform.WithMinimumDistance(minDistance))
}
// Randomise output.
if c.IsSet(outputRandomisePoints) {
transformers = append(transformers, transform.WithRandomOrder())
}
baseLocationHistory, err = transform.ProcessPoints(baseLocationHistory, transformers...)
if err != nil {
return fmt.Errorf("error transforming points: %w", err)
}
// Generate output.
if c.IsSet(outputTypeFlag) && c.IsSet(outputFileFlag) {
log.Printf("Generating output of type %s to %s...\n", c.String(outputTypeFlag), c.Path(outputFileFlag))
outputType := mustConvertOutputType(c.String(outputTypeFlag))
switch outputType {
case output_UNKNOWN:
return fmt.Errorf("invalid output type %q, valid types are: [ MAP ]", c.String(outputTypeFlag))
case output_MAP:
err = maptemplate.GenerateHTML(c.Path(outputFileFlag), baseLocationHistory)
if err != nil {
return err
}
}
}
return nil
}
func main() {
app := &cli.App{
Name: "mappyboi v2",
Usage: "Store all Google Takeout / Apple Health exports and transform to custom outputs",
Flags: []cli.Flag{
&cli.PathFlag{
Name: baseFileFlag,
Aliases: []string{"base"},
Usage: "Base location history append only `FILE`, in .json or .json.gz (by suffixing with .gz mappyboi will compress the resulting file, using significantly less storage)",
TakesFile: true,
Required: true,
},
&cli.PathFlag{
Name: googleLocationHistoryFlag,
Aliases: []string{"glh"},
Usage: "Google Takeout Location History `FILE`",
TakesFile: true,
},
&cli.PathFlag{
Name: gpxDirectoryFlag,
Aliases: []string{"gpxd"},
Usage: "GPX `DIRECTORY` to load .gpx files from",
TakesFile: false,
},
&cli.PathFlag{
Name: fitDirectoryFlag,
Aliases: []string{"fitd"},
Usage: "FIT `DIRECTORY` to load .fit files from",
TakesFile: false,
},
&cli.PathFlag{
Name: polarstepDirectoryFlag,
Aliases: []string{"pstepd"},
Usage: "Polarstep `DIRECTORY` to load locations.json files from",
TakesFile: false,
},
&cli.StringFlag{
Name: outputTypeFlag,
Aliases: []string{"ot"},
Usage: "Output format, must be one of [ MAP ]",
Value: string(output_MAP), // Default value of MAP.
},
&cli.PathFlag{
Name: outputFileFlag,
Aliases: []string{"of"},
Usage: "Output `FILE` to write to",
TakesFile: true,
},
&cli.Float64Flag{
Name: outputTransformReducePointsFlag,
Aliases: []string{"rp"},
Usage: "If you struggle to open the file in a browser due to too many points, reduce the number of points by increasing this value.",
},
&cli.BoolFlag{
Name: outputRandomisePoints,
Aliases: []string{"rand"},
Usage: "If you want to export the view of the points, but otherwise randomise the data to prevent perfect tracking, this will randomise the order.",
},
&cli.TimestampFlag{
Name: outputFilterStartDate,
Layout: time.RFC3339,
Aliases: []string{"from"},
Usage: "To filter the output to only include points on or after the provided timestamp",
},
&cli.TimestampFlag{
Name: outputFilterEndDate,
Layout: time.RFC3339,
Aliases: []string{"to"},
Usage: "To filter the output to only include points on or before the provided timestamp",
},
cli.VersionFlag,
},
Action: app,
}
app.Version = version
app.EnableBashCompletion = true
app.Authors = []*cli.Author{
{
Name: "James Jarvis",
Email: "[email protected]",
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}