Skip to content

Commit 8920d16

Browse files
authored
Merge pull request #53 from pfeiferj/way-prioritization
Way prioritization
2 parents 2b45f0e + 9e0cc23 commit 8920d16

2 files changed

Lines changed: 65 additions & 18 deletions

File tree

mapd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func loop(state *State) {
147147
logde(errors.Wrap(err, "could not find ways around current location"))
148148
}
149149

150-
state.CurrentWay, err = GetCurrentWay(state.CurrentWay.Way, state.NextWays, offline, pos)
150+
state.CurrentWay, err = GetCurrentWay(state.CurrentWay, state.NextWays, offline, pos)
151151
logde(errors.Wrap(err, "could not get current way"))
152152

153153
state.NextWays, err = NextWays(pos, state.CurrentWay, offline, state.CurrentWay.OnWay.IsForward)

way.go

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type OnWayResult struct {
1515
IsForward bool
1616
}
1717

18-
func OnWay(way Way, pos Position) (OnWayResult, error) {
18+
func OnWay(way Way, pos Position, extended bool) (OnWayResult, error) {
1919
res := OnWayResult{}
2020
if pos.Latitude < way.MaxLat()+PADDING && pos.Latitude > way.MinLat()-PADDING && pos.Longitude < way.MaxLon()+PADDING && pos.Longitude > way.MinLon()-PADDING {
2121
d, err := DistanceToWay(pos, way)
@@ -30,6 +30,9 @@ func OnWay(way Way, pos Position) (OnWayResult, error) {
3030
}
3131
road_width_estimate := float64(lanes) * LANE_WIDTH
3232
max_dist := 5 + road_width_estimate
33+
if extended {
34+
max_dist = max_dist * 2
35+
}
3336

3437
if d.Distance < max_dist {
3538
res.OnWay = true
@@ -115,14 +118,14 @@ func GetWayStartEnd(way Way, isForward bool) (Coordinates, Coordinates) {
115118
return nodes.At(nodes.Len() - 1), nodes.At(0)
116119
}
117120

118-
func GetCurrentWay(currentWay Way, nextWays []NextWayResult, offline Offline, pos Position) (CurrentWay, error) {
119-
if currentWay.HasNodes() {
120-
onWay, err := OnWay(currentWay, pos)
121+
func GetCurrentWay(currentWay CurrentWay, nextWays []NextWayResult, offline Offline, pos Position) (CurrentWay, error) {
122+
if currentWay.Way.HasNodes() {
123+
onWay, err := OnWay(currentWay.Way, pos, false)
121124
logde(errors.Wrap(err, "could not check if on current way"))
122125
if onWay.OnWay {
123-
start, end := GetWayStartEnd(currentWay, onWay.IsForward)
126+
start, end := GetWayStartEnd(currentWay.Way, onWay.IsForward)
124127
return CurrentWay{
125-
Way: currentWay,
128+
Way: currentWay.Way,
126129
Distance: onWay.Distance,
127130
OnWay: onWay,
128131
StartPosition: start,
@@ -133,7 +136,7 @@ func GetCurrentWay(currentWay Way, nextWays []NextWayResult, offline Offline, po
133136

134137
// check the expected next ways
135138
for _, nextWay := range nextWays {
136-
onWay, err := OnWay(nextWay.Way, pos)
139+
onWay, err := OnWay(nextWay.Way, pos, false)
137140
logde(errors.Wrap(err, "could not check if on next way"))
138141
if onWay.OnWay {
139142
start, end := GetWayStartEnd(nextWay.Way, onWay.IsForward)
@@ -147,19 +150,46 @@ func GetCurrentWay(currentWay Way, nextWays []NextWayResult, offline Offline, po
147150
}
148151
}
149152

150-
// finally check all other ways
151-
ways, err := offline.Ways()
152-
if err != nil {
153-
return CurrentWay{}, errors.Wrap(err, "could not get other ways")
154-
}
155-
for i := 0; i < ways.Len(); i++ {
156-
way := ways.At(i)
157-
onWay, err := OnWay(way, pos)
153+
possibleWays, err := getPossibleWays(offline, pos)
154+
logde(errors.Wrap(err, "Failed to get possible ways"))
155+
if len(possibleWays) > 0 {
156+
preferredWay := possibleWays[0]
157+
preferredOnWay, err := OnWay(preferredWay, pos, false)
158158
logde(errors.Wrap(err, "Could not check if on way"))
159+
for _, way := range possibleWays {
160+
if way.Lanes() < preferredWay.Lanes() {
161+
continue
162+
}
163+
164+
onWay, err := OnWay(preferredWay, pos, false)
165+
logde(errors.Wrap(err, "Could not check if on way"))
166+
if way.Lanes() > preferredWay.Lanes() {
167+
preferredWay = way
168+
preferredOnWay = onWay
169+
}
170+
171+
if onWay.Distance.Distance < preferredOnWay.Distance.Distance {
172+
preferredWay = way
173+
preferredOnWay = onWay
174+
}
175+
}
176+
start, end := GetWayStartEnd(preferredWay, preferredOnWay.IsForward)
177+
return CurrentWay{
178+
Way: preferredWay,
179+
Distance: preferredOnWay.Distance,
180+
OnWay: preferredOnWay,
181+
StartPosition: start,
182+
EndPosition: end,
183+
}, nil
184+
}
185+
186+
if currentWay.Way.HasNodes() { // if we lost all matches, allow a much further match distance for previous match
187+
onWay, err := OnWay(currentWay.Way, pos, true)
188+
logde(errors.Wrap(err, "could not extended check if on current way"))
159189
if onWay.OnWay {
160-
start, end := GetWayStartEnd(way, onWay.IsForward)
190+
start, end := GetWayStartEnd(currentWay.Way, onWay.IsForward)
161191
return CurrentWay{
162-
Way: way,
192+
Way: currentWay.Way,
163193
Distance: onWay.Distance,
164194
OnWay: onWay,
165195
StartPosition: start,
@@ -171,6 +201,23 @@ func GetCurrentWay(currentWay Way, nextWays []NextWayResult, offline Offline, po
171201
return CurrentWay{}, errors.New("could not find a current way")
172202
}
173203

204+
func getPossibleWays(offline Offline, pos Position) ([]Way, error) {
205+
possibleWays := []Way{}
206+
ways, err := offline.Ways()
207+
if err != nil {
208+
return possibleWays, errors.Wrap(err, "could not get other ways")
209+
}
210+
for i := 0; i < ways.Len(); i++ {
211+
way := ways.At(i)
212+
onWay, err := OnWay(way, pos, false)
213+
logde(errors.Wrap(err, "Could not check if on way"))
214+
if onWay.OnWay {
215+
possibleWays = append(possibleWays, way)
216+
}
217+
}
218+
return possibleWays, nil
219+
}
220+
174221
func IsForward(lineStart Coordinates, lineEnd Coordinates, bearing float64) bool {
175222
startLat := lineStart.Latitude()
176223
startLon := lineStart.Longitude()

0 commit comments

Comments
 (0)