< Summary

Class:Itinero.Network.Tiles.Standalone.Writer.StandaloneNetworkTileWriter
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/Standalone/Writer/StandaloneNetworkTileWriter.cs
Covered lines:56
Uncovered lines:4
Coverable lines:60
Total lines:194
Line coverage:93.3% (56 of 60)
Covered branches:6
Total branches:12
Branch coverage:50% (6 of 12)
Tag:263_26948838820

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
GetEdge(...)100%1100%
get_EdgeTypeMap()100%1100%
IsInTile(...)100%1100%
get_TileId()100%1100%
AddVertex(...)50%2100%
AddEdge(...)50%873.33%
AddGlobalRestriction(...)100%1100%
AddTurnCosts(...)50%2100%
AddOutgoingBoundaryCrossing(...)100%1100%
AddIncomingBoundaryCrossing(...)100%1100%
GetResultingTile()100%1100%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/Standalone/Writer/StandaloneNetworkTileWriter.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using Itinero.Geo;
 4using Itinero.Network.Tiles.Standalone.Global;
 5
 6// ReSharper disable PossibleMultipleEnumeration
 7
 8namespace Itinero.Network.Tiles.Standalone.Writer;
 9
 10/// <summary>
 11/// A writer class to write to a standalone tile.
 12/// </summary>
 13public class StandaloneNetworkTileWriter
 14{
 15    private readonly StandaloneNetworkTile _tile;
 16    private readonly int _zoom;
 17    private readonly (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) _turnCostTypeMap;
 18
 2719    internal StandaloneNetworkTileWriter(StandaloneNetworkTile tile, int zoom,
 2720        (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) edgeTypeMap,
 2721        (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) turnCostTypeMap)
 2722    {
 2723        _tile = tile;
 2724        this.EdgeTypeMap = edgeTypeMap;
 2725        _turnCostTypeMap = turnCostTypeMap;
 2726        _zoom = zoom;
 2727    }
 28
 29    /// <summary>
 30    /// Gets the edge associated with the given id.
 31    /// </summary>
 32    /// <param name="edgeId">The edge id.</param>
 33    /// <param name="forward">The forward flag.</param>
 34    /// <returns>The edge details.</returns>
 35    public INetworkTileEdge GetEdge(EdgeId edgeId, bool forward)
 2236    {
 2237        var edge = new NetworkTileEnumerator();
 2238        edge.MoveTo(_tile.NetworkTile);
 2239        edge.MoveTo(edgeId, forward);
 2240        return edge;
 2241    }
 42
 43    /// <summary>
 44    /// Gets the edge type map.
 45    /// </summary>
 2746    public (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) EdgeTypeMap { get; }
 47
 48    /// <summary>
 49    /// Returns true if the given coordinates are inside the tile boundaries.
 50    /// </summary>
 51    /// <param name="longitude">The longitude.</param>
 52    /// <param name="latitude">The latitude.</param>
 53    /// <returns>True if inside, false otherwise.</returns>
 54    public bool IsInTile(double longitude, double latitude)
 23655    {
 56        // get the local tile id.
 23657        var (x, y) = TileStatic.WorldToTile(longitude, latitude, _zoom);
 23658        return _tile.TileId == TileStatic.ToLocalId(x, y, _zoom);
 23659    }
 60
 61    /// <summary>
 62    /// Gets the tile id.
 63    /// </summary>
 2264    public uint TileId => _tile.TileId;
 65
 66    /// <summary>
 67    /// Adds a new vertex.
 68    /// </summary>
 69    /// <param name="longitude">The longitude.</param>
 70    /// <param name="latitude">The latitude.</param>
 71    /// <param name="elevation">The elevation.</param>
 72    /// <returns>The new vertex id.</returns>
 73    /// <exception cref="ArgumentException"></exception>
 74    public VertexId AddVertex(double longitude, double latitude, float? elevation = null)
 7775    {
 76        // check the local tile id.
 7777        var (x, y) = TileStatic.WorldToTile(longitude, latitude, _zoom);
 7778        var localTileId = TileStatic.ToLocalId(x, y, _zoom);
 7779        if (_tile.TileId != localTileId) throw new ArgumentException("Coordinate are not inside the tile");
 80
 7781        return _tile.NetworkTile.AddVertex(longitude, latitude, elevation);
 7782    }
 83
 84    /// <summary>
 85    /// Adds a new edge.
 86    /// </summary>
 87    /// <param name="vertex1">The from vertex.</param>
 88    /// <param name="vertex2">The to vertex.</param>>
 89    /// <param name="edgeTypeId">The edge type id.</param>
 90    /// <param name="shape">The shape, if any.</param>
 91    /// <param name="attributes">The attributes, if any.</param>
 92    /// <param name="globalEdgeId">The global edge id, if any.</param>
 93    /// <returns>The edge id.</returns>
 94    /// <exception cref="ArgumentException"></exception>
 95    /// <exception cref="ArgumentOutOfRangeException"></exception>
 96    public EdgeId AddEdge(VertexId vertex1, VertexId vertex2, uint edgeTypeId,
 97        IEnumerable<(double longitude, double latitude, float? e)>? shape = null,
 98        IEnumerable<(string key, string value)>? attributes = null,
 99        GlobalEdgeId? globalEdgeId = null)
 50100    {
 50101        if (_tile.TileId != vertex1.TileId) throw new ArgumentException("Vertex not in tile");
 50102        if (_tile.TileId != vertex2.TileId) throw new ArgumentException("Vertex not in tile");
 103
 104        // get the edge length in centimeters.
 50105        if (!_tile.NetworkTile.TryGetVertex(vertex1, out var longitude, out var latitude, out var e))
 0106        {
 0107            throw new ArgumentOutOfRangeException(nameof(vertex1), $"Vertex {vertex1} not found.");
 108        }
 109
 50110        var vertex1Location = (longitude, latitude, e);
 50111        if (!_tile.NetworkTile.TryGetVertex(vertex2, out longitude, out latitude, out e))
 0112        {
 0113            throw new ArgumentOutOfRangeException(nameof(vertex1), $"Vertex {vertex2} not found.");
 114        }
 115
 50116        var vertex2Location = (longitude, latitude, e);
 117
 50118        var length = (uint)(vertex1Location.DistanceEstimateInMeterShape(
 50119            vertex2Location, shape) * 100);
 120
 50121        return _tile.NetworkTile.AddEdge(vertex1, vertex2, shape, attributes, null, edgeTypeId, length, globalEdgeId);
 50122    }
 123
 124    /// <summary>
 125    /// Adds a global restriction for processing when the tile is loaded.
 126    /// </summary>
 127    /// <param name="sequence">The sequence of global edge ids with local edge ids if they are already known.</param>
 128    /// <param name="isProhibitory">The type of restriction.</param>
 129    /// <param name="attributes">The raw attributes of the restriction.</param>
 130    public void AddGlobalRestriction(IEnumerable<(GlobalEdgeId globalEdgeId, EdgeId? edge)> sequence,
 131        bool isProhibitory, IEnumerable<(string key, string value)> attributes)
 14132    {
 133        // get the turn cost type id.
 14134        var turnCostTypeId = _turnCostTypeMap.func(attributes);
 135
 136        // write to tile.
 14137        _tile.AddGlobalRestriction(sequence, isProhibitory, turnCostTypeId, attributes);
 14138    }
 139
 140    /// <summary>
 141    /// Adds turn costs.
 142    /// </summary>
 143    /// <param name="vertex">The vertex where the costs are located.</param>
 144    /// <param name="attributes">The attributes representing the type of costs.</param>
 145    /// <param name="edges">The edges involved in the costs.</param>
 146    /// <param name="costs">The costs.</param>
 147    /// <param name="prefix">When the costs are only valid after first traversing a sequence of edges.</param>
 148    public void AddTurnCosts(VertexId vertex, IEnumerable<(string key, string value)> attributes,
 149        EdgeId[] edges, uint[,] costs, IEnumerable<EdgeId>? prefix = null)
 22150    {
 22151        prefix ??= ArraySegment<EdgeId>.Empty;
 152
 153        // get the turn cost type id.
 22154        var turnCostTypeId = _turnCostTypeMap.func(attributes);
 155
 156        // add the turn cost table using the type id.
 22157        _tile.NetworkTile.AddTurnCosts(vertex, turnCostTypeId, edges, costs, attributes, prefix);
 22158    }
 159
 160    /// <summary>
 161    /// Adds a new outgoing boundary crossing.
 162    /// </summary>
 163    /// <param name="globalEdgeId">The global edge id, a stable identified to match boundary edges.</param>
 164    /// <param name="tail">The tail vertex.</param>
 165    /// <param name="edgeTypeId">The edge type id.</param>
 166    /// <param name="attributes">The attributes.</param>
 167    public void AddOutgoingBoundaryCrossing(GlobalEdgeId globalEdgeId, VertexId tail,
 168        uint edgeTypeId, IEnumerable<(string key, string value)> attributes)
 9169    {
 9170        _tile.AddBoundaryCrossing(false, globalEdgeId, tail, attributes, edgeTypeId);
 9171    }
 172
 173    /// <summary>
 174    /// Adds a new incoming boundary crossing.
 175    /// </summary>
 176    /// <param name="globalEdgeId">The global edge id, a stable identified to match boundary edges.</param>
 177    /// <param name="tail">The head vertex.</param>
 178    /// <param name="edgeTypeId">The edge type id.</param>
 179    /// <param name="attributes">The attributes.</param>
 180    public void AddIncomingBoundaryCrossing(GlobalEdgeId globalEdgeId, VertexId tail,
 181        uint edgeTypeId, IEnumerable<(string key, string value)> attributes)
 8182    {
 8183        _tile.AddBoundaryCrossing(true, globalEdgeId, tail, attributes, edgeTypeId);
 8184    }
 185
 186    /// <summary>
 187    /// Gets the resulting tile.
 188    /// </summary>
 189    /// <returns></returns>
 190    public StandaloneNetworkTile GetResultingTile()
 54191    {
 54192        return _tile;
 54193    }
 194}