< Summary

Class:Itinero.Instructions.IRouteAndInstructionsExtensions
Assembly:Itinero.Instructions
File(s):/home/runner/work/routing2/routing2/src/Itinero.Instructions/IRouteAndInstructionsExtensions.cs
Covered lines:59
Uncovered lines:0
Coverable lines:59
Total lines:102
Line coverage:100% (59 of 59)
Covered branches:17
Total branches:18
Branch coverage:94.4% (17 of 18)
Tag:230_15134869466

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
AugmentRoute(...)94.44%18100%

File(s)

/home/runner/work/routing2/routing2/src/Itinero.Instructions/IRouteAndInstructionsExtensions.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using Itinero.Routes;
 5
 6namespace Itinero.Instructions;
 7
 8/// <summary>
 9///     Contains extension methods for further operations after instruction generation.
 10/// </summary>
 11// ReSharper disable once InconsistentNaming
 12public static class IRouteAndInstructionsExtensions
 13{
 14
 15    /// <summary>
 16    ///     Adds the instructions to the route object.
 17    /// </summary>
 18    /// <remarks>
 19    ///     Some instructions have a length of 0 (thus: instruction.ShapeIndex == instruction.ShapeIndexEnd).
 20    ///     This will result in multiple shapeMetas pointing to the same 'shape'. Use <see cref="RemoveDuplicateShapeMet
 21    ///     to remove them.
 22    /// </remarks>
 23    /// <param name="routeAndInstructions">The route and the instructions.</param>
 24    /// <param name="keyForLanguage">A callback to configure the key per language code.</param>
 25    /// <returns>Another augmented route and the same instructions.</returns>
 26    public static IRouteAndInstructions AugmentRoute(this IRouteAndInstructions routeAndInstructions,
 27        Func<string, string>? keyForLanguage = null)
 228    {
 229        keyForLanguage ??= l => $"instruction:{l}";
 30
 231        var route = routeAndInstructions.Route;
 232        var instructions = routeAndInstructions.Instructions;
 233        var shapeMetas = routeAndInstructions.Route.ShapeMeta;
 34
 235        var metas = new List<Route.Meta>();
 36        // 'instructions' and 'shapeMeta' might have different boundaries - so reusing the old shapeMeta's is not possib
 37        // We build a new set of shapeMeta instead
 238        var instructionPointer = 0;
 239        var shapeMetaPointer = 0;
 240        var routeCount = shapeMetas.Last().Shape;
 41
 242        Route.Meta? lastMeta = null;
 743        while ((lastMeta?.Shape ?? 0) < routeCount &&
 744               instructionPointer < instructions.Count &&
 745               shapeMetaPointer < shapeMetas.Count)
 546        {
 547            var currentMeta = shapeMetas[shapeMetaPointer];
 548            var currentInstruction = instructions[instructionPointer];
 49
 50
 551            var latestIncludedPoint = Math.Min(
 552                currentMeta.Shape,
 553                currentInstruction.BaseInstruction.ShapeIndexEnd);
 54
 555            var attributes = new List<(string key, string value)>(currentMeta.Attributes);
 2556            foreach (var (languageCode, text) in currentInstruction.Text)
 557            {
 558                attributes.Add((keyForLanguage(languageCode), text));
 559            }
 60
 561            var distance = route.DistanceBetween(lastMeta?.Shape ?? 0, latestIncludedPoint);
 562            var speed = currentMeta.Distance / currentMeta.Time;
 63
 564            var meta = new Route.Meta
 565            {
 566                Shape = latestIncludedPoint,
 567                AttributesAreForward = currentMeta.AttributesAreForward,
 568                Attributes = attributes,
 569                Profile = currentMeta.Profile,
 570                Distance = distance,
 571                Time = speed * distance
 572            };
 73
 574            if (currentMeta.Shape == meta.Shape)
 375            {
 376                shapeMetaPointer++;
 377            }
 78
 579            if (currentInstruction.BaseInstruction.ShapeIndexEnd == meta.Shape)
 280            {
 281                instructionPointer++;
 282            }
 83
 584            metas.Add(meta);
 585            lastMeta = meta;
 586        }
 87
 288        var augmentedRoute = new Route
 289        {
 290            Attributes = route.Attributes,
 291            Branches = route.Branches,
 292            Profile = route.Profile,
 293            Shape = route.Shape,
 294            Stops = route.Stops,
 295            TotalDistance = route.TotalDistance,
 296            TotalTime = route.TotalTime,
 297            ShapeMeta = metas
 298        };
 99
 2100        return new RouteAndInstructions(augmentedRoute, instructions);
 2101    }
 102}

Methods/Properties

AugmentRoute(...)