| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics.CodeAnalysis; |
| | | 4 | | using System.Linq; |
| | | 5 | | using OsmSharp; |
| | | 6 | | |
| | | 7 | | namespace Itinero.IO.Osm.Restrictions.Barriers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Parses OSM barriers. |
| | | 11 | | /// </summary> |
| | | 12 | | public class OsmBarrierParser |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Returns true if the node is a barrier. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="node">The node.</param> |
| | | 18 | | /// <returns>True if the node is a barrier, false otherwise.</returns> |
| | | 19 | | public bool IsBarrier(Node node) |
| | 21 | 20 | | { |
| | 21 | 21 | | return node.Tags != null && node.Tags.ContainsKey("barrier"); |
| | 21 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Tries to parse a barrier. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="node">The node.</param> |
| | | 28 | | /// <param name="barrier">The barrier, if any.</param> |
| | | 29 | | /// <returns>True if parsing succeeded.</returns> |
| | | 30 | | public bool TryParse(Node node, [NotNullWhen(returnValue: true)] out OsmBarrier? barrier) |
| | 12 | 31 | | { |
| | 12 | 32 | | if (node.Id == null) throw new ArgumentException("Node with id null cannot be a barrier"); |
| | | 33 | | |
| | 12 | 34 | | barrier = null; |
| | | 35 | | |
| | 24 | 36 | | if (!this.IsBarrier(node)) return false; |
| | | 37 | | |
| | 0 | 38 | | barrier = OsmBarrier.Create(node.Id.Value, node.Tags.Select(t => (t.Key, t.Value)).ToList()); |
| | | 39 | | |
| | 0 | 40 | | return true; |
| | 12 | 41 | | } |
| | | 42 | | } |