| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using Itinero.Geo.Elevation; |
| | 5 | | using Itinero.IO.Osm.Restrictions; |
| | 6 | | using Itinero.IO.Osm.Restrictions.Barriers; |
| | 7 | | using Itinero.Network; |
| | 8 | | using Itinero.Network.Mutation; |
| | 9 | | using OsmSharp; |
| | 10 | | using OsmSharp.Streams; |
| | 11 | |
|
| | 12 | | namespace Itinero.IO.Osm; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class RouterDbStreamTarget : OsmStreamTarget |
| | 16 | | { |
| 3 | 17 | | private readonly Dictionary<long, VertexId> _vertices = new(); |
| | 18 | | private readonly RoutingNetworkMutator _mutableRouterDb; |
| | 19 | | private readonly IElevationHandler? _elevationHandler; |
| 3 | 20 | | private readonly OsmTurnRestrictionParser _restrictionParser = new(); |
| 3 | 21 | | private readonly Dictionary<long, Way?> _restrictionMembers = new(); |
| 3 | 22 | | private readonly OsmBarrierParser _barrierParser = new(); |
| 3 | 23 | | private readonly List<OsmBarrier> _osmBarriers = new(); |
| 3 | 24 | | private readonly Dictionary<long, (double longitude, double latitude)> _nodeLocations = new(); |
| 3 | 25 | | private readonly HashSet<long> _usedNodes = new(); |
| 3 | 26 | | private readonly List<OsmTurnRestriction> _osmTurnRestrictions = new(); |
| 3 | 27 | | private readonly Dictionary<(long wayId, int node1Idx, int node2Idx), EdgeId> _restrictedEdges = new(); |
| | 28 | |
|
| | 29 | | /// <inheritdoc /> |
| 3 | 30 | | public RouterDbStreamTarget(RoutingNetworkMutator mutableRouterDb, |
| 3 | 31 | | IElevationHandler? elevationHandler = null) |
| 3 | 32 | | { |
| 3 | 33 | | _mutableRouterDb = mutableRouterDb; |
| 3 | 34 | | _elevationHandler = elevationHandler; |
| 3 | 35 | | } |
| | 36 | |
|
| 3 | 37 | | private bool _firstPass = true; |
| | 38 | |
|
| | 39 | | public override void Initialize() |
| 6 | 40 | | { |
| 6 | 41 | | _firstPass = true; |
| 6 | 42 | | } |
| | 43 | |
|
| | 44 | | public override bool OnBeforePull() |
| 3 | 45 | | { |
| | 46 | | // execute the first pass. |
| 3 | 47 | | this.DoPull(true, false, false); |
| | 48 | |
|
| | 49 | | // add barriers as turn weights. |
| 3 | 50 | | var tileEnumerator = _mutableRouterDb.GetEdgeEnumerator(); |
| 3 | 51 | | var networkRestrictions = new List<NetworkRestriction>(); |
| 9 | 52 | | foreach (var osmBarrier in _osmBarriers) |
| 0 | 53 | | { |
| 0 | 54 | | var networkBarriersResult = osmBarrier.ToNetworkRestrictions(n => |
| 0 | 55 | | { |
| 0 | 56 | | if (!_vertices.TryGetValue(n, out var v)) throw new Exception("Node should exist as vertex"); |
| 0 | 57 | | tileEnumerator.MoveTo(v); |
| 0 | 58 | | return tileEnumerator; |
| 0 | 59 | | }); |
| 0 | 60 | | if (networkBarriersResult.IsError) continue; |
| | 61 | |
|
| 0 | 62 | | networkRestrictions.AddRange(networkBarriersResult.Value); |
| 0 | 63 | | } |
| | 64 | |
|
| 3 | 65 | | this.AddNetworkRestrictions(networkRestrictions); |
| | 66 | |
|
| | 67 | | // move to second pass. |
| 3 | 68 | | _firstPass = false; |
| 3 | 69 | | this.Source.Reset(); |
| 3 | 70 | | this.DoPull(); |
| | 71 | |
|
| 3 | 72 | | return false; |
| 3 | 73 | | } |
| | 74 | |
|
| | 75 | | /// <inheritdoc /> |
| | 76 | | public override void AddNode(Node node) |
| 10 | 77 | | { |
| 10 | 78 | | if (!node.Id.HasValue) return; |
| 10 | 79 | | if (!node.Longitude.HasValue || !node.Latitude.HasValue) return; |
| | 80 | |
|
| | 81 | | // FIRST PASS: ignore nodes. |
| 10 | 82 | | if (_firstPass) |
| 0 | 83 | | { |
| 0 | 84 | | if (_barrierParser.IsBarrier(node)) |
| 0 | 85 | | { |
| | 86 | | // make sure the barriers are core nodes, they need a turn cost. |
| | 87 | | // log nodes are barriers to be able to detect their ways, |
| | 88 | | // only take nodes in the tile to mark as barrier. |
| 0 | 89 | | _vertices[node.Id.Value] = VertexId.Empty; |
| 0 | 90 | | } |
| 0 | 91 | | return; |
| | 92 | | } |
| | 93 | |
|
| | 94 | | // SECOND PASS: keep node locations. |
| 10 | 95 | | _nodeLocations[node.Id.Value] = (node.Longitude.Value, node.Latitude.Value); |
| 14 | 96 | | if (!_vertices.TryGetValue(node.Id.Value, out _)) return; |
| | 97 | |
|
| | 98 | | // a vertex can be a barrier, check here. |
| 6 | 99 | | if (_barrierParser.TryParse(node, out var barrier)) |
| 0 | 100 | | { |
| 0 | 101 | | _osmBarriers.Add(barrier); |
| 0 | 102 | | } |
| 10 | 103 | | } |
| | 104 | |
|
| | 105 | | /// <inheritdoc /> |
| | 106 | | public override void AddWay(Way way) |
| 6 | 107 | | { |
| 6 | 108 | | if (way.Nodes == null || way.Nodes.Length == 0 || !way.Id.HasValue) return; |
| | 109 | |
|
| 6 | 110 | | if (_firstPass) |
| 3 | 111 | | { |
| | 112 | | // FIRST PASS: keep track of nodes that are used as routing nodes. |
| 3 | 113 | | _vertices[way.Nodes[0]] = VertexId.Empty; |
| 18 | 114 | | for (var i = 0; i < way.Nodes.Length; i++) |
| 6 | 115 | | { |
| 6 | 116 | | var node = way.Nodes[i]; |
| 6 | 117 | | if (_usedNodes.Contains(node)) |
| 0 | 118 | | { |
| 0 | 119 | | _vertices[node] = VertexId.Empty; |
| 0 | 120 | | continue; |
| | 121 | | } |
| | 122 | |
|
| 6 | 123 | | _usedNodes.Add(node); |
| 6 | 124 | | } |
| | 125 | |
|
| 3 | 126 | | _vertices[way.Nodes[^1]] = VertexId.Empty; |
| 3 | 127 | | return; |
| | 128 | | } |
| | 129 | |
|
| | 130 | | // SECOND PASS: keep restricted ways and add edges. |
| | 131 | |
|
| | 132 | | // if way is a member of restriction, queue for later. |
| 3 | 133 | | var saveEdge = _restrictionMembers.ContainsKey(way.Id.Value); |
| 3 | 134 | | if (saveEdge) _restrictionMembers[way.Id.Value] = way; |
| | 135 | |
|
| | 136 | | // process the way into edges. |
| 3 | 137 | | var vertex1 = VertexId.Empty; |
| 3 | 138 | | var vertex1Idx = -1; |
| 3 | 139 | | var shape = new List<(double longitude, double latitude, float? e)>(); |
| 18 | 140 | | for (var n = 0; n < way.Nodes.Length; n++) |
| 6 | 141 | | { |
| 6 | 142 | | var node = way.Nodes[n]; |
| 6 | 143 | | if (!_nodeLocations.TryGetValue(node, out var location)) |
| 0 | 144 | | { |
| | 145 | | // an incomplete way, node not in source. |
| 0 | 146 | | break; |
| | 147 | | } |
| | 148 | |
|
| 6 | 149 | | if (!_vertices.TryGetValue(node, out var vertex2)) |
| 0 | 150 | | { |
| | 151 | | // node is shape. |
| 0 | 152 | | var coordinate = location.AddElevation( |
| 0 | 153 | | elevationHandler: _elevationHandler); |
| 0 | 154 | | shape.Add(coordinate); |
| 0 | 155 | | continue; |
| | 156 | | } |
| | 157 | |
|
| 6 | 158 | | if (vertex2.IsEmpty()) |
| 6 | 159 | | { |
| | 160 | | // node is core and not present yet. |
| 6 | 161 | | var coordinate = location.AddElevation( |
| 6 | 162 | | elevationHandler: _elevationHandler); |
| 6 | 163 | | vertex2 = _mutableRouterDb.AddVertex(coordinate); |
| 6 | 164 | | _vertices[node] = vertex2; |
| 6 | 165 | | } |
| | 166 | |
|
| 6 | 167 | | if (vertex1.IsEmpty()) |
| 3 | 168 | | { |
| 3 | 169 | | vertex1 = vertex2; |
| 3 | 170 | | vertex1Idx = n; |
| 3 | 171 | | continue; |
| | 172 | | } |
| | 173 | |
|
| | 174 | | // add edges. |
| 5 | 175 | | var filteredTags = way.Tags?.Select(x => (x.Key, x.Value)); |
| 3 | 176 | | var edgeId = _mutableRouterDb.AddEdge(vertex1, vertex2, |
| 3 | 177 | | shape, |
| 3 | 178 | | filteredTags); |
| | 179 | |
|
| | 180 | | // check if this edge needs saving for restriction. |
| 3 | 181 | | var edgeIdKey = (way.Id.Value, vertex1Idx, n); |
| 3 | 182 | | if (saveEdge) _restrictedEdges[edgeIdKey] = edgeId; |
| | 183 | |
|
| | 184 | | // move to next part. |
| 3 | 185 | | vertex1 = vertex2; |
| 3 | 186 | | shape.Clear(); |
| 3 | 187 | | } |
| 6 | 188 | | } |
| | 189 | |
|
| | 190 | | /// <inheritdoc /> |
| | 191 | | public override void AddRelation(Relation relation) |
| 0 | 192 | | { |
| 0 | 193 | | if (relation.Members == null || relation.Members.Length == 0) return; |
| 0 | 194 | | if (_firstPass) |
| 0 | 195 | | { |
| 0 | 196 | | if (!_restrictionParser.IsRestriction(relation, out _)) return; |
| | 197 | |
|
| | 198 | | // log member ways. |
| 0 | 199 | | foreach (var relationMember in relation.Members) |
| 0 | 200 | | { |
| 0 | 201 | | if (relationMember.Type != OsmGeoType.Way) continue; |
| | 202 | |
|
| 0 | 203 | | _restrictionMembers[relationMember.Id] = null; |
| 0 | 204 | | } |
| 0 | 205 | | return; |
| | 206 | | } |
| | 207 | |
|
| | 208 | | // try to parse restriction. |
| 0 | 209 | | var result = _restrictionParser.TryParse(relation, |
| 0 | 210 | | k => !_restrictionMembers.TryGetValue(k, out var osmGeo) ? null : osmGeo, |
| 0 | 211 | | out var restriction); |
| | 212 | |
|
| 0 | 213 | | if (result.IsError) return; |
| 0 | 214 | | if (!result.Value) return; |
| 0 | 215 | | if (restriction == null) |
| 0 | 216 | | throw new Exception("restriction parsing was successful but restriction is null"); |
| | 217 | |
|
| 0 | 218 | | var networkRestrictionResult = restriction.ToNetworkRestrictions((long wayId, int startNode, int endNode) => |
| 0 | 219 | | { |
| 0 | 220 | | if (startNode < endNode) |
| 0 | 221 | | { |
| 0 | 222 | | if (!_restrictedEdges.TryGetValue((wayId, startNode, endNode), out var edgeId)) return null; |
| 0 | 223 | |
|
| 0 | 224 | | return (edgeId, true); |
| 0 | 225 | | } |
| 0 | 226 | | else |
| 0 | 227 | | { |
| 0 | 228 | | if (!_restrictedEdges.TryGetValue((wayId, endNode, startNode), out var edgeId)) return null; |
| 0 | 229 | |
|
| 0 | 230 | | return (edgeId, false); |
| 0 | 231 | | } |
| 0 | 232 | | }); |
| 0 | 233 | | if (networkRestrictionResult.IsError) return; |
| | 234 | |
|
| 0 | 235 | | this.AddNetworkRestrictions(networkRestrictionResult.Value); |
| 0 | 236 | | } |
| | 237 | |
|
| | 238 | | private void AddNetworkRestrictions(IEnumerable<NetworkRestriction> networkRestrictions) |
| 3 | 239 | | { |
| 3 | 240 | | var enumerator = _mutableRouterDb.GetEdgeEnumerator(); |
| 9 | 241 | | foreach (var networkRestriction in networkRestrictions) |
| 0 | 242 | | { |
| 0 | 243 | | if (networkRestriction.Count < 2) |
| 0 | 244 | | { |
| | 245 | | // TODO: log something? |
| 0 | 246 | | continue; |
| | 247 | | } |
| | 248 | |
|
| | 249 | | // get last edge and turn cost vertex. |
| 0 | 250 | | var last = networkRestriction[^1]; |
| 0 | 251 | | var lastEdge = _mutableRouterDb.GetEdge(last.edge, last.forward); |
| 0 | 252 | | var turnCostVertex = lastEdge.Tail; |
| | 253 | |
|
| 0 | 254 | | var secondToLast = networkRestriction[^2]; |
| 0 | 255 | | if (networkRestriction.IsProhibitory) |
| 0 | 256 | | { |
| | 257 | | // easy, we only add a single cost. |
| 0 | 258 | | var costs = new uint[,] { { 0, 1 }, { 0, 0 } }; |
| 0 | 259 | | _mutableRouterDb.AddTurnCosts(turnCostVertex, networkRestriction.Attributes, |
| 0 | 260 | | new[] { secondToLast.edge, last.edge }, costs, |
| 0 | 261 | | networkRestriction.Take(networkRestriction.Count - 2).Select(x => x.edge)); |
| 0 | 262 | | } |
| | 263 | | else |
| 0 | 264 | | { |
| | 265 | | // hard, we need to add a cost for every *other* edge than then one in the restriction. |
| 0 | 266 | | enumerator.MoveTo(secondToLast.edge, secondToLast.forward); |
| 0 | 267 | | var to = enumerator.Head; |
| 0 | 268 | | enumerator.MoveTo(to); |
| | 269 | |
|
| 0 | 270 | | while (enumerator.MoveNext()) |
| 0 | 271 | | { |
| 0 | 272 | | if (enumerator.EdgeId == secondToLast.edge || |
| 0 | 273 | | enumerator.EdgeId == lastEdge.EdgeId) continue; |
| | 274 | |
|
| | 275 | | // easy, we only add a single cost. |
| 0 | 276 | | var costs = new uint[,] { { 0, 1 }, { 0, 0 } }; |
| 0 | 277 | | _mutableRouterDb.AddTurnCosts(turnCostVertex, networkRestriction.Attributes, |
| 0 | 278 | | new[] { secondToLast.edge, enumerator.EdgeId }, costs, |
| 0 | 279 | | networkRestriction.Take(networkRestriction.Count - 2).Select(x => x.edge)); |
| 0 | 280 | | } |
| 0 | 281 | | } |
| 0 | 282 | | } |
| 3 | 283 | | } |
| | 284 | | } |