| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using OsmSharp; |
| | 4 | | using OsmSharp.Db; |
| | 5 | | using OsmSharp.Streams.Filters; |
| | 6 | |
|
| | 7 | | namespace Itinero.IO.Osm.Streams; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// A relation tags preprocessor. |
| | 11 | | /// </summary> |
| | 12 | | /// <remarks> |
| | 13 | | /// Allows to add tags to relation members based on their membership. |
| | 14 | | /// </remarks> |
| | 15 | | internal class RelationTagsPreprocessor : OsmStreamFilter |
| | 16 | | { |
| | 17 | | private readonly Func<Relation, OsmGeo?, bool> _action; |
| | 18 | | private readonly Dictionary<OsmGeoKey, RelationLinked> _parentRelations; |
| | 19 | |
|
| | 20 | | private OsmGeo? _current; |
| | 21 | |
|
| 6 | 22 | | public RelationTagsPreprocessor(Func<Relation, OsmGeo?, bool> action) |
| 6 | 23 | | { |
| 6 | 24 | | _action = action; |
| | 25 | |
|
| 6 | 26 | | _parentRelations = new Dictionary<OsmGeoKey, RelationLinked>(); |
| 6 | 27 | | } |
| | 28 | |
|
| | 29 | | public override bool MoveNext(bool ignoreNodes, bool ignoreWays, bool ignoreRelations) |
| 46 | 30 | | { |
| 46 | 31 | | _current = null; |
| 57 | 32 | | if (!this.Source.MoveNext(ignoreNodes, ignoreWays, ignoreRelations)) return false; |
| | 33 | |
|
| 35 | 34 | | _current = this.Source.Current(); |
| | 35 | |
|
| | 36 | | // register parent for all member if relevant relation. |
| 35 | 37 | | switch (_current) |
| | 38 | | { |
| | 39 | | case Relation r: |
| | 40 | | // if not relevant, take no more actions. |
| 6 | 41 | | if (_action(r, null)) |
| 4 | 42 | | { |
| 20 | 43 | | foreach (var m in r.Members ?? ArraySegment<RelationMember>.Empty) |
| 4 | 44 | | { |
| 4 | 45 | | var key = new OsmGeoKey(m.Type, m.Id); |
| 4 | 46 | | if (!_parentRelations.TryGetValue(key, out var e)) |
| 2 | 47 | | { |
| 2 | 48 | | e = null; |
| 2 | 49 | | } |
| | 50 | |
|
| 4 | 51 | | _parentRelations[key] = new RelationLinked() |
| 4 | 52 | | { |
| 4 | 53 | | Relation = r, |
| 4 | 54 | | Next = e |
| 4 | 55 | | }; |
| 4 | 56 | | } |
| 4 | 57 | | } |
| 6 | 58 | | break; |
| | 59 | | } |
| | 60 | |
|
| | 61 | | // check if this is a child. |
| 35 | 62 | | var currentKey = new OsmGeoKey(_current); |
| 35 | 63 | | if (!_parentRelations.TryGetValue(currentKey, out var parent)) |
| 33 | 64 | | { |
| 33 | 65 | | return true; |
| | 66 | | } |
| 4 | 67 | | while (parent != null) |
| 2 | 68 | | { |
| 2 | 69 | | _action(parent.Relation, _current); |
| 2 | 70 | | parent = parent.Next; |
| 2 | 71 | | } |
| | 72 | |
|
| 2 | 73 | | return true; |
| 46 | 74 | | } |
| | 75 | |
|
| | 76 | | public override OsmGeo Current() |
| 35 | 77 | | { |
| 35 | 78 | | if (_current == null) |
| 0 | 79 | | throw new InvalidOperationException( |
| 0 | 80 | | $"Current is null, do {nameof(MoveNext)} first."); |
| | 81 | |
|
| 35 | 82 | | return _current; |
| 35 | 83 | | } |
| | 84 | |
|
| | 85 | | public override void Reset() |
| 11 | 86 | | { |
| 11 | 87 | | this.Source.Reset(); |
| 11 | 88 | | } |
| | 89 | |
|
| 8 | 90 | | public override bool CanReset => this.Source.CanReset; |
| | 91 | |
|
| | 92 | | private class RelationLinked |
| | 93 | | { |
| 10 | 94 | | public Relation Relation { get; set; } = null!; |
| | 95 | |
|
| 6 | 96 | | public RelationLinked? Next { get; set; } |
| | 97 | | } |
| | 98 | | } |