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