| | 1 | | namespace Itinero.Instructions.Types.Generators; |
| | 2 | |
|
| | 3 | | internal class RoundaboutInstructionGenerator : IInstructionGenerator |
| | 4 | | { |
| 11 | 5 | | public string Name { get; } = "roundabout"; |
| | 6 | |
|
| | 7 | | public BaseInstruction? Generate(IndexedRoute route, int offset) |
| 12 | 8 | | { |
| | 9 | | // The roundabout instruction starts when the next segment is on the roundabout ("Go on the roundabout...") |
| | 10 | | // and ends when the person leaves the roundabout ("... and take the n'th exit") |
| | 11 | |
|
| 12 | 12 | | if (offset >= route.Last - 1) |
| 4 | 13 | | { // No next entries |
| 4 | 14 | | return null; |
| | 15 | | } |
| | 16 | |
|
| 8 | 17 | | var usedInstructions = 1; |
| 8 | 18 | | var exitCount = 0; |
| 17 | 19 | | while (route.Meta[offset + usedInstructions].GetAttributeOrNull("junction") == "roundabout") |
| 9 | 20 | | { |
| 9 | 21 | | if (route.Branches.Count > offset + usedInstructions) |
| 9 | 22 | | { |
| 9 | 23 | | exitCount += route.Branches[offset + usedInstructions].Count; |
| 9 | 24 | | } |
| | 25 | |
|
| 9 | 26 | | usedInstructions++; |
| 9 | 27 | | } |
| | 28 | |
|
| 8 | 29 | | if (usedInstructions == 1) |
| 5 | 30 | | { |
| | 31 | | // We didn't find a roundabout in the end |
| 5 | 32 | | return null; |
| | 33 | | } |
| | 34 | |
|
| 3 | 35 | | var inDegrees = route.ArrivingDirectionAt(offset); // Offset is still on the rampup |
| 3 | 36 | | var outDegrees = route.DepartingDirectionAt(offset + usedInstructions); |
| | 37 | |
|
| 3 | 38 | | var shapeIndexEnd = offset + usedInstructions; |
| 3 | 39 | | if (shapeIndexEnd + 1 < route.Last) |
| 2 | 40 | | { |
| | 41 | | // We add an extra index, as the first segment after the roundabout doesn't need an instruction for it; |
| | 42 | | // This is always a right turn anyway |
| 2 | 43 | | shapeIndexEnd++; |
| 2 | 44 | | } |
| | 45 | |
|
| 3 | 46 | | return new RoundaboutInstruction(route, |
| 3 | 47 | | offset, |
| 3 | 48 | | shapeIndexEnd, |
| 3 | 49 | | (inDegrees - outDegrees).NormalizeDegrees(), |
| 3 | 50 | | exitCount + 1 |
| 3 | 51 | | ); |
| 12 | 52 | | } |
| | 53 | | } |