| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using Itinero.Data.Usage; |
| | 4 | | using Itinero.Network.DataStructures; |
| | 5 | | using Itinero.Network.Enumerators.Edges; |
| | 6 | | using Itinero.Network.Enumerators.Vertices; |
| | 7 | | using Itinero.Network.Mutation; |
| | 8 | | using Itinero.Network.Search.Islands; |
| | 9 | | using Itinero.Network.Tiles; |
| | 10 | | using Itinero.Network.Writer; |
| | 11 | |
|
| | 12 | | namespace Itinero.Network; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// The routing network. |
| | 16 | | /// </summary> |
| | 17 | | public sealed partial class RoutingNetwork : IEdgeEnumerable, IRoutingNetworkMutable, IRoutingNetworkWritable |
| | 18 | | { |
| | 19 | | private readonly SparseArray<NetworkTile?> _tiles; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Creates a new routing network. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="routerDb"></param> |
| | 25 | | /// <param name="zoom"></param> |
| | 26 | | /// <param name="maxIslandSize"></param> |
| 173 | 27 | | public RoutingNetwork(RouterDb routerDb, int zoom = 14, int maxIslandSize = 1024) |
| 173 | 28 | | { |
| 173 | 29 | | this.Zoom = zoom; |
| 173 | 30 | | this.RouterDb = routerDb; |
| | 31 | |
|
| 173 | 32 | | IslandManager = new RoutingNetworkIslandManager(maxIslandSize); |
| 173 | 33 | | _tiles = new SparseArray<NetworkTile?>(0); |
| 173 | 34 | | } |
| | 35 | |
|
| 123 | 36 | | internal RoutingNetwork(RouterDb routerDb, SparseArray<NetworkTile?> tiles, int zoom, RoutingNetworkIslandManager is |
| 123 | 37 | | { |
| 123 | 38 | | this.Zoom = zoom; |
| 123 | 39 | | this.RouterDb = routerDb; |
| 123 | 40 | | IslandManager = islandManager; |
| 123 | 41 | | _tiles = tiles; |
| 123 | 42 | | } |
| | 43 | |
|
| | 44 | | internal NetworkTile? GetTileForRead(uint localTileId) |
| 730313 | 45 | | { |
| 1113420 | 46 | | if (_tiles.Length <= localTileId) return null; |
| | 47 | |
|
| | 48 | | // get tile, if any. |
| 347206 | 49 | | var tile = _tiles[localTileId]; |
| 693902 | 50 | | if (tile == null) return null; |
| | 51 | |
|
| | 52 | | // check edge type map. |
| 510 | 53 | | var edgeTypeMap = this.RouterDb.GetEdgeTypeMap(); |
| 1019 | 54 | | if (tile.EdgeTypeMapId == edgeTypeMap.id) return tile; |
| | 55 | |
|
| | 56 | | // tile.EdgeTypeMapId indicates the version of the used edgeTypeMap |
| | 57 | | // If the id is different, the loaded tile needs updating; e.g. because a cost function has been changed |
| 1 | 58 | | tile = tile.CloneForEdgeTypeMap(edgeTypeMap); |
| 1 | 59 | | _tiles[localTileId] = tile; |
| | 60 | |
|
| 1 | 61 | | return tile; |
| 730313 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Gets the usage notifier. |
| | 66 | | /// </summary> |
| 503 | 67 | | public DataUseNotifier UsageNotifier { get; } = new(); |
| | 68 | |
|
| | 69 | | internal IEnumerator<uint> GetTileEnumerator() |
| 15 | 70 | | { |
| 15 | 71 | | using var enumerator = _tiles.GetEnumerator(); |
| 729802 | 72 | | while (enumerator.MoveNext()) |
| 729790 | 73 | | { |
| 729790 | 74 | | yield return (uint)enumerator.Current.i; |
| 729787 | 75 | | } |
| 12 | 76 | | } |
| | 77 | |
|
| | 78 | | NetworkTile? IEdgeEnumerable.GetTileForRead(uint localTileId) |
| 348 | 79 | | { |
| 348 | 80 | | return this.GetTileForRead(localTileId); |
| 348 | 81 | | } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Gets the zoom. |
| | 85 | | /// </summary> |
| 1068 | 86 | | public int Zoom { get; } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Gets the routing network. |
| | 90 | | /// </summary> |
| 1554 | 91 | | public RouterDb RouterDb { get; } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Tries to get the given vertex. |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="vertex">The vertex.</param> |
| | 97 | | /// <param name="longitude">The longitude.</param> |
| | 98 | | /// <param name="latitude">The latitude.</param> |
| | 99 | | /// <param name="elevation">The elevation.</param> |
| | 100 | | /// <returns>The vertex.</returns> |
| | 101 | | public bool TryGetVertex(VertexId vertex, out double longitude, out double latitude, out float? elevation) |
| 175 | 102 | | { |
| 175 | 103 | | var localTileId = vertex.TileId; |
| | 104 | |
|
| | 105 | | // get tile. |
| 175 | 106 | | var tile = this.GetTileForRead(localTileId); |
| 324 | 107 | | if (tile != null) return tile.TryGetVertex(vertex, out longitude, out latitude, out elevation); |
| | 108 | |
|
| | 109 | | // no tile, no vertex. |
| 26 | 110 | | longitude = default; |
| 26 | 111 | | latitude = default; |
| 26 | 112 | | elevation = null; |
| 26 | 113 | | return false; |
| 175 | 114 | | } |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Gets an edge enumerator. |
| | 118 | | /// </summary> |
| | 119 | | /// <returns>The enumerator.</returns> |
| | 120 | | public RoutingNetworkEdgeEnumerator GetEdgeEnumerator() |
| 365 | 121 | | { |
| 365 | 122 | | return new RoutingNetworkEdgeEnumerator(this); |
| 365 | 123 | | } |
| | 124 | |
|
| | 125 | | RoutingNetworkEdgeEnumerator IRoutingNetworkWritable.GetEdgeEnumerator() |
| 0 | 126 | | { |
| 0 | 127 | | return this.GetEdgeEnumerator(); |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | /// <summary> |
| | 131 | | /// Gets a vertex enumerator. |
| | 132 | | /// </summary> |
| | 133 | | /// <returns>The enumerator.</returns> |
| | 134 | | internal RoutingNetworkVertexEnumerator GetVertexEnumerator() |
| 15 | 135 | | { |
| 15 | 136 | | return new(this); |
| 15 | 137 | | } |
| | 138 | |
|
| | 139 | | /// <summary> |
| | 140 | | /// Returns true if this network has the given tile loaded. |
| | 141 | | /// </summary> |
| | 142 | | /// <param name="localTileId"></param> |
| | 143 | | /// <returns></returns> |
| | 144 | | public bool HasTile(uint localTileId) |
| 0 | 145 | | { |
| 0 | 146 | | if (localTileId >= _tiles.Length) return false; |
| | 147 | |
|
| 0 | 148 | | return _tiles[localTileId] != null; |
| 0 | 149 | | } |
| | 150 | | } |