| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using OsmSharp; |
| | 4 | | using OsmSharp.Complete; |
| | 5 | | using OsmSharp.Db; |
| | 6 | | using OsmSharp.Streams.Filters; |
| | 7 | |
|
| | 8 | | namespace 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> |
| | 17 | | internal class CompleteOsmGeoPreprocessor : OsmStreamFilter |
| | 18 | | { |
| | 19 | | private readonly Func<OsmGeo, CompleteOsmGeo?, bool> _action; |
| 3 | 20 | | private readonly Children _children = new(); |
| | 21 | |
|
| 3 | 22 | | public CompleteOsmGeoPreprocessor(Func<OsmGeo, CompleteOsmGeo?, bool> action) |
| 3 | 23 | | { |
| 3 | 24 | | _action = action; |
| 3 | 25 | | } |
| | 26 | |
|
| | 27 | | private OsmGeo? _current; |
| | 28 | |
|
| | 29 | | public override bool MoveNext(bool ignoreNodes, bool ignoreWays, bool ignoreRelations) |
| 20 | 30 | | { |
| 20 | 31 | | _current = null; |
| 25 | 32 | | if (!this.Source.MoveNext(ignoreNodes, ignoreWays, ignoreRelations)) return false; |
| | 33 | |
|
| 15 | 34 | | _current = this.Source.Current(); |
| | 35 | |
|
| | 36 | | // could be a child of another relevant object. |
| 15 | 37 | | _children.AddAsChild(_current); |
| | 38 | |
|
| | 39 | | // if not relevant, take no more actions. |
| 25 | 40 | | if (_current.Type == OsmGeoType.Node) return true; |
| 5 | 41 | | if (!_action(_current, null)) |
| 1 | 42 | | { |
| 1 | 43 | | return true; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | // see if we can complete object, if so, feed to action. |
| 4 | 47 | | var completed = _current.CreateComplete(_children); |
| 4 | 48 | | if (completed is CompleteOsmGeo completeOsmGeo) |
| 2 | 49 | | { |
| 2 | 50 | | _action(_current, completeOsmGeo); |
| 2 | 51 | | } |
| | 52 | |
|
| | 53 | | // register children to keep. |
| 4 | 54 | | switch (_current) |
| | 55 | | { |
| | 56 | | case Way w: |
| 4 | 57 | | if (w.Nodes == null) return true; |
| 28 | 58 | | foreach (var n in w.Nodes) |
| 8 | 59 | | { |
| 8 | 60 | | _children.RegisterAsChild(new OsmGeoKey(OsmGeoType.Node, n)); |
| 8 | 61 | | } |
| | 62 | |
|
| 4 | 63 | | break; |
| | 64 | | case Relation r: |
| 0 | 65 | | foreach (var m in r.Members) |
| 0 | 66 | | { |
| 0 | 67 | | _children.RegisterAsChild(new OsmGeoKey(m.Type, m.Id)); |
| 0 | 68 | | } |
| | 69 | |
|
| 0 | 70 | | break; |
| | 71 | | } |
| | 72 | |
|
| 4 | 73 | | return true; |
| 20 | 74 | | } |
| | 75 | |
|
| | 76 | | public override OsmGeo Current() |
| 15 | 77 | | { |
| 15 | 78 | | if (_current == null) |
| 0 | 79 | | throw new InvalidOperationException( |
| 0 | 80 | | $"Current is null, do {nameof(MoveNext)} first."); |
| | 81 | |
|
| 15 | 82 | | return _current; |
| 15 | 83 | | } |
| | 84 | |
|
| | 85 | | public override void Reset() |
| 5 | 86 | | { |
| 5 | 87 | | this.Source.Reset(); |
| 5 | 88 | | } |
| | 89 | |
|
| 5 | 90 | | public override bool CanReset => this.Source.CanReset; |
| | 91 | |
|
| | 92 | | private class Children : IOsmGeoSource |
| | 93 | | { |
| 3 | 94 | | private readonly Dictionary<OsmGeoKey, OsmGeo?> _children = new(); |
| | 95 | |
|
| | 96 | | public void RegisterAsChild(OsmGeoKey key) |
| 8 | 97 | | { |
| 12 | 98 | | if (_children.ContainsKey(key)) return; |
| | 99 | |
|
| 4 | 100 | | _children[key] = null; |
| 8 | 101 | | } |
| | 102 | |
|
| | 103 | | public void AddAsChild(OsmGeo osmGeo) |
| 15 | 104 | | { |
| 15 | 105 | | var key = new OsmGeoKey(osmGeo); |
| 26 | 106 | | if (!_children.ContainsKey(key)) return; |
| | 107 | |
|
| 4 | 108 | | _children[key] = osmGeo; |
| 15 | 109 | | } |
| | 110 | |
|
| | 111 | | public OsmGeo? Get(OsmGeoType type, long id) |
| 6 | 112 | | { |
| 8 | 113 | | if (!_children.TryGetValue(new OsmGeoKey(type, id), out var c)) return null; |
| | 114 | |
|
| 4 | 115 | | return c; |
| 6 | 116 | | } |
| | 117 | | } |
| | 118 | | } |