< 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:224_14471318300

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
 622    public RelationTagsPreprocessor(Func<Relation, OsmGeo?, bool> action)
 623    {
 624        _action = action;
 25
 626        _parentRelations = new Dictionary<OsmGeoKey, RelationLinked>();
 627    }
 28
 29    public override bool MoveNext(bool ignoreNodes, bool ignoreWays, bool ignoreRelations)
 4630    {
 4631        _current = null;
 5732        if (!this.Source.MoveNext(ignoreNodes, ignoreWays, ignoreRelations)) return false;
 33
 3534        _current = this.Source.Current();
 35
 36        // register parent for all member if relevant relation.
 3537        switch (_current)
 38        {
 39            case Relation r:
 40                // if not relevant, take no more actions.
 641                if (_action(r, null))
 442                {
 2043                    foreach (var m in r.Members ?? ArraySegment<RelationMember>.Empty)
 444                    {
 445                        var key = new OsmGeoKey(m.Type, m.Id);
 446                        if (!_parentRelations.TryGetValue(key, out var e))
 247                        {
 248                            e = null;
 249                        }
 50
 451                        _parentRelations[key] = new RelationLinked()
 452                        {
 453                            Relation = r,
 454                            Next = e
 455                        };
 456                    }
 457                }
 658                break;
 59        }
 60
 61        // check if this is a child.
 3562        var currentKey = new OsmGeoKey(_current);
 3563        if (!_parentRelations.TryGetValue(currentKey, out var parent))
 3364        {
 3365            return true;
 66        }
 467        while (parent != null)
 268        {
 269            _action(parent.Relation, _current);
 270            parent = parent.Next;
 271        }
 72
 273        return true;
 4674    }
 75
 76    public override OsmGeo Current()
 3577    {
 3578        if (_current == null)
 079            throw new InvalidOperationException(
 080                $"Current is null, do {nameof(MoveNext)} first.");
 81
 3582        return _current;
 3583    }
 84
 85    public override void Reset()
 1186    {
 1187        this.Source.Reset();
 1188    }
 89
 890    public override bool CanReset => this.Source.CanReset;
 91
 92    private class RelationLinked
 93    {
 1094        public Relation Relation { get; set; } = null!;
 95
 696        public RelationLinked? Next { get; set; }
 97    }
 98}