< Summary

Class:Itinero.IO.Osm.Streams.CompleteOsmGeoPreprocessor
Assembly:Itinero.IO.Osm
File(s):/home/runner/work/routing2/routing2/src/Itinero.IO.Osm/Streams/CompleteOsmGeoPreprocessor.cs
Covered lines:50
Uncovered lines:7
Coverable lines:57
Total lines:118
Line coverage:87.7% (50 of 57)
Covered branches:21
Total branches:26
Branch coverage:80.7% (21 of 26)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
MoveNext(...)77.77%1882.14%
Current()50%266.66%
Reset()100%1100%
get_CanReset()100%1100%
.ctor()100%1100%
RegisterAsChild(...)100%2100%
AddAsChild(...)100%2100%
Get(...)100%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using OsmSharp;
 4using OsmSharp.Complete;
 5using OsmSharp.Db;
 6using OsmSharp.Streams.Filters;
 7
 8namespace Itinero.IO.Osm.Streams;
 9
 10/// <summary>
 11/// A complete OSM preprocessor.
 12/// </summary>
 13/// <remarks>
 14/// A preprocessor that allows update tags based using the complete version of OSM geo objects. This can be used for exa
 15/// - Add tags based on geometry of ways: for example length.
 16/// </remarks>
 17internal class CompleteOsmGeoPreprocessor : OsmStreamFilter
 18{
 19    private readonly Func<OsmGeo, CompleteOsmGeo?, bool> _action;
 320    private readonly Children _children = new();
 21
 322    public CompleteOsmGeoPreprocessor(Func<OsmGeo, CompleteOsmGeo?, bool> action)
 323    {
 324        _action = action;
 325    }
 26
 27    private OsmGeo? _current;
 28
 29    public override bool MoveNext(bool ignoreNodes, bool ignoreWays, bool ignoreRelations)
 2030    {
 2031        _current = null;
 2532        if (!this.Source.MoveNext(ignoreNodes, ignoreWays, ignoreRelations)) return false;
 33
 1534        _current = this.Source.Current();
 35
 36        // could be a child of another relevant object.
 1537        _children.AddAsChild(_current);
 38
 39        // if not relevant, take no more actions.
 2540        if (_current.Type == OsmGeoType.Node) return true;
 541        if (!_action(_current, null))
 142        {
 143            return true;
 44        }
 45
 46        // see if we can complete object, if so, feed to action.
 447        var completed = _current.CreateComplete(_children);
 448        if (completed is CompleteOsmGeo completeOsmGeo)
 249        {
 250            _action(_current, completeOsmGeo);
 251        }
 52
 53        // register children to keep.
 454        switch (_current)
 55        {
 56            case Way w:
 457                if (w.Nodes == null) return true;
 2858                foreach (var n in w.Nodes)
 859                {
 860                    _children.RegisterAsChild(new OsmGeoKey(OsmGeoType.Node, n));
 861                }
 62
 463                break;
 64            case Relation r:
 065                foreach (var m in r.Members)
 066                {
 067                    _children.RegisterAsChild(new OsmGeoKey(m.Type, m.Id));
 068                }
 69
 070                break;
 71        }
 72
 473        return true;
 2074    }
 75
 76    public override OsmGeo Current()
 1577    {
 1578        if (_current == null)
 079            throw new InvalidOperationException(
 080                $"Current is null, do {nameof(MoveNext)} first.");
 81
 1582        return _current;
 1583    }
 84
 85    public override void Reset()
 586    {
 587        this.Source.Reset();
 588    }
 89
 590    public override bool CanReset => this.Source.CanReset;
 91
 92    private class Children : IOsmGeoSource
 93    {
 394        private readonly Dictionary<OsmGeoKey, OsmGeo?> _children = new();
 95
 96        public void RegisterAsChild(OsmGeoKey key)
 897        {
 1298            if (_children.ContainsKey(key)) return;
 99
 4100            _children[key] = null;
 8101        }
 102
 103        public void AddAsChild(OsmGeo osmGeo)
 15104        {
 15105            var key = new OsmGeoKey(osmGeo);
 26106            if (!_children.ContainsKey(key)) return;
 107
 4108            _children[key] = osmGeo;
 15109        }
 110
 111        public OsmGeo? Get(OsmGeoType type, long id)
 6112        {
 8113            if (!_children.TryGetValue(new OsmGeoKey(type, id), out var c)) return null;
 114
 4115            return c;
 6116        }
 117    }
 118}