Allow start point to be within 500m of bounding box of its nearest neighbour
This commit is contained in:
parent
617b12ff64
commit
af15ee6707
3 changed files with 59 additions and 1 deletions
48
cmd/summarize-json/main.go
Normal file
48
cmd/summarize-json/main.go
Normal file
|
@ -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
|
||||
}
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue