From f0b45fd4c6809d9e2306ffd04b8d301d91b4eb56 Mon Sep 17 00:00:00 2001 From: Ray Miller Date: Tue, 14 Jul 2020 09:15:08 +0100 Subject: [PATCH] Allow minimum distance between POI and minimum settlement to be overridden --- cmd/analyze-gpx/main.go | 6 +++++- pkg/placenames/summarize.go | 29 +++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/cmd/analyze-gpx/main.go b/cmd/analyze-gpx/main.go index ddb842d..e88cdb8 100644 --- a/cmd/analyze-gpx/main.go +++ b/cmd/analyze-gpx/main.go @@ -19,9 +19,11 @@ import ( func main() { log.SetFlags(0) stopNames := flag.String("stops", "", "Source for refreshment stops") + minDist := flag.Float64("min-dist", 0.2, "Minimum distance (km) between points of interest") + minSettlement := flag.String("min-settlement", "Other Settlement", "Exclude populated places smaller than this (City, Town, Village, Hamlet, Other Settlement)") flag.Parse() if flag.NArg() != 1 { - log.Fatal("Usage: %s [--stops=ctccambridge|cyclingmaps] GPX_FILE_OR_DIRECTORY") + log.Fatal("Usage: %s [--stops=ctccambridge|cyclingmaps] [--min-dist X] [--min-settlement S] GPX_FILE_OR_DIRECTORY") } inFile := flag.Arg(0) info, err := os.Stat(inFile) @@ -40,6 +42,8 @@ func main() { if err != nil { log.Fatal(err) } + gs.SetMinDistance(*minDist) + gs.SetMinSettlement(*minSettlement) if info.IsDir() { err = summarizeDirectory(gs, stops, inFile) } else { diff --git a/pkg/placenames/summarize.go b/pkg/placenames/summarize.go index 2393a80..a347c4d 100644 --- a/pkg/placenames/summarize.go +++ b/pkg/placenames/summarize.go @@ -13,9 +13,19 @@ import ( "github.com/ray1729/gpx-utils/pkg/cafes" ) +var populatedPlaceRank = map[string]int{ + "City": 5, + "Town": 4, + "Village": 3, + "Hamlet": 3, + "Other Settlement": 1, +} + type GPXSummarizer struct { - poi *rtreego.Rtree - trans osgb.CoordinateTransformer + poi *rtreego.Rtree + trans osgb.CoordinateTransformer + minDist float64 + minSettlementRank int } func NewGPXSummarizer() (*GPXSummarizer, error) { @@ -27,7 +37,15 @@ func NewGPXSummarizer() (*GPXSummarizer, error) { if err != nil { return nil, err } - return &GPXSummarizer{poi: rt, trans: trans}, nil + return &GPXSummarizer{poi: rt, trans: trans, minDist: 0.2, minSettlementRank: 1}, nil +} + +func (gs *GPXSummarizer) SetMinSettlement(t string) { + gs.minSettlementRank = populatedPlaceRank[t] +} + +func (gs *GPXSummarizer) SetMinDistance(d float64) { + gs.minDist = d } func distance(p1, p2 rtreego.Point) float64 { @@ -116,7 +134,10 @@ func (gs *GPXSummarizer) SummarizeTrack(r io.Reader, stops *rtreego.Rtree) (*Tra s.Distance += distance(thisPoint, prevPoint) dE += thisPoint[0] - start[0] dN += thisPoint[1] - start[1] - if nn.Contains(thisPoint) && nn.Name != prevPlace && distance(thisPoint, prevPlacePoint) > 0.2 { + if nn.Contains(thisPoint) && + nn.Name != prevPlace && + distance(thisPoint, prevPlacePoint) > gs.minDist && + populatedPlaceRank[nn.Type] >= gs.minSettlementRank { s.PointsOfInterest = append(s.PointsOfInterest, POI{Name: nn.Name, Type: nn.Type, Distance: s.Distance}) prevPlace = nn.Name prevPlacePoint = thisPoint