| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using Itinero.Network.Tiles.Standalone.Global; |
| | | 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 | | /// Converts the given barrier into two or more global network restrictions. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="osmBarrier">The OSM barrier.</param> |
| | | 17 | | /// <returns>The restrictions using network edges and vertices.</returns> |
| | | 18 | | public static IEnumerable<GlobalRestriction> ToGlobalNetworkRestrictions( |
| | | 19 | | this OsmBarrier osmBarrier) |
| | 2378 | 20 | | { |
| | 8544 | 21 | | var attributes = osmBarrier.Node.Tags?.Select(tag => (tag.Key, tag.Value)).ToArray() ?? |
| | 2378 | 22 | | ArraySegment<(string key, string value)>.Empty; |
| | | 23 | | |
| | 16180 | 24 | | foreach (var tailHop in osmBarrier.GetTailHops()) |
| | 32027 | 25 | | foreach (var otherHop in osmBarrier.GetTailHops()) |
| | 9229 | 26 | | { |
| | 13752 | 27 | | if (tailHop == otherHop) continue; |
| | | 28 | | |
| | 4706 | 29 | | var headHop = otherHop.GetInverted(); |
| | | 30 | | |
| | 4706 | 31 | | yield return new GlobalRestriction([tailHop, headHop], true, attributes); |
| | 4706 | 32 | | } |
| | 2378 | 33 | | } |
| | | 34 | | |
| | | 35 | | private static IEnumerable<GlobalEdgeId> GetTailHops( |
| | | 36 | | this OsmBarrier osmBarrier) |
| | 6901 | 37 | | { |
| | 6901 | 38 | | var node = osmBarrier.Node.Id!.Value; |
| | | 39 | | |
| | 42321 | 40 | | foreach (var fromWay in osmBarrier.Ways) |
| | 10809 | 41 | | { |
| | 10809 | 42 | | var previous = 0; |
| | 107492 | 43 | | for (var n = 1; n < fromWay.Nodes.Length; n++) |
| | 42937 | 44 | | { |
| | 42937 | 45 | | var current = fromWay.Nodes[n]; |
| | 78974 | 46 | | if (current != node) continue; |
| | 6900 | 47 | | if (n == previous) continue; |
| | | 48 | | |
| | 6900 | 49 | | yield return GlobalEdgeId.Create(fromWay.Id!.Value, tail: previous, head: n); |
| | 6900 | 50 | | previous = n; |
| | 6900 | 51 | | } |
| | | 52 | | |
| | 10809 | 53 | | previous = fromWay.Nodes.Length - 1; |
| | 107492 | 54 | | for (var n = fromWay.Nodes.Length - 2; n >= 0; n--) |
| | 42937 | 55 | | { |
| | 42937 | 56 | | var current = fromWay.Nodes[n]; |
| | 79022 | 57 | | if (current != node) continue; |
| | 6852 | 58 | | if (n == previous) continue; |
| | | 59 | | |
| | 6852 | 60 | | yield return GlobalEdgeId.Create(fromWay.Id!.Value, tail: previous, head: n); |
| | 6852 | 61 | | previous = n; |
| | 6852 | 62 | | } |
| | 10809 | 63 | | } |
| | 6901 | 64 | | } |
| | | 65 | | } |