| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using Itinero.Geo; |
| | | 4 | | using Itinero.Network.Tiles.Standalone.Global; |
| | | 5 | | |
| | | 6 | | // ReSharper disable PossibleMultipleEnumeration |
| | | 7 | | |
| | | 8 | | namespace Itinero.Network.Tiles.Standalone.Writer; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// A writer class to write to a standalone tile. |
| | | 12 | | /// </summary> |
| | | 13 | | public class StandaloneNetworkTileWriter |
| | | 14 | | { |
| | | 15 | | private readonly StandaloneNetworkTile _tile; |
| | | 16 | | private readonly int _zoom; |
| | | 17 | | private readonly (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) _turnCostTypeMap; |
| | | 18 | | |
| | 0 | 19 | | internal StandaloneNetworkTileWriter(StandaloneNetworkTile tile, int zoom, |
| | 0 | 20 | | (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) edgeTypeMap, |
| | 0 | 21 | | (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) turnCostTypeMap) |
| | 0 | 22 | | { |
| | 0 | 23 | | _tile = tile; |
| | 0 | 24 | | this.EdgeTypeMap = edgeTypeMap; |
| | 0 | 25 | | _turnCostTypeMap = turnCostTypeMap; |
| | 0 | 26 | | _zoom = zoom; |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets the edge associated with the given id. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="edgeId">The edge id.</param> |
| | | 33 | | /// <param name="forward">The forward flag.</param> |
| | | 34 | | /// <returns>The edge details.</returns> |
| | | 35 | | public INetworkTileEdge GetEdge(EdgeId edgeId, bool forward) |
| | 0 | 36 | | { |
| | 0 | 37 | | var edge = new NetworkTileEnumerator(); |
| | 0 | 38 | | edge.MoveTo(_tile.NetworkTile); |
| | 0 | 39 | | edge.MoveTo(edgeId, forward); |
| | 0 | 40 | | return edge; |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets the edge type map. |
| | | 45 | | /// </summary> |
| | 0 | 46 | | public (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) EdgeTypeMap { get; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Returns true if the given coordinates are inside the tile boundaries. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="longitude">The longitude.</param> |
| | | 52 | | /// <param name="latitude">The latitude.</param> |
| | | 53 | | /// <returns>True if inside, false otherwise.</returns> |
| | | 54 | | public bool IsInTile(double longitude, double latitude) |
| | 0 | 55 | | { |
| | | 56 | | // get the local tile id. |
| | 0 | 57 | | var (x, y) = TileStatic.WorldToTile(longitude, latitude, _zoom); |
| | 0 | 58 | | return _tile.TileId == TileStatic.ToLocalId(x, y, _zoom); |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets the tile id. |
| | | 63 | | /// </summary> |
| | 0 | 64 | | public uint TileId => _tile.TileId; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Adds a new vertex. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="longitude">The longitude.</param> |
| | | 70 | | /// <param name="latitude">The latitude.</param> |
| | | 71 | | /// <param name="elevation">The elevation.</param> |
| | | 72 | | /// <returns>The new vertex id.</returns> |
| | | 73 | | /// <exception cref="ArgumentException"></exception> |
| | | 74 | | public VertexId AddVertex(double longitude, double latitude, float? elevation = null) |
| | 0 | 75 | | { |
| | | 76 | | // check the local tile id. |
| | 0 | 77 | | var (x, y) = TileStatic.WorldToTile(longitude, latitude, _zoom); |
| | 0 | 78 | | var localTileId = TileStatic.ToLocalId(x, y, _zoom); |
| | 0 | 79 | | if (_tile.TileId != localTileId) throw new ArgumentException("Coordinate are not inside the tile"); |
| | | 80 | | |
| | 0 | 81 | | return _tile.NetworkTile.AddVertex(longitude, latitude, elevation); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Adds a new edge. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="vertex1">The from vertex.</param> |
| | | 88 | | /// <param name="vertex2">The to vertex.</param>> |
| | | 89 | | /// <param name="edgeTypeId">The edge type id.</param> |
| | | 90 | | /// <param name="shape">The shape, if any.</param> |
| | | 91 | | /// <param name="attributes">The attributes, if any.</param> |
| | | 92 | | /// <param name="globalEdgeId">The global edge id, if any.</param> |
| | | 93 | | /// <returns>The edge id.</returns> |
| | | 94 | | /// <exception cref="ArgumentException"></exception> |
| | | 95 | | /// <exception cref="ArgumentOutOfRangeException"></exception> |
| | | 96 | | public EdgeId AddEdge(VertexId vertex1, VertexId vertex2, uint edgeTypeId, |
| | | 97 | | IEnumerable<(double longitude, double latitude, float? e)>? shape = null, |
| | | 98 | | IEnumerable<(string key, string value)>? attributes = null, |
| | | 99 | | GlobalEdgeId? globalEdgeId = null) |
| | 0 | 100 | | { |
| | 0 | 101 | | if (_tile.TileId != vertex1.TileId) throw new ArgumentException("Vertex not in tile"); |
| | 0 | 102 | | if (_tile.TileId != vertex2.TileId) throw new ArgumentException("Vertex not in tile"); |
| | | 103 | | |
| | | 104 | | // get the edge length in centimeters. |
| | 0 | 105 | | if (!_tile.NetworkTile.TryGetVertex(vertex1, out var longitude, out var latitude, out var e)) |
| | 0 | 106 | | { |
| | 0 | 107 | | throw new ArgumentOutOfRangeException(nameof(vertex1), $"Vertex {vertex1} not found."); |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | var vertex1Location = (longitude, latitude, e); |
| | 0 | 111 | | if (!_tile.NetworkTile.TryGetVertex(vertex2, out longitude, out latitude, out e)) |
| | 0 | 112 | | { |
| | 0 | 113 | | throw new ArgumentOutOfRangeException(nameof(vertex1), $"Vertex {vertex2} not found."); |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | var vertex2Location = (longitude, latitude, e); |
| | | 117 | | |
| | 0 | 118 | | var length = (uint)(vertex1Location.DistanceEstimateInMeterShape( |
| | 0 | 119 | | vertex2Location, shape) * 100); |
| | | 120 | | |
| | 0 | 121 | | return _tile.NetworkTile.AddEdge(vertex1, vertex2, shape, attributes, null, edgeTypeId, length, globalEdgeId); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Adds a global restriction for processing when the tile is loaded. |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="sequence">The sequence of global edge ids with local edge ids if they are already known.</param> |
| | | 128 | | /// <param name="isProhibitory">The type of restriction.</param> |
| | | 129 | | /// <param name="attributes">The raw attributes of the restriction.</param> |
| | | 130 | | public void AddGlobalRestriction(IEnumerable<(GlobalEdgeId globalEdgeId, EdgeId? edge)> sequence, |
| | | 131 | | bool isProhibitory, IEnumerable<(string key, string value)> attributes) |
| | 0 | 132 | | { |
| | | 133 | | // get the turn cost type id. |
| | 0 | 134 | | var turnCostTypeId = _turnCostTypeMap.func(attributes); |
| | | 135 | | |
| | | 136 | | // write to tile. |
| | 0 | 137 | | _tile.AddGlobalRestriction(sequence, isProhibitory, turnCostTypeId, attributes); |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Adds turn costs. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <param name="vertex">The vertex where the costs are located.</param> |
| | | 144 | | /// <param name="attributes">The attributes representing the type of costs.</param> |
| | | 145 | | /// <param name="edges">The edges involved in the costs.</param> |
| | | 146 | | /// <param name="costs">The costs.</param> |
| | | 147 | | /// <param name="prefix">When the costs are only valid after first traversing a sequence of edges.</param> |
| | | 148 | | public void AddTurnCosts(VertexId vertex, IEnumerable<(string key, string value)> attributes, |
| | | 149 | | EdgeId[] edges, uint[,] costs, IEnumerable<EdgeId>? prefix = null) |
| | 0 | 150 | | { |
| | 0 | 151 | | prefix ??= ArraySegment<EdgeId>.Empty; |
| | | 152 | | |
| | | 153 | | // get the turn cost type id. |
| | 0 | 154 | | var turnCostTypeId = _turnCostTypeMap.func(attributes); |
| | | 155 | | |
| | | 156 | | // add the turn cost table using the type id. |
| | 0 | 157 | | _tile.NetworkTile.AddTurnCosts(vertex, turnCostTypeId, edges, costs, attributes, prefix); |
| | 0 | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Adds a new outgoing boundary crossing. |
| | | 162 | | /// </summary> |
| | | 163 | | /// <param name="globalEdgeId">The global edge id, a stable identified to match boundary edges.</param> |
| | | 164 | | /// <param name="tail">The tail vertex.</param> |
| | | 165 | | /// <param name="edgeTypeId">The edge type id.</param> |
| | | 166 | | /// <param name="attributes">The attributes.</param> |
| | | 167 | | public void AddOutgoingBoundaryCrossing(GlobalEdgeId globalEdgeId, VertexId tail, |
| | | 168 | | uint edgeTypeId, IEnumerable<(string key, string value)> attributes) |
| | 0 | 169 | | { |
| | 0 | 170 | | _tile.AddBoundaryCrossing(false, globalEdgeId, tail, attributes, edgeTypeId); |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Adds a new incoming boundary crossing. |
| | | 175 | | /// </summary> |
| | | 176 | | /// <param name="globalEdgeId">The global edge id, a stable identified to match boundary edges.</param> |
| | | 177 | | /// <param name="tail">The head vertex.</param> |
| | | 178 | | /// <param name="edgeTypeId">The edge type id.</param> |
| | | 179 | | /// <param name="attributes">The attributes.</param> |
| | | 180 | | public void AddIncomingBoundaryCrossing(GlobalEdgeId globalEdgeId, VertexId tail, |
| | | 181 | | uint edgeTypeId, IEnumerable<(string key, string value)> attributes) |
| | 0 | 182 | | { |
| | 0 | 183 | | _tile.AddBoundaryCrossing(true, globalEdgeId, tail, attributes, edgeTypeId); |
| | 0 | 184 | | } |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Gets the resulting tile. |
| | | 188 | | /// </summary> |
| | | 189 | | /// <returns></returns> |
| | | 190 | | public StandaloneNetworkTile GetResultingTile() |
| | 0 | 191 | | { |
| | 0 | 192 | | return _tile; |
| | 0 | 193 | | } |
| | | 194 | | } |