< Summary

Class:Itinero.Routing.IRouterExtensions
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Routing/IRouterExtensions.cs
Covered lines:90
Uncovered lines:34
Coverable lines:124
Total lines:232
Line coverage:72.5% (90 of 124)
Covered branches:23
Total branches:32
Branch coverage:71.8% (23 of 32)
Tag:263_26948838820

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
From(...)100%1100%
From(...)100%1100%
From(...)100%10%
From(...)100%10%
From(...)100%10%
From(...)100%10%
CalculateAsync()100%8100%
<CalculateAsync()50%257.14%
CalculateAsync()87.5%890%
<CalculateAsync()50%257.14%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Routing/IRouterExtensions.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Threading;
 4using System.Threading.Tasks;
 5using Itinero.Geo;
 6using Itinero.Geo.Directions;
 7using Itinero.Network;
 8using Itinero.Routes.Paths;
 9using Itinero.Routing.Alternatives;
 10using Itinero.Routing.Costs;
 11using Itinero.Routing.Flavours.Dijkstra;
 12using Itinero.Snapping;
 13
 14namespace Itinero.Routing;
 15
 16/// <summary>
 17///     Contains extensions for the IRouter interface.
 18/// </summary>
 19public static class IRouterExtensions
 20{
 21    /// <summary>
 22    ///     Configures the router to route from the given point.
 23    /// </summary>
 24    /// <param name="router">The router.</param>
 25    /// <param name="snapPoint">The point to route from.</param>
 26    /// <returns>A configured router.</returns>
 27    public static IHasSource From(this IRouter router, SnapPoint snapPoint)
 1161128    {
 1161129        return router.From((snapPoint, (bool?)null));
 1161130    }
 31
 32    /// <summary>
 33    ///     Configures the router to route from the given point.
 34    /// </summary>
 35    /// <param name="router">The router.</param>
 36    /// <param name="directedSnapPoint">The point to route from.</param>
 37    /// <returns>A configured router.</returns>
 38    public static IHasSource From(this IRouter router, (SnapPoint snapPoint, bool? direction) directedSnapPoint)
 1161139    {
 1161140        return new Router(router.Network, router.Settings)
 1161141        {
 1161142            Source = directedSnapPoint
 1161143        };
 1161144    }
 45
 46    /// <summary>
 47    ///     Configures the router to route from the given point.
 48    /// </summary>
 49    /// <param name="router">The router.</param>
 50    /// <param name="directedSnapPoint">The point to route from.</param>
 51    /// <returns>A configured router.</returns>
 52    public static IHasSource From(this IRouter router,
 53        (SnapPoint snapPoint, DirectionEnum? direction) directedSnapPoint)
 054    {
 055        return router.From(directedSnapPoint.ToDirected(router.Network));
 056    }
 57
 58    /// <summary>
 59    ///     Configures the router to route from the given point.
 60    /// </summary>
 61    /// <param name="router">The router.</param>
 62    /// <param name="snapPoints">The points to route from.</param>
 63    /// <returns>A configured router.</returns>
 64    public static IHasSources From(this IRouter router, IReadOnlyList<SnapPoint> snapPoints)
 065    {
 066        return router.From(snapPoints.ToDirected());
 067    }
 68
 69    /// <summary>
 70    ///     Configures the router to route from the given point.
 71    /// </summary>
 72    /// <param name="router">The router.</param>
 73    /// <param name="directedSnapPoints">The points to route from.</param>
 74    /// <returns>A configured router.</returns>
 75    public static IHasSources From(this IRouter router,
 76        IReadOnlyList<(SnapPoint snapPoint, DirectionEnum? direction)> directedSnapPoints)
 077    {
 078        return router.From(directedSnapPoints.ToDirected(router.Network));
 079    }
 80
 81    /// <summary>
 82    ///     Configures the router to route from the given point.
 83    /// </summary>
 84    /// <param name="router">The router.</param>
 85    /// <param name="directedSnapPoints">The points to route from.</param>
 86    /// <returns>A configured router.</returns>
 87    public static IHasSources From(this IRouter router,
 88        IReadOnlyList<(SnapPoint snapPoint, bool? direction)> directedSnapPoints)
 089    {
 090        return new Router(router.Network, router.Settings)
 091        {
 092            Sources = directedSnapPoints
 093        };
 094    }
 95
 96    internal static async Task<IReadOnlyList<IReadOnlyList<Result<Path>>>> CalculateAsync(this IRouter manyToManyRouter,
 97        IReadOnlyList<SnapPoint> sources, IReadOnlyList<SnapPoint> targets, CancellationToken cancellationToken)
 1154298    {
 1154299        var settings = manyToManyRouter.Settings;
 11542100        var routingNetwork = manyToManyRouter.Network;
 101
 11542102        var profile = settings.Profile;
 11542103        var costFunction = routingNetwork.GetCostFunctionFor(profile);
 104
 11542105        var maxBox = settings.MaxBoxFor(routingNetwork, sources);
 106
 107        bool CheckMaxDistance(VertexId v)
 304245108        {
 304245109            if (maxBox == null)
 4110            {
 4111                return false;
 112            }
 113
 304241114            if (routingNetwork == null)
 0115            {
 0116                throw new Exception("Router cannot be null here.");
 117            }
 118
 304241119            var vertex = routingNetwork.GetVertex(v);
 304241120            if (!maxBox.Value.Overlaps(vertex))
 51273121            {
 51273122                return true;
 123            }
 124
 252968125            return false;
 304245126        }
 127
 11542128        var isMainN = routingNetwork.GetIsMainNFunc(profile);
 11542129        var results = new IReadOnlyList<Result<Path>>[sources.Count];
 46168130        for (var s = 0; s < sources.Count; s++)
 11542131        {
 11542132            var source = sources[s];
 11542133            var pathsAndCosts = await Flavours.Dijkstra.Dijkstra.Default.RunAsync(routingNetwork, source, targets,
 11542134                costFunction.GetDijkstraWeightFunc(),
 11542135                async v =>
 304245136                {
 304245137                    if (!routingNetwork.UsageNotifier.IsVertexDataReady(routingNetwork, v.vertexId))
 0138                    {
 0139                        await routingNetwork.UsageNotifier.NotifyVertex(routingNetwork, v.vertexId, cancellationToken);
 0140                    }
 304245141                    return CheckMaxDistance(v.vertexId);
 315787142                }, isMainN: isMainN);
 143
 11542144            var sourceResults = new Result<Path>[pathsAndCosts.Length];
 402636145            for (var r = 0; r < sourceResults.Length; r++)
 189776146            {
 189776147                var (path, _) = pathsAndCosts[r];
 189776148                if (path == null)
 59484149                {
 59484150                    sourceResults[r] = new Result<Path>("Path not found!");
 59484151                }
 152                else
 130292153                {
 130292154                    sourceResults[r] = path;
 130292155                }
 189776156            }
 157
 11542158            results[s] = sourceResults;
 11542159        }
 160
 11542161        return results;
 11542162    }
 163
 164    internal static async Task<IReadOnlyList<IReadOnlyList<Result<Path>>>> CalculateAsync(this IRouter manyToManyRouter,
 165        IReadOnlyList<(SnapPoint snapPoint, bool? direction)> sources,
 166        IReadOnlyList<(SnapPoint snapPoint, bool? direction)> targets)
 5167    {
 5168        var settings = manyToManyRouter.Settings;
 5169        var routerDb = manyToManyRouter.Network;
 170
 5171        var profile = settings.Profile;
 5172        var costFunction = routerDb.GetCostFunctionFor(profile);
 173
 5174        var maxBox = settings.MaxBoxFor(routerDb, sources);
 175
 176        bool CheckMaxDistance(VertexId v)
 12177        {
 12178            if (maxBox == null)
 12179            {
 12180                return false;
 181            }
 182
 0183            if (routerDb == null)
 0184            {
 0185                throw new Exception("Router cannot be null here.");
 186            }
 187
 0188            var vertex = routerDb.GetVertex(v);
 0189            if (!maxBox.Value.Overlaps(vertex))
 0190            {
 0191                return true;
 192            }
 193
 0194            return false;
 12195        }
 196
 5197        var isMainN = routerDb.GetIsMainNFunc(profile);
 5198        var results = new IReadOnlyList<Result<Path>>[sources.Count];
 20199        for (var s = 0; s < sources.Count; s++)
 5200        {
 5201            var source = sources[s];
 5202            var paths = await Flavours.Dijkstra.Dijkstra.Default.RunAsync(routerDb, source, targets,
 5203                costFunction.GetDijkstraWeightFunc(),
 5204                async e =>
 12205                {
 12206                    if (!routerDb.UsageNotifier.IsVertexDataReady(routerDb, e.vertexId))
 0207                    {
 0208                        await routerDb.UsageNotifier.NotifyVertex(routerDb, e.vertexId);
 0209                    }
 12210                    return CheckMaxDistance(e.vertexId);
 17211                }, isMainN: isMainN);
 212
 5213            var sourceResults = new Result<Path>[paths.Length];
 20214            for (var r = 0; r < sourceResults.Length; r++)
 5215            {
 5216                var (path, _) = paths[r];
 5217                if (path == null)
 5218                {
 5219                    sourceResults[r] = new Result<Path>("Routes not found!");
 5220                }
 221                else
 0222                {
 0223                    sourceResults[r] = path;
 0224                }
 5225            }
 226
 5227            results[s] = sourceResults;
 5228        }
 229
 5230        return results;
 5231    }
 232}