< Summary

Class:Itinero.IO.Osm.Restrictions.Turns.OsmTurnRestrictionExtensions
Assembly:Itinero.IO.Osm
File(s):/home/runner/work/routing2/routing2/src/Itinero.IO.Osm/Restrictions/Turns/OsmTurnRestrictionExtensions.cs
Covered lines:92
Uncovered lines:13
Coverable lines:105
Total lines:167
Line coverage:87.6% (92 of 105)
Covered branches:45
Total branches:64
Branch coverage:70.3% (45 of 64)
Tag:251_23667616543

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
ToGlobalNetworkRestrictions()83.33%6100%
GetViaTail(...)50%1466.66%
GetTailHops()87.5%8100%
GetViaHead(...)64.28%1477.77%
GetHeadHops()87.5%8100%
GetViaHops(...)71.42%1488.88%

File(s)

/home/runner/work/routing2/routing2/src/Itinero.IO.Osm/Restrictions/Turns/OsmTurnRestrictionExtensions.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using Itinero.Network.Tiles.Standalone.Global;
 5
 6namespace Itinero.IO.Osm.Restrictions.Turns;
 7
 8public static class OsmTurnRestrictionExtensions
 9{
 10    /// <summary>
 11    /// Converts the given OSM turn restriction into one or more sequences on the network.
 12    /// </summary>
 13    /// <param name="osmTurnRestriction">The OSM turn restriction.</param>
 14    /// <returns>The restriction using network edges and vertices.</returns>
 15    public static IEnumerable<GlobalRestriction> ToGlobalNetworkRestrictions(this OsmTurnRestriction osmTurnRestriction)
 6916    {
 6917        var viaSequences = osmTurnRestriction.GetViaHops();
 6918        if (viaSequences == null) yield break;
 19
 31920        foreach (var tailEdge in osmTurnRestriction.GetTailHops())
 28221            foreach (var headEdge in osmTurnRestriction.GetHeadHops())
 5722            {
 5723                IEnumerable<GlobalEdgeId> edges = [tailEdge];
 5724                edges = edges.Concat(viaSequences).Concat([headEdge]);
 25
 5726                yield return new GlobalRestriction(edges,
 5727                    osmTurnRestriction.IsProbibitory, osmTurnRestriction.Attributes);
 5728            }
 6929    }
 30
 31    private static long? GetViaTail(this OsmTurnRestriction osmTurnRestriction)
 13832    {
 33        // assume the restriction has a via-node like most of them.
 13834        var node = osmTurnRestriction.ViaNodeId;
 27235        if (node != null) return node.Value;
 36
 37        // but some have via-ways, so get the node from the first via-way.
 438        var viaWay = osmTurnRestriction.Via.FirstOrDefault();
 439        if (viaWay == null) return null;
 1640        foreach (var fromWay in osmTurnRestriction.From)
 441        {
 442            if (fromWay.Nodes[^1] == viaWay.Nodes[0] ||
 443                fromWay.Nodes[^1] == viaWay.Nodes[^1])
 444            {
 445                return fromWay.Nodes[^1];
 46            }
 47
 048            if (fromWay.Nodes[0] == viaWay.Nodes[0] ||
 049                fromWay.Nodes[0] == viaWay.Nodes[^1])
 050            {
 051                return fromWay.Nodes[0];
 52            }
 053        }
 54
 55        // not cool, probably restriction not mapped correctly.
 056        return null;
 13857    }
 58
 59    private static IEnumerable<GlobalEdgeId> GetTailHops(
 60        this OsmTurnRestriction osmTurnRestriction)
 6961    {
 6962        var node = osmTurnRestriction.GetViaTail();
 6963        if (node == null) yield break;
 64
 31965        foreach (var fromWay in osmTurnRestriction.From)
 5666        {
 5667            if (fromWay.Nodes[^1] == node)
 5568            {
 5569                yield return GlobalEdgeId.Create(fromWay.Id!.Value, 0, fromWay.Nodes.Length - 1);
 5570                continue;
 71            }
 72
 173            if (fromWay.Nodes[0] == node)
 174            {
 175                yield return GlobalEdgeId.Create(fromWay.Id!.Value, fromWay.Nodes.Length - 1, 0);
 176            }
 177        }
 6978    }
 79
 80    private static long? GetViaHead(this OsmTurnRestriction osmTurnRestriction)
 12581    {
 82        // assume the restriction has a via-node like most of them.
 12583        var node = osmTurnRestriction.ViaNodeId;
 24684        if (node != null) return node.Value;
 85
 86        // but some have via-ways, so get the node from the first via-way.
 487        var viaWay = osmTurnRestriction.Via.FirstOrDefault();
 488        if (viaWay == null) return null;
 1689        foreach (var toWay in osmTurnRestriction.To)
 490        {
 491            if (toWay.Nodes[^1] == viaWay.Nodes[0] ||
 492                toWay.Nodes[^1] == viaWay.Nodes[^1])
 093            {
 094                return toWay.Nodes[^1];
 95            }
 96
 497            if (toWay.Nodes[0] == viaWay.Nodes[0] ||
 498                toWay.Nodes[0] == viaWay.Nodes[^1])
 499            {
 4100                return toWay.Nodes[0];
 101            }
 0102        }
 103
 0104        return null;
 125105    }
 106
 107    private static IEnumerable<GlobalEdgeId> GetHeadHops(
 108        this OsmTurnRestriction osmTurnRestriction)
 56109    {
 56110        var node = osmTurnRestriction.GetViaHead();
 56111        if (node == null) yield break;
 112
 113        // assume the restriction has a via-node like most of them.
 282114        foreach (var toWay in osmTurnRestriction.To)
 57115        {
 57116            if (toWay.Nodes[0] == node)
 42117            {
 42118                yield return GlobalEdgeId.Create(toWay.Id!.Value, 0, toWay.Nodes.Length - 1);
 42119                continue;
 120            }
 121
 15122            if (toWay.Nodes[^1] == node)
 15123            {
 15124                yield return GlobalEdgeId.Create(toWay.Id!.Value, toWay.Nodes.Length - 1, 0);
 15125            }
 15126        }
 56127    }
 128
 129    private static IReadOnlyList<GlobalEdgeId>? GetViaHops(
 130        this OsmTurnRestriction osmTurnRestriction)
 69131    {
 69132        var tailNode = osmTurnRestriction.GetViaTail();
 69133        if (tailNode == null) return null;
 69134        var headNode = osmTurnRestriction.GetViaHead();
 69135        if (headNode == null) return null;
 136
 137        // via is a node if true.
 136138        if (tailNode.Value == headNode.Value) return ArraySegment<GlobalEdgeId>.Empty;
 139
 140        // there have to be via ways at this point.
 141        // it is assumed ways are split to follow along the sequence.
 2142        var currentNode = tailNode.Value;
 2143        var edges = new List<GlobalEdgeId>();
 10144        foreach (var viaWay in osmTurnRestriction.Via)
 2145        {
 2146            if (viaWay.Nodes[0] == currentNode)
 1147            {
 1148                edges.Add(GlobalEdgeId.Create(viaWay.Id!.Value, 0, viaWay.Nodes.Length - 1));
 1149                currentNode = viaWay.Nodes[^1];
 1150            }
 1151            else if (viaWay.Nodes[^1] == currentNode)
 1152            {
 1153                edges.Add(GlobalEdgeId.Create(viaWay.Id!.Value, viaWay.Nodes.Length - 1, 0));
 1154                currentNode = viaWay.Nodes[0];
 1155            }
 156            else
 0157            {
 0158                return null;
 159            }
 2160        }
 161
 2162        if (currentNode != headNode)
 0163            return null;
 164
 2165        return edges;
 69166    }
 167}