< 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:0
Uncovered lines:60
Coverable lines:60
Total lines:194
Line coverage:0% (0 of 60)
Covered branches:0
Total branches:12
Branch coverage:0% (0 of 12)
Tag:251_23667616543

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%10%
GetEdge(...)100%10%
get_EdgeTypeMap()100%10%
IsInTile(...)100%10%
get_TileId()100%10%
AddVertex(...)0%20%
AddEdge(...)0%80%
AddGlobalRestriction(...)100%10%
AddTurnCosts(...)0%20%
AddOutgoingBoundaryCrossing(...)100%10%
AddIncomingBoundaryCrossing(...)100%10%
GetResultingTile()100%10%

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
 019    internal StandaloneNetworkTileWriter(StandaloneNetworkTile tile, int zoom,
 020        (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) edgeTypeMap,
 021        (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) turnCostTypeMap)
 022    {
 023        _tile = tile;
 024        this.EdgeTypeMap = edgeTypeMap;
 025        _turnCostTypeMap = turnCostTypeMap;
 026        _zoom = zoom;
 027    }
 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)
 036    {
 037        var edge = new NetworkTileEnumerator();
 038        edge.MoveTo(_tile.NetworkTile);
 039        edge.MoveTo(edgeId, forward);
 040        return edge;
 041    }
 42
 43    /// <summary>
 44    /// Gets the edge type map.
 45    /// </summary>
 046    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)
 055    {
 56        // get the local tile id.
 057        var (x, y) = TileStatic.WorldToTile(longitude, latitude, _zoom);
 058        return _tile.TileId == TileStatic.ToLocalId(x, y, _zoom);
 059    }
 60
 61    /// <summary>
 62    /// Gets the tile id.
 63    /// </summary>
 064    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)
 075    {
 76        // check the local tile id.
 077        var (x, y) = TileStatic.WorldToTile(longitude, latitude, _zoom);
 078        var localTileId = TileStatic.ToLocalId(x, y, _zoom);
 079        if (_tile.TileId != localTileId) throw new ArgumentException("Coordinate are not inside the tile");
 80
 081        return _tile.NetworkTile.AddVertex(longitude, latitude, elevation);
 082    }
 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)
 0100    {
 0101        if (_tile.TileId != vertex1.TileId) throw new ArgumentException("Vertex not in tile");
 0102        if (_tile.TileId != vertex2.TileId) throw new ArgumentException("Vertex not in tile");
 103
 104        // get the edge length in centimeters.
 0105        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
 0110        var vertex1Location = (longitude, latitude, e);
 0111        if (!_tile.NetworkTile.TryGetVertex(vertex2, out longitude, out latitude, out e))
 0112        {
 0113            throw new ArgumentOutOfRangeException(nameof(vertex1), $"Vertex {vertex2} not found.");
 114        }
 115
 0116        var vertex2Location = (longitude, latitude, e);
 117
 0118        var length = (uint)(vertex1Location.DistanceEstimateInMeterShape(
 0119            vertex2Location, shape) * 100);
 120
 0121        return _tile.NetworkTile.AddEdge(vertex1, vertex2, shape, attributes, null, edgeTypeId, length, globalEdgeId);
 0122    }
 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)
 0132    {
 133        // get the turn cost type id.
 0134        var turnCostTypeId = _turnCostTypeMap.func(attributes);
 135
 136        // write to tile.
 0137        _tile.AddGlobalRestriction(sequence, isProhibitory, turnCostTypeId, attributes);
 0138    }
 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)
 0150    {
 0151        prefix ??= ArraySegment<EdgeId>.Empty;
 152
 153        // get the turn cost type id.
 0154        var turnCostTypeId = _turnCostTypeMap.func(attributes);
 155
 156        // add the turn cost table using the type id.
 0157        _tile.NetworkTile.AddTurnCosts(vertex, turnCostTypeId, edges, costs, attributes, prefix);
 0158    }
 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)
 0169    {
 0170        _tile.AddBoundaryCrossing(false, globalEdgeId, tail, attributes, edgeTypeId);
 0171    }
 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)
 0182    {
 0183        _tile.AddBoundaryCrossing(true, globalEdgeId, tail, attributes, edgeTypeId);
 0184    }
 185
 186    /// <summary>
 187    /// Gets the resulting tile.
 188    /// </summary>
 189    /// <returns></returns>
 190    public StandaloneNetworkTile GetResultingTile()
 0191    {
 0192        return _tile;
 0193    }
 194}