| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | using Itinero.Geo; |
| | | 4 | | |
| | | 5 | | namespace Itinero.Instructions.Types.Generators; |
| | | 6 | | |
| | | 7 | | internal class IntersectionInstructionGenerator : IInstructionGenerator |
| | | 8 | | { |
| | 7 | 9 | | public string Name { get; } = "intersection"; |
| | | 10 | | |
| | | 11 | | public BaseInstruction? Generate(IndexedRoute route, int offset) |
| | 4 | 12 | | { |
| | 4 | 13 | | if (route.Last == offset + 1) |
| | 0 | 14 | | { |
| | | 15 | | // The next maneuver is 'arrive', no need to emit a complicated intersection-instruction |
| | 0 | 16 | | return null; |
| | | 17 | | } |
| | | 18 | | |
| | 4 | 19 | | var branches = route.Branches[offset]; |
| | 4 | 20 | | if (branches.Count == 0) |
| | 0 | 21 | | { |
| | 0 | 22 | | return null; |
| | | 23 | | } |
| | | 24 | | |
| | 4 | 25 | | var incomingStreets = new List<(int relativeDegrees, IEnumerable<(string, string)> tags)>(); |
| | | 26 | | |
| | | 27 | | |
| | 4 | 28 | | var incomingDirection = route.ArrivingDirectionAt(offset); |
| | 26 | 29 | | foreach (var branch in branches) |
| | 7 | 30 | | { |
| | 7 | 31 | | var branchAbsDirection = branch.Coordinate.AngleWithMeridian(route.Shape[offset]); |
| | 7 | 32 | | var branchRelDirection = incomingDirection - branchAbsDirection; |
| | 7 | 33 | | incomingStreets.Add((branchRelDirection.NormalizeDegrees(), branch.Attributes)); |
| | 7 | 34 | | } |
| | | 35 | | |
| | 4 | 36 | | var directionChange = route.DirectionChangeAt(offset); |
| | 4 | 37 | | var nextStep = (directionChange, route.Meta[offset].Attributes); |
| | 4 | 38 | | incomingStreets.Add(nextStep); |
| | | 39 | | |
| | 15 | 40 | | incomingStreets = incomingStreets.OrderByDescending(br => br.relativeDegrees).ToList(); |
| | | 41 | | |
| | 4 | 42 | | var actualIndex = incomingStreets.IndexOf(nextStep); |
| | | 43 | | |
| | | 44 | | |
| | 4 | 45 | | var instruction = new IntersectionInstruction(route, |
| | 4 | 46 | | offset, |
| | 4 | 47 | | offset + 1, |
| | 4 | 48 | | directionChange, |
| | 4 | 49 | | incomingStreets, |
| | 4 | 50 | | (uint)actualIndex); |
| | | 51 | | |
| | 4 | 52 | | return instruction; |
| | 4 | 53 | | } |
| | | 54 | | } |