| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using Itinero.IO; |
| | | 5 | | using Itinero.Network.Storage; |
| | | 6 | | using Itinero.Network.Tiles.Standalone.Global; |
| | | 7 | | using Itinero.Network.TurnCosts; |
| | | 8 | | |
| | | 9 | | namespace Itinero.Network.Tiles; |
| | | 10 | | |
| | | 11 | | internal partial class NetworkTile |
| | | 12 | | { |
| | | 13 | | private const int DefaultSizeIncrease = 16; |
| | | 14 | | |
| | | 15 | | private readonly uint _tileId; |
| | | 16 | | private readonly int _zoom; // the zoom level. |
| | | 17 | | private readonly Guid _edgeTypeMapId; // the edge type index id. |
| | | 18 | | |
| | 502 | 19 | | private uint _nextVertexId = 0; // the next vertex id. |
| | | 20 | | |
| | | 21 | | // the pointers, per vertex, to their first edge. |
| | | 22 | | // TODO: investigate if it's worth storing these with less precision, one tile will never contain this much data. |
| | | 23 | | // TODO: investigate if we can not use one-increasing vertex ids but also use their pointers like with the edges. |
| | | 24 | | private uint[] _pointers; |
| | | 25 | | private uint _nextCrossTileId; // the next id for an edge that crosses tile boundaries. |
| | | 26 | | private uint[] _crossEdgePointers; // points to the cross tile boundary edges. |
| | | 27 | | |
| | | 28 | | // the next edge id. |
| | 502 | 29 | | private uint _nextEdgeId = 0; |
| | | 30 | | |
| | | 31 | | // the edges. |
| | | 32 | | private byte[] _edges; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Creates a new tile. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="zoom">The zoom level.</param> |
| | | 38 | | /// <param name="tileId">The tile id.</param> |
| | | 39 | | /// <param name="edgeTypeMapId">The edge type index id.</param> |
| | 486 | 40 | | public NetworkTile(int zoom, uint tileId, Guid? edgeTypeMapId = null) |
| | 486 | 41 | | { |
| | 486 | 42 | | _zoom = zoom; |
| | 486 | 43 | | _tileId = tileId; |
| | 486 | 44 | | _edgeTypeMapId = edgeTypeMapId ?? Guid.Empty; |
| | 486 | 45 | | _nextCrossTileId = 0; |
| | | 46 | | |
| | 486 | 47 | | _pointers = new uint[0]; |
| | 486 | 48 | | _edges = new byte[0]; |
| | 486 | 49 | | _crossEdgePointers = new uint[0]; |
| | | 50 | | |
| | 486 | 51 | | _coordinates = new byte[0]; |
| | 486 | 52 | | _shapes = new byte[0]; |
| | 486 | 53 | | _attributes = new byte[0]; |
| | 486 | 54 | | _strings = new string[0]; |
| | 486 | 55 | | } |
| | | 56 | | |
| | 16 | 57 | | private NetworkTile(int zoom, uint tileId, Guid edgeTypeMapId, uint nextCrossTileId, uint[] pointers, |
| | 16 | 58 | | byte[] edges, |
| | 16 | 59 | | uint[] crossEdgePointers, byte[] coordinates, byte[] shapes, |
| | 16 | 60 | | byte[] attributes, |
| | 16 | 61 | | string[] strings, byte[] turnCosts, uint nextVertexId, uint nextEdgeId, |
| | 16 | 62 | | uint nextAttributePointer, |
| | 16 | 63 | | uint nextShapePointer, uint nextStringId) |
| | 16 | 64 | | { |
| | 16 | 65 | | _zoom = zoom; |
| | 16 | 66 | | _tileId = tileId; |
| | 16 | 67 | | _edgeTypeMapId = edgeTypeMapId; |
| | 16 | 68 | | _nextCrossTileId = nextCrossTileId; |
| | 16 | 69 | | _pointers = pointers; |
| | 16 | 70 | | _edges = edges; |
| | 16 | 71 | | _crossEdgePointers = crossEdgePointers; |
| | | 72 | | |
| | 16 | 73 | | _coordinates = coordinates; |
| | 16 | 74 | | _shapes = shapes; |
| | 16 | 75 | | _attributes = attributes; |
| | 16 | 76 | | _strings = strings; |
| | 16 | 77 | | _turnCosts = turnCosts; |
| | | 78 | | |
| | 16 | 79 | | _nextVertexId = nextVertexId; |
| | 16 | 80 | | _nextEdgeId = nextEdgeId; |
| | 16 | 81 | | _nextAttributePointer = nextAttributePointer; |
| | 16 | 82 | | _nextShapePointer = nextShapePointer; |
| | 16 | 83 | | _nextStringId = nextStringId; |
| | 16 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Clones this graph tile. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <returns>The copy of this tile.</returns> |
| | | 90 | | public NetworkTile Clone() |
| | 10 | 91 | | { |
| | 10 | 92 | | return new(_zoom, _tileId, _edgeTypeMapId, _nextCrossTileId, (uint[])_pointers.Clone(), (byte[])_edges.Clone(), |
| | 10 | 93 | | (uint[])_crossEdgePointers.Clone(), |
| | 10 | 94 | | (byte[])_coordinates.Clone(), (byte[])_shapes.Clone(), (byte[])_attributes.Clone(), (string[])_strings.Clone |
| | 10 | 95 | | _nextVertexId, |
| | 10 | 96 | | _nextEdgeId, _nextAttributePointer, _nextShapePointer, _nextStringId); |
| | 10 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Gets the tile id. |
| | | 101 | | /// </summary> |
| | 15906891 | 102 | | public uint TileId => _tileId; |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Gets the number of vertices. |
| | | 106 | | /// </summary> |
| | 1142293 | 107 | | public uint VertexCount => _nextVertexId; |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Gets the edge type map id. |
| | | 111 | | /// </summary> |
| | 4300623 | 112 | | public Guid EdgeTypeMapId => _edgeTypeMapId; |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Adds a new vertex and returns its id. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <param name="longitude">The longitude.</param> |
| | | 118 | | /// <param name="latitude">The latitude.</param> |
| | | 119 | | /// <param name="e">The elevation in meters.</param> |
| | | 120 | | /// <returns>The ID of the new vertex.</returns> |
| | | 121 | | public VertexId AddVertex(double longitude, double latitude, float? e = null) |
| | 36922 | 122 | | { |
| | | 123 | | // set coordinate. |
| | 36922 | 124 | | this.SetCoordinate(_nextVertexId, longitude, latitude, e); |
| | | 125 | | |
| | | 126 | | // create id. |
| | 36922 | 127 | | var vertexId = new VertexId(_tileId, _nextVertexId); |
| | 36922 | 128 | | _nextVertexId++; |
| | | 129 | | |
| | | 130 | | // make room. |
| | 36922 | 131 | | ArrayBaseExtensions.EnsureMinimumSize(ref _pointers, vertexId.LocalId, DefaultSizeIncrease); |
| | | 132 | | |
| | 36922 | 133 | | return vertexId; |
| | 36922 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Returns true if the vertex exists. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="vertex"></param> |
| | | 140 | | /// <returns></returns> |
| | | 141 | | public bool HasVertex(VertexId vertex) |
| | 41 | 142 | | { |
| | 41 | 143 | | if (vertex.LocalId >= _nextVertexId) |
| | 13 | 144 | | { |
| | 13 | 145 | | return false; |
| | | 146 | | } |
| | | 147 | | |
| | 28 | 148 | | return true; |
| | 41 | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Gets the vertex with the given id. |
| | | 153 | | /// </summary> |
| | | 154 | | /// <param name="vertex">The vertex.</param> |
| | | 155 | | /// <param name="longitude">The longitude.</param> |
| | | 156 | | /// <param name="latitude">The latitude.</param> |
| | | 157 | | /// <param name="elevation">The elevation.</param> |
| | | 158 | | /// <returns>True if the vertex exists.</returns> |
| | | 159 | | public bool TryGetVertex(VertexId vertex, out double longitude, out double latitude, out float? elevation) |
| | 5824631 | 160 | | { |
| | 5824631 | 161 | | longitude = default; |
| | 5824631 | 162 | | latitude = default; |
| | 5824631 | 163 | | elevation = null; |
| | 5824631 | 164 | | if (vertex.LocalId >= _nextVertexId) |
| | 5327 | 165 | | { |
| | 5327 | 166 | | return false; |
| | | 167 | | } |
| | | 168 | | |
| | 5819304 | 169 | | this.GetCoordinate(vertex.LocalId, out longitude, out latitude, out elevation); |
| | 5819304 | 170 | | return true; |
| | 5824631 | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Adds a new edge and returns its id. |
| | | 175 | | /// </summary> |
| | | 176 | | /// <param name="vertex1">The first vertex.</param> |
| | | 177 | | /// <param name="vertex2">The second vertex.</param> |
| | | 178 | | /// <param name="shape">The shape."</param> |
| | | 179 | | /// <param name="attributes">The attributes."</param> |
| | | 180 | | /// <param name="edgeId">The edge id if this edge is a part of another tile.</param> |
| | | 181 | | /// <param name="edgeTypeId">The edge type id, if any.</param> |
| | | 182 | | /// <param name="length">The length in centimeters.</param> |
| | | 183 | | /// <param name="globalEdgeId">The global edge id, if any.</param> |
| | | 184 | | /// <returns>The new edge id.</returns> |
| | | 185 | | public EdgeId AddEdge(VertexId vertex1, VertexId vertex2, |
| | | 186 | | IEnumerable<(double longitude, double latitude, float? e)>? shape = null, |
| | | 187 | | IEnumerable<(string key, string value)>? attributes = null, EdgeId? edgeId = null, uint? edgeTypeId = null, |
| | | 188 | | uint? length = null, GlobalEdgeId? globalEdgeId = null) |
| | 51904 | 189 | | { |
| | 51904 | 190 | | if (vertex2.TileId != _tileId) |
| | 2275 | 191 | | { |
| | | 192 | | // this edge crosses tiles boundaries, it need special treatment and a stable id. |
| | | 193 | | // because the edge originates in this tile, this tile is responsible for generating the id. |
| | 2275 | 194 | | if (edgeId != null) throw new ArgumentException( |
| | 0 | 195 | | "The edge id shouldn't be a given, it should be generated by the originating tile.", |
| | 0 | 196 | | nameof(edgeId)); |
| | 2275 | 197 | | if (vertex1.TileId != _tileId) throw new ArgumentException("None of the two vertices in this edge are in thi |
| | 0 | 198 | | nameof(vertex1)); |
| | | 199 | | |
| | | 200 | | // generate a new cross tile id and store pointer to edge. |
| | 2275 | 201 | | edgeId = EdgeId.CrossEdgeId(_tileId, _nextCrossTileId); |
| | 2275 | 202 | | ArrayBaseExtensions.EnsureMinimumSize(ref _crossEdgePointers, _nextCrossTileId + 1); |
| | 2275 | 203 | | _crossEdgePointers[_nextCrossTileId] = _nextEdgeId; |
| | 2275 | 204 | | _nextCrossTileId++; |
| | 2275 | 205 | | } |
| | 49629 | 206 | | else if (vertex1.TileId != _tileId) |
| | 2274 | 207 | | { |
| | | 208 | | // this edge crosses tiles boundaries, it need special treatment and a stable id. |
| | | 209 | | // because the edge originates in another tile it should already have an id. |
| | 2274 | 210 | | if (edgeId == null) throw new ArgumentException( |
| | 0 | 211 | | "Cannot add an edge that doesn't start in this tile without a proper edge id.", |
| | 0 | 212 | | nameof(edgeId)); |
| | 2274 | 213 | | if (edgeId.Value.TileId != vertex1.TileId) throw new ArgumentException("The edge id doesn't match the tile i |
| | 0 | 214 | | nameof(edgeId)); |
| | 2274 | 215 | | } |
| | | 216 | | else |
| | 47355 | 217 | | { |
| | | 218 | | // this edge starts in this tile, it get an id from this tile. |
| | 47355 | 219 | | edgeId = new EdgeId(_tileId, _nextEdgeId); |
| | 47355 | 220 | | } |
| | | 221 | | |
| | | 222 | | // write the edge data. |
| | 51904 | 223 | | var newEdgePointer = _nextEdgeId; |
| | 51904 | 224 | | var size = EncodeVertex(ref _edges, _tileId, _nextEdgeId, vertex1); |
| | 51904 | 225 | | _nextEdgeId += size; |
| | 51904 | 226 | | size = EncodeVertex(ref _edges, _tileId, _nextEdgeId, vertex2); |
| | 51904 | 227 | | _nextEdgeId += size; |
| | | 228 | | |
| | | 229 | | // get previous pointers if vertex already has edges |
| | | 230 | | // set the new pointers. |
| | 51904 | 231 | | uint? v1p = null; |
| | 51904 | 232 | | if (vertex1.TileId == _tileId) |
| | 49630 | 233 | | { |
| | 49630 | 234 | | v1p = _pointers[vertex1.LocalId].DecodeNullableData(); |
| | 49630 | 235 | | _pointers[vertex1.LocalId] = newEdgePointer.EncodeToNullableData(); |
| | 49630 | 236 | | } |
| | | 237 | | |
| | 51904 | 238 | | uint? v2p = null; |
| | 51904 | 239 | | if (vertex2.TileId == _tileId) |
| | 49629 | 240 | | { |
| | 49629 | 241 | | v2p = _pointers[vertex2.LocalId].DecodeNullableData(); |
| | 49629 | 242 | | _pointers[vertex2.LocalId] = newEdgePointer.EncodeToNullableData(); |
| | 49629 | 243 | | } |
| | | 244 | | |
| | | 245 | | // set next pointers. |
| | 51904 | 246 | | size = EncodePointer(ref _edges, _nextEdgeId, v1p); |
| | 51904 | 247 | | _nextEdgeId += size; |
| | 51904 | 248 | | size = EncodePointer(ref _edges, _nextEdgeId, v2p); |
| | 51904 | 249 | | _nextEdgeId += size; |
| | | 250 | | |
| | | 251 | | // write edge id explicitly if not in this edge. |
| | 51904 | 252 | | if (vertex1.TileId != vertex2.TileId) |
| | 4549 | 253 | | { |
| | | 254 | | // this data will only be there for edges crossing tile boundaries. |
| | 4549 | 255 | | _nextEdgeId += (uint)_edges.SetDynamicUInt32(_nextEdgeId, |
| | 4549 | 256 | | edgeId.Value.LocalId - EdgeId.MinCrossId); |
| | 4549 | 257 | | } |
| | | 258 | | |
| | | 259 | | // write edge profile id. |
| | 51904 | 260 | | _nextEdgeId += SetDynamicUIn32Nullable(ref _edges, _nextEdgeId, edgeTypeId); |
| | | 261 | | |
| | | 262 | | // write length. |
| | 51904 | 263 | | _nextEdgeId += SetDynamicUIn32Nullable(ref _edges, _nextEdgeId, length); |
| | | 264 | | |
| | | 265 | | // set tail and head order. |
| | 51904 | 266 | | _edges.SetTailHeadOrder(_nextEdgeId, null, null); |
| | 51904 | 267 | | _nextEdgeId++; |
| | | 268 | | |
| | | 269 | | // take care of shape if any. |
| | 51904 | 270 | | uint? shapePointer = null; |
| | 51904 | 271 | | if (shape != null) |
| | 51687 | 272 | | { |
| | 51687 | 273 | | shapePointer = this.SetShape(shape); |
| | 51687 | 274 | | } |
| | | 275 | | |
| | 51904 | 276 | | size = EncodePointer(ref _edges, _nextEdgeId, shapePointer); |
| | 51904 | 277 | | _nextEdgeId += size; |
| | | 278 | | |
| | | 279 | | // take care of attributes if any. |
| | 51904 | 280 | | uint? attributesPointer = null; |
| | 51904 | 281 | | if (attributes != null || globalEdgeId != null) |
| | 51719 | 282 | | { |
| | 51719 | 283 | | attributesPointer = this.SetAttributes(attributes ?? [], globalEdgeId); |
| | 51719 | 284 | | } |
| | | 285 | | |
| | 51904 | 286 | | size = EncodePointer(ref _edges, _nextEdgeId, attributesPointer); |
| | 51904 | 287 | | _nextEdgeId += size; |
| | | 288 | | |
| | 51904 | 289 | | return edgeId.Value; |
| | 51904 | 290 | | } |
| | | 291 | | |
| | | 292 | | private HashSet<EdgeId>? _deletedEdges; |
| | | 293 | | |
| | | 294 | | /// <summary> |
| | | 295 | | /// Marks the given edge as deleted. |
| | | 296 | | /// </summary> |
| | | 297 | | /// <param name="edge">The edge to delete.</param> |
| | | 298 | | internal void DeleteEdge(EdgeId edge) |
| | 10 | 299 | | { |
| | 10 | 300 | | _deletedEdges ??= []; |
| | 10 | 301 | | _deletedEdges.Add(edge); |
| | 10 | 302 | | } |
| | | 303 | | |
| | | 304 | | /// <summary> |
| | | 305 | | /// Returns true if the edge is deleted. |
| | | 306 | | /// </summary> |
| | | 307 | | /// <param name="edge">The edge.</param> |
| | | 308 | | /// <returns>True if the edge was deleted.</returns> |
| | | 309 | | internal bool IsEdgeDeleted(EdgeId edge) |
| | 6541976 | 310 | | { |
| | 13083946 | 311 | | if (_deletedEdges == null) return false; |
| | | 312 | | |
| | 6 | 313 | | return _deletedEdges.Contains(edge); |
| | 6541976 | 314 | | } |
| | | 315 | | |
| | | 316 | | /// <summary> |
| | | 317 | | /// Returns true if there are deleted edges. |
| | | 318 | | /// </summary> |
| | 405 | 319 | | internal bool HasDeletedEdges => _deletedEdges != null; |
| | | 320 | | |
| | | 321 | | /// <summary> |
| | | 322 | | /// Removes all the deleted edges. |
| | | 323 | | /// </summary> |
| | | 324 | | /// <remarks>This changes all the edges if all edges written after a deleted edge.</remarks> |
| | | 325 | | internal void RemoveDeletedEdges() |
| | 10 | 326 | | { |
| | 10 | 327 | | if (_deletedEdges == null) return; |
| | | 328 | | |
| | | 329 | | // reset vertex pointers. |
| | 340 | 330 | | for (var i = 0; i < _pointers.Length; i++) |
| | 160 | 331 | | { |
| | 160 | 332 | | _pointers[i] = 0; |
| | 160 | 333 | | } |
| | | 334 | | |
| | | 335 | | // redo edges, skipping deleted edges. |
| | 10 | 336 | | var nextEdgeId = _nextEdgeId; |
| | 10 | 337 | | var p = 0U; |
| | 10 | 338 | | var newP = 0U; |
| | 24 | 339 | | while (p < nextEdgeId) |
| | 14 | 340 | | { |
| | | 341 | | // read edge data. |
| | 14 | 342 | | var currentEdgeId = p; |
| | 14 | 343 | | p += this.DecodeVertex(p, out var local1Id, out var tile1Id); |
| | 14 | 344 | | var vertex1 = new VertexId(tile1Id, local1Id); |
| | 14 | 345 | | p += this.DecodeVertex(p, out var local2Id, out var tile2Id); |
| | 14 | 346 | | var vertex2 = new VertexId(tile2Id, local2Id); |
| | 14 | 347 | | p += this.DecodePointer(p, out _); |
| | 14 | 348 | | p += this.DecodePointer(p, out _); |
| | 14 | 349 | | uint? crossEdgeId = null; |
| | 14 | 350 | | if (tile1Id != tile2Id) |
| | 4 | 351 | | { |
| | 4 | 352 | | p += _edges.GetDynamicUInt32(p, out var c); |
| | 4 | 353 | | crossEdgeId = c; |
| | 4 | 354 | | } |
| | | 355 | | |
| | 14 | 356 | | p += _edges.GetDynamicUInt32Nullable(p, out var edgeTypeId); |
| | 14 | 357 | | p += _edges.GetDynamicUInt32Nullable(p, out var length); |
| | 14 | 358 | | var tailHeadOrder = _edges[p]; |
| | 14 | 359 | | p++; |
| | 14 | 360 | | p += this.DecodePointer(p, out var shapePointer); |
| | 14 | 361 | | p += this.DecodePointer(p, out var attributePointer); |
| | | 362 | | |
| | | 363 | | // check if edge was deleted. |
| | 20 | 364 | | if (_deletedEdges.Contains(new EdgeId(_tileId, currentEdgeId))) continue; |
| | 8 | 365 | | if (crossEdgeId.HasValue) |
| | 4 | 366 | | { |
| | 8 | 367 | | if (_deletedEdges.Contains(EdgeId.CrossEdgeId(vertex1.TileId, crossEdgeId.Value))) continue; |
| | 0 | 368 | | } |
| | | 369 | | |
| | | 370 | | // no need to overwrite identical data. |
| | 4 | 371 | | if (p == newP) continue; |
| | | 372 | | |
| | | 373 | | // write edge data again. |
| | 4 | 374 | | var newEdgePointer = newP; |
| | 4 | 375 | | newP += EncodeVertex(ref _edges, _tileId, newP, vertex1); |
| | 4 | 376 | | newP += EncodeVertex(ref _edges, _tileId, newP, vertex2); |
| | 4 | 377 | | uint? v1p = null; |
| | 4 | 378 | | if (vertex1.TileId == _tileId) |
| | 4 | 379 | | { |
| | 4 | 380 | | v1p = _pointers[vertex1.LocalId].DecodeNullableData(); |
| | 4 | 381 | | _pointers[vertex1.LocalId] = newEdgePointer.EncodeToNullableData(); |
| | 4 | 382 | | } |
| | | 383 | | |
| | 4 | 384 | | uint? v2P = null; |
| | 4 | 385 | | if (vertex2.TileId == _tileId) |
| | 4 | 386 | | { |
| | 4 | 387 | | v2P = _pointers[vertex2.LocalId].DecodeNullableData(); |
| | 4 | 388 | | _pointers[vertex2.LocalId] = newEdgePointer.EncodeToNullableData(); |
| | 4 | 389 | | } |
| | | 390 | | |
| | 4 | 391 | | newP += EncodePointer(ref _edges, newP, v1p); |
| | 4 | 392 | | newP += EncodePointer(ref _edges, newP, v2P); |
| | 4 | 393 | | if (crossEdgeId != null) |
| | 0 | 394 | | { |
| | 0 | 395 | | newP += (uint)_edges.SetDynamicUInt32(newP, crossEdgeId.Value); |
| | 0 | 396 | | if (vertex1.TileId == _tileId) |
| | 0 | 397 | | { |
| | 0 | 398 | | _crossEdgePointers[crossEdgeId.Value] = newEdgePointer; |
| | 0 | 399 | | } |
| | 0 | 400 | | } |
| | | 401 | | |
| | 4 | 402 | | newP += _edges.SetDynamicUInt32Nullable(newP, edgeTypeId); |
| | 4 | 403 | | newP += _edges.SetDynamicUInt32Nullable(newP, length); |
| | 4 | 404 | | _edges[newP] = tailHeadOrder; |
| | 4 | 405 | | newP++; |
| | 4 | 406 | | newP += EncodePointer(ref _edges, newP, shapePointer); |
| | 4 | 407 | | newP += EncodePointer(ref _edges, newP, attributePointer); |
| | 4 | 408 | | } |
| | | 409 | | |
| | 10 | 410 | | _nextEdgeId = newP; |
| | 10 | 411 | | _deletedEdges = null; |
| | 10 | 412 | | } |
| | | 413 | | |
| | | 414 | | internal NetworkTile CloneForEdgeTypeMap( |
| | | 415 | | (Guid id, Func<IEnumerable<(string key, string value)>, uint> func) edgeTypeMap) |
| | 6 | 416 | | { |
| | 6 | 417 | | var edges = new byte[_edges.Length]; |
| | 6 | 418 | | var pointers = new uint[_pointers.Length]; |
| | 6 | 419 | | var crossEdgePointers = new uint[_crossEdgePointers.Length]; |
| | 6 | 420 | | var nextEdgeId = _nextEdgeId; |
| | 6 | 421 | | var p = 0U; |
| | 6 | 422 | | var newP = 0U; |
| | 16 | 423 | | while (p < nextEdgeId) |
| | 10 | 424 | | { |
| | | 425 | | // read edge data. |
| | 10 | 426 | | p += this.DecodeVertex(p, out var local1Id, out var tile1Id); |
| | 10 | 427 | | var vertex1 = new VertexId(tile1Id, local1Id); |
| | 10 | 428 | | p += this.DecodeVertex(p, out var local2Id, out var tile2Id); |
| | 10 | 429 | | var vertex2 = new VertexId(tile2Id, local2Id); |
| | 10 | 430 | | p += this.DecodePointer(p, out _); |
| | 10 | 431 | | p += this.DecodePointer(p, out _); |
| | 10 | 432 | | uint? crossEdgeId = null; |
| | 10 | 433 | | if (tile1Id != tile2Id) |
| | 0 | 434 | | { |
| | 0 | 435 | | p += (uint)_edges.GetDynamicUInt32(p, out var c); |
| | 0 | 436 | | crossEdgeId = c; |
| | 0 | 437 | | } |
| | | 438 | | |
| | 10 | 439 | | p += _edges.GetDynamicUInt32Nullable(p, out var _); |
| | 10 | 440 | | p += _edges.GetDynamicUInt32Nullable(p, out var length); |
| | 10 | 441 | | var tailHeadOrder = _edges[p]; |
| | 10 | 442 | | p++; |
| | 10 | 443 | | p += this.DecodePointer(p, out var shapePointer); |
| | 10 | 444 | | p += this.DecodePointer(p, out var attributePointer); |
| | | 445 | | |
| | | 446 | | // generate new edge type id. |
| | 10 | 447 | | var newEdgeTypeId = edgeTypeMap.func(this.GetAttributes(attributePointer)); |
| | | 448 | | |
| | | 449 | | // write edge data again. |
| | 10 | 450 | | var newEdgePointer = newP; |
| | 10 | 451 | | newP += EncodeVertex(ref edges, _tileId, newP, vertex1); |
| | 10 | 452 | | newP += EncodeVertex(ref edges, _tileId, newP, vertex2); |
| | 10 | 453 | | uint? v1p = null; |
| | 10 | 454 | | if (vertex1.TileId == _tileId) |
| | 10 | 455 | | { |
| | 10 | 456 | | v1p = pointers[vertex1.LocalId].DecodeNullableData(); |
| | 10 | 457 | | pointers[vertex1.LocalId] = newEdgePointer.EncodeToNullableData(); |
| | 10 | 458 | | } |
| | | 459 | | |
| | 10 | 460 | | uint? v2p = null; |
| | 10 | 461 | | if (vertex2.TileId == _tileId) |
| | 10 | 462 | | { |
| | 10 | 463 | | v2p = pointers[vertex2.LocalId].DecodeNullableData(); |
| | 10 | 464 | | pointers[vertex2.LocalId] = newEdgePointer.EncodeToNullableData(); |
| | 10 | 465 | | } |
| | | 466 | | |
| | 10 | 467 | | newP += EncodePointer(ref edges, newP, v1p); |
| | 10 | 468 | | newP += EncodePointer(ref edges, newP, v2p); |
| | 10 | 469 | | if (crossEdgeId != null) |
| | 0 | 470 | | { |
| | 0 | 471 | | newP += edges.SetDynamicUInt32(newP, crossEdgeId.Value); |
| | 0 | 472 | | if (vertex1.TileId == _tileId) |
| | 0 | 473 | | { |
| | 0 | 474 | | crossEdgePointers[crossEdgeId.Value] = newEdgePointer; |
| | 0 | 475 | | } |
| | 0 | 476 | | } |
| | | 477 | | |
| | 10 | 478 | | newP += edges.SetDynamicUInt32Nullable(newP, newEdgeTypeId); |
| | 10 | 479 | | newP += edges.SetDynamicUInt32Nullable(newP, length); |
| | 10 | 480 | | edges[newP] = tailHeadOrder; |
| | 10 | 481 | | newP++; |
| | 10 | 482 | | newP += EncodePointer(ref edges, newP, shapePointer); |
| | 10 | 483 | | newP += EncodePointer(ref edges, newP, attributePointer); |
| | 10 | 484 | | } |
| | | 485 | | |
| | 6 | 486 | | return new NetworkTile(_zoom, _tileId, edgeTypeMap.id, _nextCrossTileId, pointers, edges, crossEdgePointers, |
| | 6 | 487 | | _coordinates, |
| | 6 | 488 | | _shapes, _attributes, _strings, _turnCosts, _nextVertexId, _nextEdgeId, |
| | 6 | 489 | | _nextAttributePointer, _nextShapePointer, _nextStringId); |
| | 6 | 490 | | } |
| | | 491 | | |
| | | 492 | | internal uint VertexEdgePointer(uint vertex) |
| | 1143302 | 493 | | { |
| | 1143302 | 494 | | return _pointers[vertex]; |
| | 1143302 | 495 | | } |
| | | 496 | | |
| | | 497 | | internal static byte EncodeVertex(ref byte[] edges, uint localTileId, uint location, VertexId vertexId) |
| | 103836 | 498 | | { |
| | 103836 | 499 | | if (vertexId.TileId == localTileId) |
| | 99287 | 500 | | { |
| | | 501 | | // same tile, only store local id. |
| | 99287 | 502 | | if (edges.Length <= location + 5) |
| | 12608 | 503 | | { |
| | 12608 | 504 | | Array.Resize(ref edges, edges.Length + DefaultSizeIncrease); |
| | 12608 | 505 | | } |
| | | 506 | | |
| | 99287 | 507 | | return edges.SetDynamicUInt32(location, vertexId.LocalId); |
| | | 508 | | } |
| | | 509 | | |
| | | 510 | | // other tile, store full id. |
| | 4549 | 511 | | if (edges.Length <= location + 10) |
| | 1959 | 512 | | { |
| | 1959 | 513 | | Array.Resize(ref edges, edges.Length + DefaultSizeIncrease); |
| | 1959 | 514 | | } |
| | | 515 | | |
| | 4549 | 516 | | var encodedId = vertexId.Encode(); |
| | 4549 | 517 | | return edges.SetDynamicUInt64(location, encodedId); |
| | 103836 | 518 | | } |
| | | 519 | | |
| | | 520 | | internal byte DecodeVertex(uint location, out uint localId, out uint tileId) |
| | 13086088 | 521 | | { |
| | 13086088 | 522 | | var size = _edges.GetDynamicUInt64(location, out var encodedId); |
| | 13086088 | 523 | | if (encodedId < uint.MaxValue) |
| | 12974130 | 524 | | { |
| | 12974130 | 525 | | localId = (uint)encodedId; |
| | 12974130 | 526 | | tileId = _tileId; |
| | 12974130 | 527 | | return size; |
| | | 528 | | } |
| | | 529 | | |
| | 111958 | 530 | | VertexId.Decode(encodedId, out tileId, out localId); |
| | 111958 | 531 | | return size; |
| | 13086088 | 532 | | } |
| | | 533 | | |
| | | 534 | | internal byte DecodeEdgeCrossId(uint location, out uint edgeCrossId) |
| | 111913 | 535 | | { |
| | 111913 | 536 | | var s = _edges.GetDynamicUInt32(location, out var c); |
| | 111913 | 537 | | edgeCrossId = EdgeId.MinCrossId + c; |
| | 111913 | 538 | | return s; |
| | 111913 | 539 | | } |
| | | 540 | | |
| | | 541 | | internal uint GetEdgeCrossPointer(uint edgeCrossId) |
| | 49248 | 542 | | { |
| | 49248 | 543 | | return _crossEdgePointers[edgeCrossId]; |
| | 49248 | 544 | | } |
| | | 545 | | |
| | | 546 | | internal static byte EncodePointer(ref byte[] edges, uint location, uint? pointer) |
| | 207672 | 547 | | { |
| | | 548 | | // TODO: save the diff instead of the full pointer. |
| | 207672 | 549 | | if (edges.Length <= location + 5) |
| | 27457 | 550 | | { |
| | 27457 | 551 | | Array.Resize(ref edges, edges.Length + DefaultSizeIncrease); |
| | 27457 | 552 | | } |
| | | 553 | | |
| | 207672 | 554 | | return edges.SetDynamicUInt32(location, |
| | 207672 | 555 | | pointer.EncodeAsNullableData()); |
| | 207672 | 556 | | } |
| | | 557 | | |
| | | 558 | | internal byte DecodePointer(uint location, out uint? pointer) |
| | 14390085 | 559 | | { |
| | 14390085 | 560 | | var size = _edges.GetDynamicUInt32(location, out var data); |
| | 14390085 | 561 | | pointer = data.DecodeNullableData(); |
| | 14390085 | 562 | | return size; |
| | 14390085 | 563 | | } |
| | | 564 | | |
| | | 565 | | internal static byte SetDynamicUIn32Nullable(ref byte[] edges, uint pointer, uint? data) |
| | 103808 | 566 | | { |
| | 114572 | 567 | | while (edges.Length <= pointer + 5) |
| | 10764 | 568 | | { |
| | 10764 | 569 | | Array.Resize(ref edges, edges.Length + DefaultSizeIncrease); |
| | 10764 | 570 | | } |
| | | 571 | | |
| | 103808 | 572 | | return edges.SetDynamicUInt32Nullable(pointer, data); |
| | 103808 | 573 | | } |
| | | 574 | | |
| | | 575 | | internal void GetTailHeadOrder(uint location, ref byte? tail, ref byte? head) |
| | 6543019 | 576 | | { |
| | 6543019 | 577 | | _edges.GetTailHeadOrder(location, ref tail, ref head); |
| | 6543019 | 578 | | } |
| | | 579 | | |
| | | 580 | | internal byte DecodeEdgePointerId(uint location, out uint? edgeProfileId) |
| | 13086038 | 581 | | { |
| | 13086038 | 582 | | return _edges.GetDynamicUInt32Nullable(location, out edgeProfileId); |
| | 13086038 | 583 | | } |
| | | 584 | | |
| | | 585 | | private void WriteEdgesAndVerticesTo(Stream stream) |
| | 8 | 586 | | { |
| | | 587 | | // write vertex pointers. |
| | 8 | 588 | | stream.WriteVarUInt32(_nextVertexId); |
| | 42 | 589 | | for (var i = 0; i < _nextVertexId; i++) |
| | 13 | 590 | | { |
| | 13 | 591 | | stream.WriteVarUInt32(_pointers[i]); |
| | 13 | 592 | | } |
| | | 593 | | |
| | | 594 | | // write edges. |
| | 8 | 595 | | stream.WriteVarUInt32(_nextEdgeId); |
| | 106 | 596 | | for (var i = 0; i < _nextEdgeId; i++) |
| | 45 | 597 | | { |
| | 45 | 598 | | stream.WriteByte(_edges[i]); |
| | 45 | 599 | | } |
| | | 600 | | |
| | | 601 | | // write cross edge pointers. |
| | 8 | 602 | | stream.WriteVarUInt32(_nextCrossTileId); |
| | 16 | 603 | | for (var i = 0; i < _nextCrossTileId; i++) |
| | 0 | 604 | | { |
| | 0 | 605 | | stream.WriteVarUInt32(_crossEdgePointers[i]); |
| | 0 | 606 | | } |
| | 8 | 607 | | } |
| | | 608 | | |
| | | 609 | | private void ReadEdgesAndVerticesFrom(Stream stream) |
| | 8 | 610 | | { |
| | | 611 | | // read vertex pointers. |
| | 8 | 612 | | _nextVertexId = stream.ReadVarUInt32(); |
| | 8 | 613 | | Array.Resize(ref _pointers, (int)_nextVertexId); |
| | 42 | 614 | | for (var i = 0; i < _nextVertexId; i++) |
| | 13 | 615 | | { |
| | 13 | 616 | | _pointers[i] = stream.ReadVarUInt32(); |
| | 13 | 617 | | } |
| | | 618 | | |
| | | 619 | | // read edges. |
| | 8 | 620 | | _nextEdgeId = stream.ReadVarUInt32(); |
| | 8 | 621 | | Array.Resize(ref _edges, (int)_nextEdgeId); |
| | 106 | 622 | | for (var i = 0; i < _nextEdgeId; i++) |
| | 45 | 623 | | { |
| | 45 | 624 | | _edges[i] = (byte)stream.ReadByte(); |
| | 45 | 625 | | } |
| | | 626 | | |
| | | 627 | | // read cross tile edge pointers. |
| | 8 | 628 | | _nextCrossTileId = stream.ReadVarUInt32(); |
| | 8 | 629 | | Array.Resize(ref _crossEdgePointers, (int)_nextCrossTileId); |
| | 16 | 630 | | for (var i = 0; i < _nextCrossTileId; i++) |
| | 0 | 631 | | { |
| | 0 | 632 | | _crossEdgePointers[i] = stream.ReadVarUInt32(); |
| | 0 | 633 | | } |
| | 8 | 634 | | } |
| | | 635 | | |
| | | 636 | | private void ReadEdgesAndVerticesFrom(byte[] data, ref int offset) |
| | 0 | 637 | | { |
| | 0 | 638 | | _nextVertexId = BitCoderBuffer.GetVarUInt32(data, ref offset); |
| | 0 | 639 | | Array.Resize(ref _pointers, (int)_nextVertexId); |
| | 0 | 640 | | for (var i = 0; i < _nextVertexId; i++) |
| | 0 | 641 | | { |
| | 0 | 642 | | _pointers[i] = BitCoderBuffer.GetVarUInt32(data, ref offset); |
| | 0 | 643 | | } |
| | | 644 | | |
| | 0 | 645 | | _nextEdgeId = BitCoderBuffer.GetVarUInt32(data, ref offset); |
| | 0 | 646 | | Array.Resize(ref _edges, (int)_nextEdgeId); |
| | 0 | 647 | | Buffer.BlockCopy(data, offset, _edges, 0, (int)_nextEdgeId); |
| | 0 | 648 | | offset += (int)_nextEdgeId; |
| | | 649 | | |
| | 0 | 650 | | _nextCrossTileId = BitCoderBuffer.GetVarUInt32(data, ref offset); |
| | 0 | 651 | | Array.Resize(ref _crossEdgePointers, (int)_nextCrossTileId); |
| | 0 | 652 | | for (var i = 0; i < _nextCrossTileId; i++) |
| | 0 | 653 | | { |
| | 0 | 654 | | _crossEdgePointers[i] = BitCoderBuffer.GetVarUInt32(data, ref offset); |
| | 0 | 655 | | } |
| | 0 | 656 | | } |
| | | 657 | | |
| | | 658 | | private void WriteEdgesAndVerticesTo(byte[] data, ref int offset) |
| | 0 | 659 | | { |
| | 0 | 660 | | BitCoderBuffer.SetVarUInt32(data, ref offset, _nextVertexId); |
| | 0 | 661 | | for (var i = 0; i < _nextVertexId; i++) |
| | 0 | 662 | | { |
| | 0 | 663 | | BitCoderBuffer.SetVarUInt32(data, ref offset, _pointers[i]); |
| | 0 | 664 | | } |
| | | 665 | | |
| | 0 | 666 | | BitCoderBuffer.SetVarUInt32(data, ref offset, _nextEdgeId); |
| | 0 | 667 | | Buffer.BlockCopy(_edges, 0, data, offset, (int)_nextEdgeId); |
| | 0 | 668 | | offset += (int)_nextEdgeId; |
| | | 669 | | |
| | 0 | 670 | | BitCoderBuffer.SetVarUInt32(data, ref offset, _nextCrossTileId); |
| | 0 | 671 | | for (var i = 0; i < _nextCrossTileId; i++) |
| | 0 | 672 | | { |
| | 0 | 673 | | BitCoderBuffer.SetVarUInt32(data, ref offset, _crossEdgePointers[i]); |
| | 0 | 674 | | } |
| | 0 | 675 | | } |
| | | 676 | | } |