| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using Itinero.Network; |
| | 5 | | using OsmSharp; |
| | 6 | |
|
| | 7 | | namespace Itinero.IO.Osm.Restrictions; |
| | 8 | |
|
| | 9 | | public static class OsmTurnRestrictionExtensions |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// The signature of a function to get edges for the given nodes pair along the given way. |
| | 13 | | /// </summary> |
| | 14 | | public delegate (EdgeId edge, bool forward)? GetEdgeFor(long wayId, int node1Idx, int node2Idx); |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Converts the given OSM turn restriction into one or more sequences on the network. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="osmTurnRestriction">The OSM turn restriction.</param> |
| | 20 | | /// <param name="getEdgeFor">A function to get edges for pairs of nodes for a given way.</param> |
| | 21 | | /// <returns>The restriction using network edges and vertices.</returns> |
| | 22 | | public static Result<IEnumerable<NetworkRestriction>> ToNetworkRestrictions( |
| | 23 | | this OsmTurnRestriction osmTurnRestriction, |
| | 24 | | GetEdgeFor getEdgeFor) |
| 1 | 25 | | { |
| | 26 | | // get from edges. |
| 1 | 27 | | var fromEdges = new List<(EdgeId edge, bool forward)>(); |
| 5 | 28 | | foreach (var hop in osmTurnRestriction.GetFromHops()) |
| 1 | 29 | | { |
| 1 | 30 | | if (hop.IsError) continue; |
| 1 | 31 | | var (way, minStartNode, endNode) = hop.Value; |
| | 32 | |
|
| | 33 | | // get the from-hop from the two nodes and the way id. |
| 1 | 34 | | var edge = getEdgeFor.GetFromHopEdge(way.Id.Value, minStartNode, endNode); |
| 2 | 35 | | if (edge.HasValue) fromEdges.Add(edge.Value); |
| 1 | 36 | | } |
| | 37 | |
|
| 1 | 38 | | if (fromEdges.Count == 0) |
| 0 | 39 | | { |
| 0 | 40 | | return new Result<IEnumerable<NetworkRestriction>>( |
| 0 | 41 | | "could not parse any part of the from-part of the restriction"); |
| | 42 | | } |
| | 43 | |
|
| | 44 | | // get to edges. |
| 1 | 45 | | var toEdges = new List<(EdgeId edge, bool forward)>(); |
| 5 | 46 | | foreach (var hop in osmTurnRestriction.GetToHops()) |
| 1 | 47 | | { |
| 1 | 48 | | if (hop.IsError) continue; |
| 1 | 49 | | var (way, startNode, maxEndNode) = hop.Value; |
| | 50 | |
|
| | 51 | | // get the from-hop from the two nodes and the way id. |
| 1 | 52 | | var edge = getEdgeFor.GetToHopEdge(way.Id.Value, startNode, maxEndNode); |
| 2 | 53 | | if (edge.HasValue) toEdges.Add(edge.Value); |
| 1 | 54 | | } |
| | 55 | |
|
| 1 | 56 | | if (toEdges.Count == 0) |
| 0 | 57 | | { |
| 0 | 58 | | return new Result<IEnumerable<NetworkRestriction>>( |
| 0 | 59 | | "could not parse any part of the to-part of the restriction"); |
| | 60 | | } |
| | 61 | |
|
| | 62 | | // get the in between sequence. |
| 1 | 63 | | var viaEdges = new List<(EdgeId edgeId, bool forward)>(); |
| 1 | 64 | | var viaSequences = osmTurnRestriction.GetViaHops(); |
| 1 | 65 | | if (viaSequences.IsError) return viaSequences.ConvertError<IEnumerable<NetworkRestriction>>(); |
| 3 | 66 | | foreach (var hop in viaSequences.Value) |
| 0 | 67 | | { |
| 0 | 68 | | var (way, startNode, endNode) = hop; |
| | 69 | |
|
| | 70 | | // get the from-hop from the two nodes and the way id. |
| 0 | 71 | | var edge = getEdgeFor.GetViaHopEdges(way.Id.Value, startNode, endNode); |
| 0 | 72 | | viaEdges.AddRange(edge); |
| 0 | 73 | | } |
| | 74 | |
|
| 1 | 75 | | var networkRestrictions = new List<NetworkRestriction>(); |
| 5 | 76 | | foreach (var fromEdge in fromEdges) |
| 5 | 77 | | foreach (var toEdge in toEdges) |
| 1 | 78 | | { |
| 1 | 79 | | var sequence = new List<(EdgeId edgeId, bool forward)> { fromEdge }; |
| 1 | 80 | | sequence.AddRange(viaEdges); |
| 1 | 81 | | sequence.Add(toEdge); |
| | 82 | |
|
| 1 | 83 | | networkRestrictions.Add(new NetworkRestriction(sequence, osmTurnRestriction.IsProbibitory, |
| 1 | 84 | | osmTurnRestriction.Attributes)); |
| 1 | 85 | | } |
| | 86 | |
|
| 1 | 87 | | return networkRestrictions; |
| 1 | 88 | | } |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Gets the node that connects the from ways to the via way(s) or nodes. |
| | 92 | | /// </summary> |
| | 93 | | /// <param name="osmTurnRestriction">The turn restriction.</param> |
| | 94 | | /// <returns>The node where the from ways end.</returns> |
| | 95 | | public static Result<long> GetViaFrom(this OsmTurnRestriction osmTurnRestriction) |
| 7 | 96 | | { |
| | 97 | | // assume the restriction has a via-node like most of them. |
| 7 | 98 | | var node = osmTurnRestriction.ViaNodeId; |
| 12 | 99 | | if (node != null) return node.Value; |
| | 100 | |
|
| | 101 | | // but some have via-ways, so get the node from the first via-way. |
| 2 | 102 | | var viaWay = osmTurnRestriction.Via.FirstOrDefault(); |
| 2 | 103 | | if (viaWay == null) return new Result<long>("no via node or via way found"); |
| 8 | 104 | | foreach (var fromWay in osmTurnRestriction.From) |
| 2 | 105 | | { |
| 2 | 106 | | if (fromWay.Nodes[^1] == viaWay.Nodes[0] || |
| 2 | 107 | | fromWay.Nodes[^1] == viaWay.Nodes[^1]) |
| 2 | 108 | | { |
| 2 | 109 | | return fromWay.Nodes[^1]; |
| | 110 | | } |
| | 111 | |
|
| 0 | 112 | | if (fromWay.Nodes[0] == viaWay.Nodes[0] || |
| 0 | 113 | | fromWay.Nodes[0] == viaWay.Nodes[^1]) |
| 0 | 114 | | { |
| 0 | 115 | | return fromWay.Nodes[0]; |
| | 116 | | } |
| 0 | 117 | | } |
| | 118 | |
|
| 0 | 119 | | return new Result<long>("no via node found to end the from ways"); |
| 7 | 120 | | } |
| | 121 | |
|
| | 122 | | /// <summary> |
| | 123 | | /// Enumerates from ways and their nodes in the direction of the restricted sequence. |
| | 124 | | /// </summary> |
| | 125 | | /// <param name="osmTurnRestriction">The turn restriction.</param> |
| | 126 | | /// <returns>The from ways and for each the start node index where the sequence starts at the earliest and the end n |
| | 127 | | public static IEnumerable<Result<(Way way, int minStartNode, int endNode)>> GetFromHops( |
| | 128 | | this OsmTurnRestriction osmTurnRestriction) |
| 3 | 129 | | { |
| 3 | 130 | | var node = osmTurnRestriction.GetViaFrom(); |
| | 131 | |
|
| 3 | 132 | | var fromWayRestriction = new List<Result<(Way way, int minStartNode, int endNode)>>(); |
| 15 | 133 | | foreach (var fromWay in osmTurnRestriction.From) |
| 3 | 134 | | { |
| 3 | 135 | | if (node.IsError) |
| 0 | 136 | | { |
| 0 | 137 | | fromWayRestriction.Add(node.ConvertError<(Way way, int minStartNode, int endNode)>()); |
| 0 | 138 | | continue; |
| | 139 | | } |
| | 140 | |
|
| 3 | 141 | | if (fromWay.Nodes[^1] == node) |
| 2 | 142 | | { |
| 2 | 143 | | fromWayRestriction.Add((fromWay, 0, fromWay.Nodes.Length - 1)); |
| 2 | 144 | | continue; |
| | 145 | | } |
| | 146 | |
|
| 1 | 147 | | if (fromWay.Nodes[0] == node) |
| 1 | 148 | | { |
| 1 | 149 | | fromWayRestriction.Add((fromWay, fromWay.Nodes.Length - 1, 0)); |
| 1 | 150 | | } |
| 1 | 151 | | } |
| | 152 | |
|
| 3 | 153 | | return fromWayRestriction; |
| 3 | 154 | | } |
| | 155 | |
|
| | 156 | | /// <summary> |
| | 157 | | /// Gets the node that connects the to ways to the via way(s) or nodes. |
| | 158 | | /// </summary> |
| | 159 | | /// <param name="osmTurnRestriction">The turn restriction.</param> |
| | 160 | | /// <returns>The node where the to ways begin.</returns> |
| | 161 | | public static Result<long> GetViaTo(this OsmTurnRestriction osmTurnRestriction) |
| 4 | 162 | | { |
| | 163 | | // assume the restriction has a via-node like most of them. |
| 4 | 164 | | var node = osmTurnRestriction.ViaNodeId; |
| 6 | 165 | | if (node != null) return node.Value; |
| | 166 | |
|
| | 167 | | // but some have via-ways, so get the node from the first via-way. |
| 2 | 168 | | var viaWay = osmTurnRestriction.Via.FirstOrDefault(); |
| 2 | 169 | | if (viaWay == null) return new Result<long>("no via node or via way found"); |
| 8 | 170 | | foreach (var toWay in osmTurnRestriction.To) |
| 2 | 171 | | { |
| 2 | 172 | | if (toWay.Nodes[^1] == viaWay.Nodes[0] || |
| 2 | 173 | | toWay.Nodes[^1] == viaWay.Nodes[^1]) |
| 0 | 174 | | { |
| 0 | 175 | | return toWay.Nodes[^1]; |
| | 176 | | } |
| | 177 | |
|
| 2 | 178 | | if (toWay.Nodes[0] == viaWay.Nodes[0] || |
| 2 | 179 | | toWay.Nodes[0] == viaWay.Nodes[^1]) |
| 2 | 180 | | { |
| 2 | 181 | | return toWay.Nodes[0]; |
| | 182 | | } |
| 0 | 183 | | } |
| | 184 | |
|
| 0 | 185 | | return new Result<long>("no via node found to start the to ways"); |
| 4 | 186 | | } |
| | 187 | |
|
| | 188 | | /// <summary> |
| | 189 | | /// Enumerates to ways and their nodes in the direction of the restricted sequence. |
| | 190 | | /// </summary> |
| | 191 | | /// <param name="osmTurnRestriction">The turn restriction.</param> |
| | 192 | | /// <returns>The to ways and for each the node where the to sequence starts and where it ends at the latest.</return |
| | 193 | | public static IEnumerable<Result<(Way way, int startNode, int maxEndNode)>> GetToHops( |
| | 194 | | this OsmTurnRestriction osmTurnRestriction) |
| 1 | 195 | | { |
| 1 | 196 | | var node = osmTurnRestriction.GetViaTo(); |
| | 197 | |
|
| | 198 | | // assume the restriction has a via-node like most of them. |
| 1 | 199 | | var toWayResults = new List<Result<(Way way, int startNode, int maxEndNode)>>(); |
| 5 | 200 | | foreach (var toWay in osmTurnRestriction.To) |
| 1 | 201 | | { |
| 1 | 202 | | if (node.IsError) |
| 0 | 203 | | { |
| 0 | 204 | | toWayResults.Add(node.ConvertError<(Way way, int startNode, int endNode)>()); |
| 0 | 205 | | continue; |
| | 206 | | } |
| | 207 | |
|
| 1 | 208 | | if (toWay.Nodes[0] == node) |
| 1 | 209 | | { |
| 1 | 210 | | toWayResults.Add((toWay, 0, toWay.Nodes.Length - 1)); |
| 1 | 211 | | continue; |
| | 212 | | } |
| | 213 | |
|
| 0 | 214 | | if (toWay.Nodes[^1] == node) |
| 0 | 215 | | { |
| 0 | 216 | | toWayResults.Add((toWay, toWay.Nodes.Length - 1, 0)); |
| 0 | 217 | | } |
| 0 | 218 | | } |
| | 219 | |
|
| 1 | 220 | | return toWayResults; |
| 1 | 221 | | } |
| | 222 | |
|
| | 223 | | /// <summary> |
| | 224 | | /// Gets the via way segments. |
| | 225 | | /// </summary> |
| | 226 | | /// <param name="osmTurnRestriction">The OSM turn restrictions.</param> |
| | 227 | | /// <returns>The ways and the two node indexes representing the via part.</returns> |
| | 228 | | public static Result<IReadOnlyList<(Way via, int startNode, int endNode)>> GetViaHops( |
| | 229 | | this OsmTurnRestriction osmTurnRestriction) |
| 1 | 230 | | { |
| 1 | 231 | | var node1 = osmTurnRestriction.GetViaFrom(); |
| 1 | 232 | | if (node1.IsError) return node1.ConvertError<IReadOnlyList<(Way via, int node1Idx, int node2Idx)>>(); |
| 1 | 233 | | var node2 = osmTurnRestriction.GetViaTo(); |
| 1 | 234 | | if (node2.IsError) return node2.ConvertError<IReadOnlyList<(Way via, int node1Idx, int node2Idx)>>(); |
| | 235 | |
|
| 1 | 236 | | if (node1.Value == node2.Value) |
| 1 | 237 | | { |
| | 238 | | // the most default case, one via node. |
| 1 | 239 | | return Array.Empty<(Way via, int node1Idx, int node2Idx)>(); |
| | 240 | | } |
| | 241 | |
|
| | 242 | | // there have to be via ways at this point. |
| | 243 | | // it is assumed ways are split to follow along the sequence. |
| 0 | 244 | | var currentNode = node1.Value; |
| 0 | 245 | | var sequences = new List<(Way via, int node1Idx, int node2Idx)>(); |
| 0 | 246 | | foreach (var viaWay in osmTurnRestriction.Via) |
| 0 | 247 | | { |
| 0 | 248 | | if (viaWay.Nodes[0] == currentNode) |
| 0 | 249 | | { |
| 0 | 250 | | sequences.Add((viaWay, 0, viaWay.Nodes.Length - 1)); |
| 0 | 251 | | currentNode = viaWay.Nodes[^1]; |
| 0 | 252 | | } |
| 0 | 253 | | else if (viaWay.Nodes[^1] == currentNode) |
| 0 | 254 | | { |
| 0 | 255 | | sequences.Add((viaWay, viaWay.Nodes.Length - 1, 0)); |
| 0 | 256 | | currentNode = viaWay.Nodes[0]; |
| 0 | 257 | | } |
| | 258 | | else |
| 0 | 259 | | { |
| 0 | 260 | | return new Result<IReadOnlyList<(Way via, int startNode, int endNode)>>( |
| 0 | 261 | | "one of via ways does not fit in a sequence"); |
| | 262 | | } |
| 0 | 263 | | } |
| | 264 | |
|
| 0 | 265 | | if (currentNode != node2) |
| 0 | 266 | | return new Result<IReadOnlyList<(Way via, int startNode, int endNode)>>( |
| 0 | 267 | | "last node of via sequence does not match"); |
| | 268 | |
|
| 0 | 269 | | return sequences; |
| 1 | 270 | | } |
| | 271 | |
|
| | 272 | | private static (EdgeId edge, bool forward)? GetFromHopEdge(this GetEdgeFor getEdgeFor, long wayId, int minStartNode, |
| 1 | 273 | | { |
| 1 | 274 | | if (minStartNode < endNode) |
| 1 | 275 | | { |
| 2 | 276 | | for (var n = endNode - 1; n >= minStartNode; n--) |
| 1 | 277 | | { |
| 1 | 278 | | var edge = getEdgeFor(wayId, n, endNode); |
| 2 | 279 | | if (edge != null) return edge; |
| 0 | 280 | | } |
| 0 | 281 | | } |
| | 282 | | else |
| 0 | 283 | | { |
| 0 | 284 | | for (var n = endNode + 1; n <= minStartNode; n++) |
| 0 | 285 | | { |
| 0 | 286 | | var edge = getEdgeFor(wayId, n, endNode); |
| 0 | 287 | | if (edge != null) return edge; |
| 0 | 288 | | } |
| 0 | 289 | | } |
| | 290 | |
|
| 0 | 291 | | return null; |
| 1 | 292 | | } |
| | 293 | |
|
| | 294 | | private static IEnumerable<(EdgeId edge, bool forward)> GetViaHopEdges(this GetEdgeFor getEdgeFor, long wayId, |
| | 295 | | int startNode, int endNode) |
| 0 | 296 | | { |
| 0 | 297 | | if (startNode < endNode) |
| 0 | 298 | | { |
| 0 | 299 | | for (var n = startNode + 1; n <= endNode; n++) |
| 0 | 300 | | { |
| 0 | 301 | | var edge = getEdgeFor(wayId, startNode, n); |
| 0 | 302 | | if (edge == null) continue; |
| | 303 | |
|
| 0 | 304 | | startNode = n; |
| 0 | 305 | | yield return edge.Value; |
| 0 | 306 | | } |
| 0 | 307 | | } |
| | 308 | | else |
| 0 | 309 | | { |
| 0 | 310 | | for (var n = startNode - 1; n >= endNode; n--) |
| 0 | 311 | | { |
| 0 | 312 | | var edge = getEdgeFor(wayId, startNode, n); |
| 0 | 313 | | if (edge == null) continue; |
| | 314 | |
|
| 0 | 315 | | startNode = n; |
| 0 | 316 | | yield return edge.Value; |
| 0 | 317 | | } |
| 0 | 318 | | } |
| 0 | 319 | | } |
| | 320 | |
|
| | 321 | | private static (EdgeId edge, bool forward)? GetToHopEdge(this GetEdgeFor getEdgeFor, long wayId, int startNode, int |
| 1 | 322 | | { |
| 1 | 323 | | if (startNode < maxEndNode) |
| 1 | 324 | | { |
| 2 | 325 | | for (var n = startNode + 1; n <= maxEndNode; n++) |
| 1 | 326 | | { |
| 1 | 327 | | var edge = getEdgeFor(wayId, startNode, n); |
| 2 | 328 | | if (edge != null) return edge; |
| 0 | 329 | | } |
| 0 | 330 | | } |
| | 331 | | else |
| 0 | 332 | | { |
| 0 | 333 | | for (var n = startNode - 1; n >= maxEndNode; n--) |
| 0 | 334 | | { |
| 0 | 335 | | var edge = getEdgeFor(wayId, startNode, n); |
| 0 | 336 | | if (edge != null) return edge; |
| 0 | 337 | | } |
| 0 | 338 | | } |
| | 339 | |
|
| 0 | 340 | | return null; |
| 1 | 341 | | } |
| | 342 | | } |