< 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:52
Uncovered lines:10
Coverable lines:62
Total lines:187
Line coverage:83.8% (52 of 62)
Covered branches:6
Total branches:12
Branch coverage:50% (6 of 12)
Tag:224_14471318300

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%
AddGlobalIdFor(...)100%1100%
AddGlobalIdFor(...)100%10%
AddTurnCosts(...)50%2100%
AddBoundaryCrossing(...)100%1100%
AddBoundaryCrossing(...)100%10%
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;
 4// ReSharper disable PossibleMultipleEnumeration
 5
 6namespace Itinero.Network.Tiles.Standalone.Writer;
 7
 8/// <summary>
 9/// A writer class to write to a standalone tile.
 10/// </summary>
 11public class StandaloneNetworkTileWriter
 12{
 13    private readonly StandaloneNetworkTile _tile;
 14    private readonly int _zoom;
 15    private readonly (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) _turnCostTypeMap;
 16
 317    internal StandaloneNetworkTileWriter(StandaloneNetworkTile tile, int zoom,
 318        (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) edgeTypeMap,
 319        (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) turnCostTypeMap)
 320    {
 321        _tile = tile;
 322        this.EdgeTypeMap = edgeTypeMap;
 323        _turnCostTypeMap = turnCostTypeMap;
 324        _zoom = zoom;
 325    }
 26
 27    /// <summary>
 28    /// Gets the edge associated with the given id.
 29    /// </summary>
 30    /// <param name="edgeId">The edge id.</param>
 31    /// <param name="forward">The forward flag.</param>
 32    /// <returns>The edge details.</returns>
 33    public INetworkTileEdge GetEdge(EdgeId edgeId, bool forward)
 134    {
 135        var edge = new NetworkTileEnumerator();
 136        edge.MoveTo(_tile.NetworkTile);
 137        edge.MoveTo(edgeId, forward);
 138        return edge;
 139    }
 40
 41    /// <summary>
 42    /// Gets the edge type map.
 43    /// </summary>
 344    public (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) EdgeTypeMap { get; }
 45
 46    /// <summary>
 47    /// Returns true if the given coordinates are inside the tile boundaries.
 48    /// </summary>
 49    /// <param name="longitude">The longitude.</param>
 50    /// <param name="latitude">The latitude.</param>
 51    /// <returns>True if inside, false otherwise.</returns>
 52    public bool IsInTile(double longitude, double latitude)
 1353    {
 54        // get the local tile id.
 1355        var (x, y) = TileStatic.WorldToTile(longitude, latitude, _zoom);
 1356        return _tile.TileId == TileStatic.ToLocalId(x, y, _zoom);
 1357    }
 58
 59    /// <summary>
 60    /// Gets the tile id.
 61    /// </summary>
 162    public uint TileId => _tile.TileId;
 63
 64    /// <summary>
 65    /// Adds a new vertex.
 66    /// </summary>
 67    /// <param name="longitude">The longitude.</param>
 68    /// <param name="latitude">The latitude.</param>
 69    /// <param name="elevation">The elevation.</param>
 70    /// <returns>The new vertex id.</returns>
 71    /// <exception cref="ArgumentException"></exception>
 72    public VertexId AddVertex(double longitude, double latitude, float? elevation = null)
 673    {
 74        // check the local tile id.
 675        var (x, y) = TileStatic.WorldToTile(longitude, latitude, _zoom);
 676        var localTileId = TileStatic.ToLocalId(x, y, _zoom);
 677        if (_tile.TileId != localTileId) throw new ArgumentException("Coordinate are not inside the tile");
 78
 679        return _tile.NetworkTile.AddVertex(longitude, latitude, elevation);
 680    }
 81
 82    /// <summary>
 83    /// Adds a new edge.
 84    /// </summary>
 85    /// <param name="vertex1">The from vertex.</param>
 86    /// <param name="vertex2">The to vertex.</param>>
 87    /// <param name="edgeTypeId">The edge type id.</param>
 88    /// <param name="shape">The shape, if any.</param>
 89    /// <param name="attributes">The attributes, if any.</param>
 90    /// <returns>The edge id.</returns>
 91    /// <exception cref="ArgumentException"></exception>
 92    /// <exception cref="ArgumentOutOfRangeException"></exception>
 93    public EdgeId AddEdge(VertexId vertex1, VertexId vertex2, uint edgeTypeId,
 94        IEnumerable<(double longitude, double latitude, float? e)>? shape = null,
 95        IEnumerable<(string key, string value)>? attributes = null)
 396    {
 397        if (_tile.TileId != vertex1.TileId) throw new ArgumentException("Vertex not in tile");
 398        if (_tile.TileId != vertex2.TileId) throw new ArgumentException("Vertex not in tile");
 99
 100        // get the edge length in centimeters.
 3101        if (!_tile.NetworkTile.TryGetVertex(vertex1, out var longitude, out var latitude, out var e))
 0102        {
 0103            throw new ArgumentOutOfRangeException(nameof(vertex1), $"Vertex {vertex1} not found.");
 104        }
 105
 3106        var vertex1Location = (longitude, latitude, e);
 3107        if (!_tile.NetworkTile.TryGetVertex(vertex2, out longitude, out latitude, out e))
 0108        {
 0109            throw new ArgumentOutOfRangeException(nameof(vertex1), $"Vertex {vertex2} not found.");
 110        }
 111
 3112        var vertex2Location = (longitude, latitude, e);
 113
 3114        var length = (uint)(vertex1Location.DistanceEstimateInMeterShape(
 3115            vertex2Location, shape) * 100);
 116
 3117        return _tile.NetworkTile.AddEdge(vertex1, vertex2, shape, attributes, null, edgeTypeId, length);
 3118    }
 119
 120    public void AddGlobalIdFor(EdgeId edgeId, Guid globalEdgeId)
 2121    {
 2122        _tile.AddGlobalIdFor(edgeId, globalEdgeId);
 2123    }
 124
 125    public void AddGlobalIdFor(BoundaryEdgeId boundaryEdgeId, Guid globalEdgeId)
 0126    {
 0127        _tile.AddGlobalIdFor(boundaryEdgeId, globalEdgeId);
 0128    }
 129
 130
 131    /// <summary>
 132    /// Adds turn costs.
 133    /// </summary>
 134    /// <param name="vertex">The vertex where the costs are located.</param>
 135    /// <param name="attributes">The attributes representing the type of costs.</param>
 136    /// <param name="edges">The edges involved in the costs.</param>
 137    /// <param name="costs">The costs.</param>
 138    /// <param name="prefix">When the costs are only valid after first traversing a sequence of edges.</param>
 139    public void AddTurnCosts(VertexId vertex, IEnumerable<(string key, string value)> attributes,
 140        EdgeId[] edges, uint[,] costs, IEnumerable<EdgeId>? prefix = null)
 1141    {
 1142        prefix ??= ArraySegment<EdgeId>.Empty;
 143
 144        // get the turn cost type id.
 1145        var turnCostTypeId = _turnCostTypeMap.func(attributes);
 146
 147        // add the turn cost table using the type id.
 1148        _tile.NetworkTile.AddTurnCosts(vertex, turnCostTypeId, edges, costs, attributes, prefix);
 1149    }
 150
 151    /// <summary>
 152    /// Adds a new boundary crossing.
 153    /// </summary>
 154    /// <param name="from">The from node and vertex, inside the tile.</param>
 155    /// <param name="to">The to node.</param>
 156    /// <param name="edgeTypeId">The edge type id.</param>
 157    /// <param name="attributes">The attributes.</param>
 158    /// <param name="length">The length in centimeters.</param>
 159    public BoundaryEdgeId AddBoundaryCrossing((VertexId vertex, long node) from, long to,
 160        uint edgeTypeId, IEnumerable<(string key, string value)> attributes, uint length)
 1161    {
 1162        return _tile.AddBoundaryCrossing(false, from.node, to, from.vertex, attributes, edgeTypeId, length);
 1163    }
 164
 165    /// <summary>
 166    /// Adds a new boundary crossing.
 167    /// </summary>
 168    /// <param name="from">The from node.</param>
 169    /// <param name="to">The to node and vertex, inside the tile.</param>
 170    /// <param name="edgeTypeId">The edge type id.</param>
 171    /// <param name="attributes">The attributes.</param>
 172    /// <param name="length">The length in centimeters.</param>
 173    public BoundaryEdgeId AddBoundaryCrossing(long from, (VertexId vertex, long node) to,
 174        uint edgeTypeId, IEnumerable<(string key, string value)> attributes, uint length)
 0175    {
 0176        return _tile.AddBoundaryCrossing(true, from, to.node, to.vertex, attributes, edgeTypeId, length);
 0177    }
 178
 179    /// <summary>
 180    /// Gets the resulting tile.
 181    /// </summary>
 182    /// <returns></returns>
 183    public StandaloneNetworkTile GetResultingTile()
 6184    {
 6185        return _tile;
 6186    }
 187}