2020-04-15 09:46:58 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-04-15 20:56:10 +01:00
|
|
|
"encoding/json"
|
2020-04-15 09:46:58 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2020-04-15 20:56:10 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2020-04-15 09:46:58 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
2020-04-15 20:56:10 +01:00
|
|
|
"path"
|
2020-04-15 09:46:58 +01:00
|
|
|
|
|
|
|
"github.com/ray1729/gpx-utils/pkg/openname"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-04-15 20:56:10 +01:00
|
|
|
openNames := flag.String("opname", "", "Path to Ordnance Server Open Names zip archive")
|
2020-04-15 09:46:58 +01:00
|
|
|
gpxFile := flag.String("gpx", "", "Path to GPX file")
|
2020-04-15 20:56:10 +01:00
|
|
|
dirName := flag.String("dir", "", "Directory to scan for GPX files")
|
2020-04-15 09:46:58 +01:00
|
|
|
flag.Parse()
|
|
|
|
if *openNames == "" {
|
2020-04-15 20:56:10 +01:00
|
|
|
log.Fatal("--opname is required")
|
2020-04-15 09:46:58 +01:00
|
|
|
}
|
2020-04-15 20:56:10 +01:00
|
|
|
if (*gpxFile == "" && *dirName == "") || (*gpxFile != "" && *dirName != "") {
|
|
|
|
log.Fatal("exactly one of --dir or --gpx is required")
|
2020-04-15 09:46:58 +01:00
|
|
|
}
|
2020-04-16 08:52:37 +01:00
|
|
|
rt, err := openname.BuildIndex(*openNames)
|
2020-04-15 09:46:58 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-16 08:52:37 +01:00
|
|
|
gs, err := openname.NewGPXSummarizer(rt)
|
2020-04-15 09:46:58 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-15 20:56:10 +01:00
|
|
|
if *gpxFile != "" {
|
2020-04-16 08:52:37 +01:00
|
|
|
err = summarizeSingleFile(gs, *gpxFile)
|
2020-04-15 20:56:10 +01:00
|
|
|
} else {
|
2020-04-16 08:52:37 +01:00
|
|
|
err = summarizeDirectory(gs, *dirName)
|
2020-04-15 20:56:10 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 08:52:37 +01:00
|
|
|
func summarizeDirectory(gs *openname.GPXSummarizer, dirName string) error {
|
2020-04-15 20:56:10 +01:00
|
|
|
files, err := ioutil.ReadDir(dirName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
if f.IsDir() || path.Ext(f.Name()) != ".gpx" {
|
2020-04-15 09:46:58 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-04-15 20:56:10 +01:00
|
|
|
filename := path.Join(dirName, f.Name())
|
2020-04-17 13:01:05 +01:00
|
|
|
r, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error opening %s for reading: %v", filename, err)
|
|
|
|
}
|
2020-04-15 20:56:10 +01:00
|
|
|
log.Printf("Analyzing %s", filename)
|
2020-04-17 13:01:05 +01:00
|
|
|
summary, err := gs.SummarizeTrack(r)
|
2020-04-15 20:56:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating summary of GPX track %s: %v", filename, err)
|
|
|
|
}
|
|
|
|
outfile := filename[:len(filename)-4] + ".json"
|
|
|
|
wc, err := os.OpenFile(outfile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating output file %s: %v", outfile, err)
|
|
|
|
}
|
|
|
|
err = writeSummary(summary, wc)
|
|
|
|
if err != nil {
|
|
|
|
wc.Close()
|
|
|
|
return fmt.Errorf("error marshalling JSON to %s: %v", outfile, err)
|
|
|
|
}
|
|
|
|
if err = wc.Close(); err != nil {
|
|
|
|
return fmt.Errorf("error closing file %s: %v", outfile, err)
|
2020-04-15 09:46:58 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-15 20:56:10 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-16 08:52:37 +01:00
|
|
|
func summarizeSingleFile(gs *openname.GPXSummarizer, filename string) error {
|
2020-04-17 13:01:05 +01:00
|
|
|
r, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error opening %s for reading: %v", filename, err)
|
|
|
|
}
|
|
|
|
summary, err := gs.SummarizeTrack(r)
|
2020-04-15 20:56:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating summary of GPX track %s: %v", filename, err)
|
|
|
|
}
|
|
|
|
if err = writeSummary(summary, os.Stdout); err != nil {
|
|
|
|
return fmt.Errorf("error marshalling summary for %s: %v", filename, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-16 08:52:37 +01:00
|
|
|
func writeSummary(s *openname.TrackSummary, w io.Writer) error {
|
2020-04-15 20:56:10 +01:00
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
if err := enc.Encode(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-04-15 09:46:58 +01:00
|
|
|
}
|