diff --git a/cmd/summarize-json/main.go b/cmd/summarize-json/main.go new file mode 100644 index 0000000..61cb98e --- /dev/null +++ b/cmd/summarize-json/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "encoding/csv" + "encoding/json" + "io/ioutil" + "log" + "os" + "path" + "strconv" + + "github.com/ray1729/gpx-utils/pkg/placenames" +) + +func main() { + log.SetFlags(0) + w := csv.NewWriter(os.Stdout) + for _, filename := range os.Args[1:] { + ts, err := readTrackSummary(filename) + if err != nil { + log.Fatal(err) + } + w.Write(constructRow(path.Base(filename), ts)) + } + w.Flush() +} + +func constructRow(filename string, ts *placenames.TrackSummary) []string { + row := make([]string, 5) + row[0] = filename[:10] + row[1] = ts.Start + row[2] = ts.Finish + row[3] = strconv.FormatFloat(ts.Distance, 'f', 1, 32) + row[4] = strconv.FormatFloat(ts.Ascent, 'f', 0, 32) + return row +} + +func readTrackSummary(path string) (*placenames.TrackSummary, error) { + data, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + var ts placenames.TrackSummary + if err := json.Unmarshal(data, &ts); err != nil { + return nil, err + } + return &ts, nil +} diff --git a/pkg/placenames/placenames.go b/pkg/placenames/placenames.go index 14e0948..fe3774e 100644 --- a/pkg/placenames/placenames.go +++ b/pkg/placenames/placenames.go @@ -29,6 +29,16 @@ func (b *NamedBoundary) Bounds() *rtreego.Rect { return r } +func (b *NamedBoundary) NearEnough(p rtreego.Point, margin float64) bool { + if len(p) != 2 { + panic("Expected a 2-dimensional point") + } + return p[0] >= b.Xmin-margin && + p[0] <= b.Xmax+margin && + p[1] >= b.Ymin-margin && + p[1] <= b.Ymax+margin +} + func (b *NamedBoundary) Contains(p rtreego.Point) bool { if len(p) != 2 { panic("Expected a 2-dimensional point") diff --git a/pkg/placenames/summarize.go b/pkg/placenames/summarize.go index 8014a6e..b436237 100644 --- a/pkg/placenames/summarize.go +++ b/pkg/placenames/summarize.go @@ -125,7 +125,7 @@ func (gs *GPXSummarizer) SummarizeTrack(r io.Reader, stops *rtreego.Rtree) (*Tra thisPoint := rtreego.Point{ngCoord.Easting, ngCoord.Northing} nn, _ := gs.poi.NearestNeighbor(thisPoint).(*NamedBoundary) if init { - if !nn.Contains(thisPoint) { + if !nn.NearEnough(thisPoint, 500.0) { return nil, fmt.Errorf("start point out of range") } start = thisPoint