| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using Itinero.Geo; |
| | 4 | | using Itinero.Network.Enumerators.Edges; |
| | 5 | | using Itinero.Network.Tiles; |
| | 6 | | // ReSharper disable PossibleMultipleEnumeration |
| | 7 | |
|
| | 8 | | namespace 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> |
| | 19 | | public class RoutingNetworkWriter : IDisposable |
| | 20 | | { |
| | 21 | | private readonly IRoutingNetworkWritable _network; |
| | 22 | |
|
| 29 | 23 | | internal RoutingNetworkWriter(IRoutingNetworkWritable network) |
| 29 | 24 | | { |
| 29 | 25 | | _network = network; |
| 29 | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Gets an edge enumerator. |
| | 30 | | /// </summary> |
| | 31 | | /// <returns>The enumerator.</returns> |
| | 32 | | internal RoutingNetworkEdgeEnumerator GetEdgeEnumerator() |
| 0 | 33 | | { |
| 0 | 34 | | return _network.GetEdgeEnumerator(); |
| 0 | 35 | | } |
| | 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) |
| 53 | 45 | | { |
| | 46 | | // get the local tile id. |
| 53 | 47 | | var (x, y) = TileStatic.WorldToTile(longitude, latitude, _network.Zoom); |
| 53 | 48 | | var localTileId = TileStatic.ToLocalId(x, y, _network.Zoom); |
| | 49 | |
|
| | 50 | | // get the tile (or create it). |
| 53 | 51 | | var (tile, _) = _network.GetTileForWrite(localTileId); |
| | 52 | |
|
| 53 | 53 | | return tile.AddVertex(longitude, latitude, elevation); |
| 53 | 54 | | } |
| | 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) |
| 26 | 72 | | { |
| | 73 | | // get the tile (or create it). |
| 26 | 74 | | var (tile, edgeTypeMap) = _network.GetTileForWrite(tail.TileId); |
| 26 | 75 | | if (tile == null) throw new ArgumentException($"Cannot add edge with a vertex that doesn't exist."); |
| | 76 | |
|
| | 77 | | // get the edge type id. |
| 26 | 78 | | edgeTypeId ??= attributes != null ? edgeTypeMap(attributes) : null; |
| | 79 | |
|
| | 80 | | // get the edge length in centimeters. |
| 26 | 81 | | if (!_network.TryGetVertex(tail, out var longitude, out var latitude, out var e)) |
| 0 | 82 | | { |
| 0 | 83 | | throw new ArgumentOutOfRangeException(nameof(tail), $"Vertex {tail} not found."); |
| | 84 | | } |
| | 85 | |
|
| 26 | 86 | | var vertex1Location = (longitude, latitude, e); |
| 26 | 87 | | if (!_network.TryGetVertex(head, out longitude, out latitude, out e)) |
| 0 | 88 | | { |
| 0 | 89 | | throw new ArgumentOutOfRangeException(nameof(tail), $"Vertex {head} not found."); |
| | 90 | | } |
| | 91 | |
|
| 26 | 92 | | var vertex2Location = (longitude, latitude, e); |
| | 93 | |
|
| 26 | 94 | | length ??= (uint)(vertex1Location.DistanceEstimateInMeterShape( |
| 26 | 95 | | vertex2Location, shape) * 100); |
| | 96 | |
|
| 26 | 97 | | var edge1 = tile.AddEdge(tail, head, shape, attributes, null, edgeTypeId, length); |
| 26 | 98 | | if (tail.TileId == head.TileId) |
| 23 | 99 | | { |
| 23 | 100 | | return edge1; |
| | 101 | | } |
| | 102 | |
|
| | 103 | | // this edge crosses tiles, also add an extra edge to the other tile. |
| 3 | 104 | | (tile, _) = _network.GetTileForWrite(head.TileId); |
| 3 | 105 | | tile.AddEdge(tail, head, shape, attributes, edge1, edgeTypeId, length); |
| | 106 | |
|
| 3 | 107 | | return edge1; |
| 26 | 108 | | } |
| | 109 | |
|
| | 110 | | public void AddTurnCosts(VertexId vertex, IEnumerable<(string key, string value)> attributes, |
| | 111 | | EdgeId[] edges, uint[,] costs, IEnumerable<EdgeId>? prefix, uint? turnCostType = null) |
| 0 | 112 | | { |
| 0 | 113 | | prefix ??= ArraySegment<EdgeId>.Empty; |
| | 114 | |
|
| | 115 | | // get the tile (or create it). |
| 0 | 116 | | var (tile, _) = _network.GetTileForWrite(vertex.TileId); |
| 0 | 117 | | if (tile == null) |
| 0 | 118 | | { |
| 0 | 119 | | throw new ArgumentException($"Cannot add turn costs to a vertex that doesn't exist."); |
| | 120 | | } |
| | 121 | |
|
| | 122 | | // get the turn cost type id. |
| 0 | 123 | | var turnCostMap = _network.RouterDb.GetTurnCostTypeMap(); |
| 0 | 124 | | turnCostType ??= turnCostMap.func(attributes); |
| | 125 | |
|
| | 126 | | // add the turn cost table using the type id. |
| 0 | 127 | | tile.AddTurnCosts(vertex, turnCostType.Value, edges, costs, attributes, prefix); |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | internal void AddTile(NetworkTile tile) |
| 0 | 131 | | { |
| 0 | 132 | | _network.SetTile(tile); |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | internal bool HasTile(uint localTileId) |
| 0 | 136 | | { |
| 0 | 137 | | return _network.HasTile(localTileId); |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | /// <inheritdoc/> |
| | 141 | | public void Dispose() |
| 29 | 142 | | { |
| 29 | 143 | | _network.ClearWriter(); |
| 29 | 144 | | } |
| | 145 | | } |