diff --git a/SharedAssemblyVersion.cs b/SharedAssemblyVersion.cs index 85709b93..54310b49 100755 --- a/SharedAssemblyVersion.cs +++ b/SharedAssemblyVersion.cs @@ -1,4 +1,4 @@ using System.Reflection; [assembly: AssemblyVersion("7.0.0")] -[assembly: AssemblyInformationalVersion("7.0.0-pre008")] +[assembly: AssemblyInformationalVersion("7.0.0-pre009")] diff --git a/src/OsmSharp/Streams/Filters/OsmStreamFilterMerge.cs b/src/OsmSharp/Streams/Filters/OsmStreamFilterMerge.cs index f3971446..b2930903 100755 --- a/src/OsmSharp/Streams/Filters/OsmStreamFilterMerge.cs +++ b/src/OsmSharp/Streams/Filters/OsmStreamFilterMerge.cs @@ -56,7 +56,20 @@ public override bool CanReset { get { - return _source1.CanReset && _source2.CanReset; + if (_source1 != null && _source2 != null) + { + return _source1.CanReset && _source2.CanReset; + } + else if (_source1 != null) + { + return _source1.CanReset; + } + else if(_source2 != null) + { + return _source2.CanReset; + } + + return true; // a stream can be reset when there is no data. } } @@ -65,8 +78,8 @@ public override bool CanReset /// public override void Reset() { - _source1.Reset(); - _source2.Reset(); + _source1?.Reset(); + _source2?.Reset(); _source1Status = null; _source2Status = null; @@ -115,16 +128,26 @@ public override void RegisterSource(OsmStreamSource source) private OsmGeo _current; private bool? _source1Status = null; // false when finished, true when there is data, null when unintialized. private bool? _source2Status = null; // false when finished, true when there is data, null when unintialized. - private ConflictResolutionType _resolutionType = ConflictResolutionType.None; + private readonly ConflictResolutionType _resolutionType; /// /// Move to the next object. /// public override bool MoveNext(bool ignoreNodes, bool ignoreWays, bool ignoreRelations) { + if (_source1 != null && _source2 == null) + { + return _source1.MoveNext(ignoreNodes, ignoreWays, ignoreRelations); + } + + if (_source1 == null && _source2 != null) + { + return _source2.MoveNext(ignoreNodes, ignoreWays, ignoreRelations); + } + OsmGeo source1Current = null; OsmGeo source2Current = null; - + // get currents or move to first. if (_source1Status == null) { diff --git a/src/OsmSharp/Streams/OsmStreamExtensions.cs b/src/OsmSharp/Streams/OsmStreamExtensions.cs index 14cc59be..6b749e6a 100755 --- a/src/OsmSharp/Streams/OsmStreamExtensions.cs +++ b/src/OsmSharp/Streams/OsmStreamExtensions.cs @@ -84,12 +84,108 @@ public static OsmStreamSource Progress(this OsmStreamSource source) return progress; } + /// + /// Merges a stream with the given other stream. + /// + public static OsmStreamSource Merge(this IEnumerable source, ConflictResolutionType resolutionType, OsmStreamSource other) + { + var merged = new OsmStreamFilterMerge(resolutionType); + merged.RegisterSource(source); + merged.RegisterSource(other); + return merged; + } + /// /// Merges the given sources into this source. /// public static OsmStreamSource Merge(this IEnumerable source, params OsmStreamSource[] sources) { - return source.Merge(ConflictResolutionType.FirstStream, new OsmEnumerableStreamSource(source)); + return source.Merge(ConflictResolutionType.FirstStream, sources); + } + + /// + /// Merges the given sources into this source. + /// + public static OsmStreamSource Merge(this IEnumerable source, ConflictResolutionType resolutionType, params OsmStreamSource[] sources) + { + if (sources.Length == 0) + { + return new OsmEnumerableStreamSource(source); + } + + var merged = new OsmStreamFilterMerge(resolutionType); + merged.RegisterSource(source); + merged.RegisterSource(sources[0]); + for (var s = 1; s < sources.Length; s++) + { + var next = new OsmStreamFilterMerge(resolutionType); + next.RegisterSource(merged); + next.RegisterSource(sources[s]); + + merged = next; + } + + return merged; + } + + /// + /// Merges all the given sources. + /// + /// The sources. + /// The resolution type. + /// A merged stream. + public static OsmStreamSource Merge(this IEnumerable> sources, + ConflictResolutionType resolutionType = ConflictResolutionType.FirstStream) + { + OsmStreamSource previous = null; + foreach (var source in sources) + { + var osmSource = new OsmEnumerableStreamSource(source); + + if (previous == null) + { + previous = osmSource; + } + else + { + var next = new OsmStreamFilterMerge(resolutionType); + next.RegisterSource(previous); + next.RegisterSource(osmSource); + + previous = next; + } + } + + return previous; + } + + /// + /// Merges all the given sources. + /// + /// The sources. + /// The resolution type. + /// A merged stream. + public static OsmStreamSource Merge(this IEnumerable sources, + ConflictResolutionType resolutionType = ConflictResolutionType.FirstStream) + { + OsmStreamSource previous = null; + foreach (var osmSource in sources) + { + if (previous == null) + { + previous = osmSource; + } + else + { + var next = new OsmStreamFilterMerge(resolutionType); + next.RegisterSource(previous); + next.RegisterSource(osmSource); + + previous = next; + } + } + + return previous; } /// @@ -97,12 +193,13 @@ public static OsmStreamSource Merge(this IEnumerable source, params OsmS /// public static OsmStreamSource Merge(this IEnumerable source, ConflictResolutionType resolutionType, params IEnumerable[] sources) { - var merge = new OsmStreamFilterMerge(resolutionType); - for(var i = 0; i < sources.Length; i++) + var osmSources = new OsmStreamSource[sources.Length]; + for (var s = 0; s < sources.Length; s++) { - merge.RegisterSource(sources[i]); + osmSources[s] = new OsmEnumerableStreamSource(sources[s]); } - return merge; + + return source.Merge(resolutionType, osmSources); } ///