| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using Itinero.Data; |
| | 5 | | using Itinero.Network.Writer; |
| | 6 | |
|
| | 7 | | namespace Itinero.Network.Tiles.Standalone.Writer; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Extension methods related to writing standalone tiles to a network. |
| | 11 | | /// </summary> |
| | 12 | | public static class RoutingNetworkWriterExtensions |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Adds a tile in the form of a standalone tile to the network. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="writer">The writer to write to.</param> |
| | 18 | | /// <param name="tile">The tile to add.</param> |
| | 19 | | /// <param name="globalIdSet">The global id set.</param> |
| | 20 | | public static void AddStandaloneTile(this RoutingNetworkWriter writer, StandaloneNetworkTile tile, |
| | 21 | | GlobalNetworkManager globalIdSet) |
| 0 | 22 | | { |
| | 23 | | // add the tile without boundary crossings. |
| 0 | 24 | | writer.AddTile(tile.NetworkTile); |
| | 25 | |
|
| | 26 | | // add the boundary crossings for tiles that are already loaded. |
| 0 | 27 | | var boundaryEdges = new Dictionary<BoundaryEdgeId, EdgeId>(); |
| 0 | 28 | | foreach (var crossing in tile.GetBoundaryCrossings()) |
| 0 | 29 | | { |
| | 30 | | // add crossings in target vertex already in global id set. |
| 0 | 31 | | var other = crossing.isToTile ? crossing.globalIdFrom : crossing.globalIdTo; |
| 0 | 32 | | if (globalIdSet.VertexIdSet.TryGet(other, out var otherVertexId)) |
| 0 | 33 | | { |
| | 34 | | // add the edge. |
| | 35 | | EdgeId newEdge; |
| 0 | 36 | | if (crossing.isToTile) |
| 0 | 37 | | { |
| 0 | 38 | | newEdge = writer.AddEdge(otherVertexId, crossing.vertex, |
| 0 | 39 | | ArraySegment<(double longitude, double latitude, float? e)>.Empty, |
| 0 | 40 | | crossing.attributes, crossing.edgeTypeId, crossing.length); |
| 0 | 41 | | } |
| | 42 | | else |
| 0 | 43 | | { |
| 0 | 44 | | newEdge = writer.AddEdge(crossing.vertex, otherVertexId, |
| 0 | 45 | | ArraySegment<(double longitude, double latitude, float? e)>.Empty, |
| 0 | 46 | | crossing.attributes, crossing.edgeTypeId, crossing.length); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | // register globally and index crossings. |
| 0 | 50 | | boundaryEdges[crossing.id] = newEdge; |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | // update global id set with the vertex in the tile. |
| 0 | 54 | | globalIdSet.VertexIdSet.Set(crossing.isToTile ? crossing.globalIdTo : crossing.globalIdFrom, |
| 0 | 55 | | crossing.vertex); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | // read and store all global edge ids. |
| 0 | 59 | | foreach (var (globalEdgeId, edgeId, boundaryEdgeId) in tile.GetGlobalEdgeIds()) |
| 0 | 60 | | { |
| 0 | 61 | | if (edgeId != null) |
| 0 | 62 | | { |
| 0 | 63 | | globalIdSet.EdgeIdSet.Set(globalEdgeId, edgeId.Value); |
| 0 | 64 | | continue; |
| | 65 | | } |
| | 66 | |
|
| 0 | 67 | | if (boundaryEdgeId == null) throw new Exception("global edge has to have at least one edge id"); |
| 0 | 68 | | if (!boundaryEdges.TryGetValue(boundaryEdgeId.Value, out var newEdgeId)) continue; |
| | 69 | |
|
| 0 | 70 | | globalIdSet.EdgeIdSet.Set(globalEdgeId, newEdgeId); |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | // add boundary crossing turn cost or register globally. |
| 0 | 74 | | foreach (var crossingTurnCosts in tile.GetGlobalTurnCost()) |
| 0 | 75 | | { |
| | 76 | | // check if all edges are in the network and fetch them. |
| 0 | 77 | | var hasAllEdges = true; |
| 0 | 78 | | var edges = crossingTurnCosts.edges.Select(x => |
| 0 | 79 | | { |
| 0 | 80 | | if (globalIdSet.EdgeIdSet.TryGet(x.globalEdgeId, out var newEdgeId)) return (newEdgeId, x.forward); |
| 0 | 81 | |
|
| 0 | 82 | | hasAllEdges = false; |
| 0 | 83 | | return (EdgeId.Empty, false); |
| 0 | 84 | | }).ToArray(); |
| | 85 | |
|
| | 86 | | // if all edges are there add turn costs. |
| | 87 | | // if not all edges are there it will be added when the last tile containing an edge for this restriction is |
| 0 | 88 | | if (hasAllEdges) |
| 0 | 89 | | { |
| | 90 | | // figure out what vertex the turn costs need to be added at. |
| 0 | 91 | | var edgeEnumerator = writer.GetEdgeEnumerator(); |
| 0 | 92 | | if (!edgeEnumerator.MoveTo(edges[^1].Item1, edges[^1].Item2)) |
| 0 | 93 | | throw new Exception("edge should exist"); |
| 0 | 94 | | var turnCostVertex = edgeEnumerator.Tail; |
| | 95 | |
|
| 0 | 96 | | writer.AddTurnCosts(turnCostVertex, crossingTurnCosts.attributes, |
| 0 | 97 | | edges.Select(x => x.Item1).ToArray(), |
| 0 | 98 | | crossingTurnCosts.costs, null, crossingTurnCosts.turnCostType); |
| 0 | 99 | | } |
| 0 | 100 | | } |
| 0 | 101 | | } |
| | 102 | | } |