< Summary

Class:Itinero.Network.Writer.RoutingNetworkWriter
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Network/Writer/RoutingNetworkWriter.cs
Covered lines:31
Uncovered lines:23
Coverable lines:54
Total lines:145
Line coverage:57.4% (31 of 54)
Covered branches:11
Total branches:20
Branch coverage:55% (11 of 20)
Tag:226_14867339564

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
GetEdgeEnumerator()100%10%
AddVertex(...)100%1100%
AddEdge(...)78.57%1481.81%
AddTurnCosts(...)0%60%
AddTile(...)100%10%
HasTile(...)100%10%
Dispose()100%1100%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Network/Writer/RoutingNetworkWriter.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using Itinero.Geo;
 4using Itinero.Network.Enumerators.Edges;
 5using Itinero.Network.Tiles;
 6// ReSharper disable PossibleMultipleEnumeration
 7
 8namespace Itinero.Network.Writer;
 9
 10/// <summary>
 11/// A writer to write to a network. This writer will never change existing data, only add new data.
 12///
 13/// This writer can:
 14/// - add new vertices
 15/// - add new edges.
 16///
 17/// This writer cannot mutate existing data, only add new.
 18/// </summary>
 19public class RoutingNetworkWriter : IDisposable
 20{
 21    private readonly IRoutingNetworkWritable _network;
 22
 2923    internal RoutingNetworkWriter(IRoutingNetworkWritable network)
 2924    {
 2925        _network = network;
 2926    }
 27
 28    /// <summary>
 29    /// Gets an edge enumerator.
 30    /// </summary>
 31    /// <returns>The enumerator.</returns>
 32    internal RoutingNetworkEdgeEnumerator GetEdgeEnumerator()
 033    {
 034        return _network.GetEdgeEnumerator();
 035    }
 36
 37    /// <summary>
 38    /// Adds a new vertex.
 39    /// </summary>
 40    /// <param name="longitude">The longitude.</param>
 41    /// <param name="latitude">The latitude.</param>
 42    /// <param name="elevation">The elevation.</param>
 43    /// <returns>The vertex id.</returns>
 44    public VertexId AddVertex(double longitude, double latitude, float? elevation = null)
 5345    {
 46        // get the local tile id.
 5347        var (x, y) = TileStatic.WorldToTile(longitude, latitude, _network.Zoom);
 5348        var localTileId = TileStatic.ToLocalId(x, y, _network.Zoom);
 49
 50        // get the tile (or create it).
 5351        var (tile, _) = _network.GetTileForWrite(localTileId);
 52
 5353        return tile.AddVertex(longitude, latitude, elevation);
 5354    }
 55
 56    /// <summary>
 57    /// Adds a new edge.
 58    /// </summary>
 59    /// <param name="tail">The tail vertex.</param>
 60    /// <param name="head">The head vertex.</param>
 61    /// <param name="shape">The shape, if any.</param>
 62    /// <param name="attributes">The attributes, if any.</param>
 63    /// <param name="edgeTypeId">The edge type id, if any.</param>
 64    /// <param name="length">The length, if any.</param>
 65    /// <returns></returns>
 66    /// <exception cref="ArgumentException"></exception>
 67    /// <exception cref="ArgumentOutOfRangeException"></exception>
 68    public EdgeId AddEdge(VertexId tail, VertexId head,
 69        IEnumerable<(double longitude, double latitude, float? e)>? shape = null,
 70        IEnumerable<(string key, string value)>? attributes = null, uint? edgeTypeId = null,
 71        uint? length = null)
 2672    {
 73        // get the tile (or create it).
 2674        var (tile, edgeTypeMap) = _network.GetTileForWrite(tail.TileId);
 2675        if (tile == null) throw new ArgumentException($"Cannot add edge with a vertex that doesn't exist.");
 76
 77        // get the edge type id.
 2678        edgeTypeId ??= attributes != null ? edgeTypeMap(attributes) : null;
 79
 80        // get the edge length in centimeters.
 2681        if (!_network.TryGetVertex(tail, out var longitude, out var latitude, out var e))
 082        {
 083            throw new ArgumentOutOfRangeException(nameof(tail), $"Vertex {tail} not found.");
 84        }
 85
 2686        var vertex1Location = (longitude, latitude, e);
 2687        if (!_network.TryGetVertex(head, out longitude, out latitude, out e))
 088        {
 089            throw new ArgumentOutOfRangeException(nameof(tail), $"Vertex {head} not found.");
 90        }
 91
 2692        var vertex2Location = (longitude, latitude, e);
 93
 2694        length ??= (uint)(vertex1Location.DistanceEstimateInMeterShape(
 2695            vertex2Location, shape) * 100);
 96
 2697        var edge1 = tile.AddEdge(tail, head, shape, attributes, null, edgeTypeId, length);
 2698        if (tail.TileId == head.TileId)
 2399        {
 23100            return edge1;
 101        }
 102
 103        // this edge crosses tiles, also add an extra edge to the other tile.
 3104        (tile, _) = _network.GetTileForWrite(head.TileId);
 3105        tile.AddEdge(tail, head, shape, attributes, edge1, edgeTypeId, length);
 106
 3107        return edge1;
 26108    }
 109
 110    public void AddTurnCosts(VertexId vertex, IEnumerable<(string key, string value)> attributes,
 111        EdgeId[] edges, uint[,] costs, IEnumerable<EdgeId>? prefix, uint? turnCostType = null)
 0112    {
 0113        prefix ??= ArraySegment<EdgeId>.Empty;
 114
 115        // get the tile (or create it).
 0116        var (tile, _) = _network.GetTileForWrite(vertex.TileId);
 0117        if (tile == null)
 0118        {
 0119            throw new ArgumentException($"Cannot add turn costs to a vertex that doesn't exist.");
 120        }
 121
 122        // get the turn cost type id.
 0123        var turnCostMap = _network.RouterDb.GetTurnCostTypeMap();
 0124        turnCostType ??= turnCostMap.func(attributes);
 125
 126        // add the turn cost table using the type id.
 0127        tile.AddTurnCosts(vertex, turnCostType.Value, edges, costs, attributes, prefix);
 0128    }
 129
 130    internal void AddTile(NetworkTile tile)
 0131    {
 0132        _network.SetTile(tile);
 0133    }
 134
 135    internal bool HasTile(uint localTileId)
 0136    {
 0137        return _network.HasTile(localTileId);
 0138    }
 139
 140    /// <inheritdoc/>
 141    public void Dispose()
 29142    {
 29143        _network.ClearWriter();
 29144    }
 145}