| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using Itinero.Network; |
| | 4 | | using Itinero.Network.Enumerators.Edges; |
| | 5 | |
|
| | 6 | | namespace Itinero.IO.Osm.Restrictions.Barriers; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Extension methods for OsmBarrier. |
| | 10 | | /// </summary> |
| | 11 | | public static class OsmBarrierExtensions |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// The signature of a function to get edges for the given nodes pair along the given way. |
| | 15 | | /// </summary> |
| | 16 | | public delegate IEdgeEnumerator GetEdgesFor(long node); |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Converts the given barrier into one or more network restrictions |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="osmBarrier">The OSM barrier.</param> |
| | 22 | | /// <param name="getEdgesFor">A function to get edges for a given node.</param> |
| | 23 | | /// <returns>The restrictions using network edges and vertices.</returns> |
| | 24 | | public static Result<IEnumerable<NetworkRestriction>> ToNetworkRestrictions( |
| | 25 | | this OsmBarrier osmBarrier, |
| | 26 | | GetEdgesFor getEdgesFor) |
| 0 | 27 | | { |
| | 28 | | // get all edges starting at the given node. |
| 0 | 29 | | var edges = new List<(EdgeId edge, bool forward)>(); |
| 0 | 30 | | var enumerator = getEdgesFor(osmBarrier.Node); |
| 0 | 31 | | while (enumerator.MoveNext()) |
| 0 | 32 | | { |
| 0 | 33 | | edges.Add((enumerator.EdgeId, enumerator.Forward)); |
| 0 | 34 | | } |
| | 35 | |
|
| 0 | 36 | | if (edges.Count < 2) return new Result<IEnumerable<NetworkRestriction>>(ArraySegment<NetworkRestriction>.Empty); |
| | 37 | |
|
| | 38 | | // for each two edges create one restriction. |
| 0 | 39 | | var restrictions = new List<NetworkRestriction>(); |
| 0 | 40 | | foreach (var from in edges) |
| 0 | 41 | | foreach (var to in edges) |
| 0 | 42 | | { |
| 0 | 43 | | if (from.edge == to.edge) continue; |
| | 44 | |
|
| 0 | 45 | | restrictions.Add(new NetworkRestriction(new[] { from, to }, true, osmBarrier.Attributes)); |
| 0 | 46 | | } |
| | 47 | |
|
| 0 | 48 | | return new Result<IEnumerable<NetworkRestriction>>(restrictions); |
| 0 | 49 | | } |
| | 50 | | } |