< Summary

Class:Itinero.IO.Osm.Streams.RelationTagsPreprocessor
Assembly:Itinero.IO.Osm
File(s):/home/runner/work/routing2/routing2/src/Itinero.IO.Osm/Streams/RelationTagsPreprocessor.cs
Covered lines:48
Uncovered lines:2
Coverable lines:50
Total lines:98
Line coverage:96% (48 of 50)
Covered branches:16
Total branches:18
Branch coverage:88.8% (16 of 18)
Tag:251_23667616543

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
MoveNext(...)93.75%16100%
Current()50%266.66%
Reset()100%1100%
get_CanReset()100%1100%
get_Relation()100%1100%
get_Next()100%1100%

File(s)

/home/runner/work/routing2/routing2/src/Itinero.IO.Osm/Streams/RelationTagsPreprocessor.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using OsmSharp;
 4using OsmSharp.Db;
 5using OsmSharp.Streams.Filters;
 6
 7namespace 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>
 15internal 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
 2222    public RelationTagsPreprocessor(Func<Relation, OsmGeo?, bool> action)
 2223    {
 2224        _action = action;
 25
 2226        _parentRelations = new Dictionary<OsmGeoKey, RelationLinked>();
 2227    }
 28
 29    public override bool MoveNext(bool ignoreNodes, bool ignoreWays, bool ignoreRelations)
 72868930    {
 72868931        _current = null;
 72873232        if (!this.Source.MoveNext(ignoreNodes, ignoreWays, ignoreRelations)) return false;
 33
 72864634        _current = this.Source.Current();
 35
 36        // register parent for all member if relevant relation.
 72864637        switch (_current)
 38        {
 39            case Relation r:
 40                // if not relevant, take no more actions.
 516441                if (_action(r, null))
 26042                {
 6686043                    foreach (var m in r.Members ?? ArraySegment<RelationMember>.Empty)
 3304044                    {
 3304045                        var key = new OsmGeoKey(m.Type, m.Id);
 3304046                        if (!_parentRelations.TryGetValue(key, out var e))
 1593047                        {
 1593048                            e = null;
 1593049                        }
 50
 3304051                        _parentRelations[key] = new RelationLinked()
 3304052                        {
 3304053                            Relation = r,
 3304054                            Next = e
 3304055                        };
 3304056                    }
 26057                }
 516458                break;
 59        }
 60
 61        // check if this is a child.
 72864662        var currentKey = new OsmGeoKey(_current);
 72864663        if (!_parentRelations.TryGetValue(currentKey, out var parent))
 72744664        {
 72744665            return true;
 66        }
 276467        while (parent != null)
 156468        {
 156469            _action(parent.Relation, _current);
 156470            parent = parent.Next;
 156471        }
 72
 120073        return true;
 72868974    }
 75
 76    public override OsmGeo Current()
 72864677    {
 72864678        if (_current == null)
 079            throw new InvalidOperationException(
 080                $"Current is null, do {nameof(MoveNext)} first.");
 81
 72864682        return _current;
 72864683    }
 84
 85    public override void Reset()
 2486    {
 2487        this.Source.Reset();
 2488    }
 89
 590    public override bool CanReset => this.Source.CanReset;
 91
 92    private class RelationLinked
 93    {
 6764494        public Relation Relation { get; set; } = null!;
 95
 3460496        public RelationLinked? Next { get; set; }
 97    }
 98}