| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using Itinero.Network.Tiles; |
| | | 4 | | using Itinero.Network.Tiles.Standalone.Global; |
| | | 5 | | |
| | | 6 | | namespace Itinero.Network.Enumerators.Edges; |
| | | 7 | | |
| | | 8 | | public abstract class EdgeEnumerator<T> : |
| | | 9 | | IEdgeEnumerator<T> where T : IEdgeEnumerable |
| | | 10 | | { |
| | | 11 | | private readonly NetworkTileEnumerator _tileEnumerator; |
| | | 12 | | |
| | 933475 | 13 | | internal EdgeEnumerator(T graph) |
| | 933475 | 14 | | { |
| | 933475 | 15 | | this.Network = graph; |
| | | 16 | | |
| | 933475 | 17 | | _tileEnumerator = new NetworkTileEnumerator(); |
| | 933475 | 18 | | } |
| | | 19 | | |
| | | 20 | | private (double longitude, double latitude, float? e) GetVertex(VertexId vertex) |
| | 2611729 | 21 | | { |
| | 2611729 | 22 | | var tile = _tileEnumerator.Tile; |
| | 2611729 | 23 | | if (tile == null || tile.TileId != vertex.TileId) |
| | 32462 | 24 | | { |
| | 32462 | 25 | | tile = this.Network.GetTileForRead(vertex.TileId); |
| | 32462 | 26 | | } |
| | | 27 | | |
| | 2611729 | 28 | | if (tile == null) |
| | 0 | 29 | | { |
| | 0 | 30 | | throw new ArgumentOutOfRangeException(nameof(vertex), $"Vertex {vertex} not found!"); |
| | | 31 | | } |
| | | 32 | | |
| | 2611729 | 33 | | if (!tile.TryGetVertex(vertex, out var longitude, out var latitude, out var e)) |
| | 0 | 34 | | { |
| | 0 | 35 | | throw new ArgumentOutOfRangeException(nameof(vertex), $"Vertex {vertex} not found!"); |
| | | 36 | | } |
| | | 37 | | |
| | 2611729 | 38 | | return (longitude, latitude, e); |
| | 2611729 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Moves the enumerator to the first edge of the given vertex. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="vertex">The vertex.</param> |
| | | 45 | | /// <returns>True if the vertex exists.</returns> |
| | | 46 | | public bool MoveTo(VertexId vertex) |
| | 1140748 | 47 | | { |
| | 1140748 | 48 | | _tailLocation = null; |
| | 1140748 | 49 | | _headLocation = null; |
| | | 50 | | |
| | 1140748 | 51 | | if (_tileEnumerator.TileId == vertex.TileId) |
| | 1124555 | 52 | | { |
| | 1124555 | 53 | | return _tileEnumerator.MoveTo(vertex); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | // move to the tile. |
| | 16193 | 57 | | var tile = this.Network.GetTileForRead(vertex.TileId); |
| | 16193 | 58 | | if (tile == null) |
| | 1 | 59 | | { |
| | 1 | 60 | | return false; |
| | | 61 | | } |
| | | 62 | | |
| | 16192 | 63 | | _tileEnumerator.MoveTo(tile); |
| | | 64 | | |
| | 16192 | 65 | | return _tileEnumerator.MoveTo(vertex); |
| | 1140748 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Moves the enumerator to the given edge. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="edgeId">The edge id.</param> |
| | | 72 | | /// <param name="forward">The forward flag, when false the enumerator is in a state as it was enumerated to the edge |
| | | 73 | | public bool MoveTo(EdgeId edgeId, bool forward = true) |
| | 3263655 | 74 | | { |
| | 3263655 | 75 | | _tailLocation = null; |
| | 3263655 | 76 | | _headLocation = null; |
| | | 77 | | |
| | 3263655 | 78 | | if (_tileEnumerator.TileId == edgeId.TileId) |
| | 2313920 | 79 | | { |
| | 2313920 | 80 | | return _tileEnumerator.MoveTo(edgeId, forward); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | // move to the tile. |
| | 949735 | 84 | | var tile = this.Network.GetTileForRead(edgeId.TileId); |
| | 949735 | 85 | | if (tile == null) |
| | 0 | 86 | | { |
| | 0 | 87 | | return false; |
| | | 88 | | } |
| | | 89 | | |
| | 949735 | 90 | | _tileEnumerator.MoveTo(tile); |
| | | 91 | | |
| | 949735 | 92 | | return _tileEnumerator.MoveTo(edgeId, forward); |
| | 3263655 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Resets this enumerator. |
| | | 97 | | /// </summary> |
| | | 98 | | public void Reset() |
| | 0 | 99 | | { |
| | 0 | 100 | | _tailLocation = null; |
| | 0 | 101 | | _headLocation = null; |
| | | 102 | | |
| | 0 | 103 | | _tileEnumerator.Reset(); |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Moves this enumerator to the next edge. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <returns>True if there is data available.</returns> |
| | | 110 | | public bool MoveNext() |
| | 4408217 | 111 | | { |
| | 4408217 | 112 | | _tailLocation = null; |
| | 4408217 | 113 | | _headLocation = null; |
| | | 114 | | |
| | 4408217 | 115 | | return _tileEnumerator.MoveNext(); |
| | 4408217 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <inheritdoc/> |
| | 998390 | 119 | | public T Network { get; } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Returns true if the edge is from -> to, false otherwise. |
| | | 123 | | /// </summary> |
| | 2084821 | 124 | | public bool Forward => _tileEnumerator.Forward; |
| | | 125 | | |
| | | 126 | | private (double longitude, double latitude, float? e)? _tailLocation; |
| | | 127 | | |
| | | 128 | | public (double longitude, double latitude, float? e) TailLocation |
| | | 129 | | { |
| | | 130 | | get |
| | 1318057 | 131 | | { |
| | 1318057 | 132 | | _tailLocation ??= this.GetVertex(this.Tail); |
| | | 133 | | |
| | 1318057 | 134 | | return _tailLocation.Value; |
| | 1318057 | 135 | | } |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Gets the source vertex. |
| | | 140 | | /// </summary> |
| | 2400853 | 141 | | public VertexId Tail => _tileEnumerator.Tail; |
| | | 142 | | |
| | | 143 | | private (double longitude, double latitude, float? e)? _headLocation; |
| | | 144 | | |
| | | 145 | | public (double longitude, double latitude, float? e) HeadLocation |
| | | 146 | | { |
| | | 147 | | get |
| | 1293682 | 148 | | { |
| | 1293682 | 149 | | _headLocation ??= this.GetVertex(this.Head); |
| | | 150 | | |
| | 1293682 | 151 | | return _headLocation.Value; |
| | 1293682 | 152 | | } |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Gets the target vertex. |
| | | 157 | | /// </summary> |
| | 2732355 | 158 | | public VertexId Head => _tileEnumerator.Head; |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Gets the edge id. |
| | | 162 | | /// </summary> |
| | 4153727 | 163 | | public EdgeId EdgeId => _tileEnumerator.EdgeId; |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Gets the shape. |
| | | 167 | | /// </summary> |
| | | 168 | | /// <returns>The shape.</returns> |
| | 1295362 | 169 | | public IEnumerable<(double longitude, double latitude, float? e)> Shape => _tileEnumerator.Shape; |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Gets the attributes. |
| | | 173 | | /// </summary> |
| | | 174 | | /// <returns>The attributes.</returns> |
| | 3399 | 175 | | public IEnumerable<(string key, string value)> Attributes => _tileEnumerator.Attributes; |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Gets the edge profile id. |
| | | 179 | | /// </summary> |
| | 1071011 | 180 | | public uint? EdgeTypeId => _tileEnumerator.EdgeTypeId; |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Gets the length in centimeters, if any. |
| | | 184 | | /// </summary> |
| | 2558569 | 185 | | public uint? Length => _tileEnumerator.Length; |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Gets the global edge id, if any. |
| | | 189 | | /// </summary> |
| | 0 | 190 | | public GlobalEdgeId? GlobalEdgeId => _tileEnumerator.GlobalEdgeId; |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Gets the head index. |
| | | 194 | | /// </summary> |
| | 410336 | 195 | | public byte? HeadOrder => _tileEnumerator.HeadOrder; |
| | | 196 | | |
| | | 197 | | /// <summary> |
| | | 198 | | /// Gets the tail index. |
| | | 199 | | /// </summary> |
| | 2 | 200 | | public byte? TailOrder => _tileEnumerator.TailOrder; |
| | | 201 | | |
| | | 202 | | /// <summary> |
| | | 203 | | /// Gets the turn cost at the tail turn (source -> [tail -> head]). |
| | | 204 | | /// </summary> |
| | | 205 | | /// <param name="sourceOrder">The order of the source edge.</param> |
| | | 206 | | /// <returns>The turn costs if any.</returns> |
| | | 207 | | public IEnumerable<(uint turnCostType, IEnumerable<(string key, string value)> attributes, uint cost, IEnumerable<Ed |
| | | 208 | | byte sourceOrder) |
| | 2033 | 209 | | { |
| | 2033 | 210 | | return _tileEnumerator.GetTurnCostToTail(sourceOrder); |
| | 2033 | 211 | | } |
| | | 212 | | |
| | | 213 | | /// <summary> |
| | | 214 | | /// Gets the turn cost at the tail turn ([head -> tail] -> target). |
| | | 215 | | /// </summary> |
| | | 216 | | /// <param name="targetOrder">The order of the target edge.</param> |
| | | 217 | | /// <returns>The turn costs if any.</returns> |
| | | 218 | | public IEnumerable<(uint turnCostType, IEnumerable<(string key, string value)> attributes, uint cost, IEnumerable<Ed |
| | | 219 | | byte targetOrder) |
| | 8 | 220 | | { |
| | 8 | 221 | | return _tileEnumerator.GetTurnCostFromTail(targetOrder); |
| | 8 | 222 | | } |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// Gets the turn cost at the tail turn (source -> [head -> tail]). |
| | | 226 | | /// </summary> |
| | | 227 | | /// <param name="sourceOrder">The order of the source edge.</param> |
| | | 228 | | /// <returns>The turn costs if any.</returns> |
| | | 229 | | public IEnumerable<(uint turnCostType, IEnumerable<(string key, string value)> attributes, uint cost, IEnumerable<Ed |
| | | 230 | | byte sourceOrder) |
| | 0 | 231 | | { |
| | 0 | 232 | | return _tileEnumerator.GetTurnCostToHead(sourceOrder); |
| | 0 | 233 | | } |
| | | 234 | | |
| | | 235 | | /// <summary> |
| | | 236 | | /// Gets the turn cost at the tail turn ([tail -> head] -> target). |
| | | 237 | | /// </summary> |
| | | 238 | | /// <param name="targetOrder">The order of the target edge.</param> |
| | | 239 | | /// <returns>The turn costs if any.</returns> |
| | | 240 | | public IEnumerable<(uint turnCostType, IEnumerable<(string key, string value)> attributes, uint cost, IEnumerable<Ed |
| | | 241 | | byte targetOrder) |
| | 0 | 242 | | { |
| | 0 | 243 | | return _tileEnumerator.GetTurnCostFromHead(targetOrder); |
| | 0 | 244 | | } |
| | | 245 | | } |