Index: Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/StochasticSoilModelReader.cs =================================================================== diff -u -rbe3fdb23bcc3b82eb8766e2cc22221862634224e -r6f3f1fabca21935a2198e59ee423f833b75a3d36 --- Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/StochasticSoilModelReader.cs (.../StochasticSoilModelReader.cs) (revision be3fdb23bcc3b82eb8766e2cc22221862634224e) +++ Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/StochasticSoilModelReader.cs (.../StochasticSoilModelReader.cs) (revision 6f3f1fabca21935a2198e59ee423f833b75a3d36) @@ -20,6 +20,7 @@ // All rights reserved. using System; +using System.Collections.Generic; using System.Data; using System.Data.SQLite; using Core.Common.Base.IO; @@ -35,9 +36,9 @@ /// public class StochasticSoilModelReader : SqLiteDatabaseReaderBase { + private readonly Dictionary soilProfile1Ds = new Dictionary(); private IDataReader dataReader; private SegmentPointReader segmentPointReader; - private SoilProfile1DReader soilProfile1DReader; /// /// Creates a new instance of , @@ -75,6 +76,7 @@ VerifyVersion(Path); VerifyConstraints(Path); InitializeReaders(); + InitializeLookups(); } /// @@ -115,12 +117,33 @@ segmentPointReader = null; } - if (soilProfile1DReader != null) + base.Dispose(disposing); + } + + private void InitializeLookups() + { + using (var soilProfile1DReader = new SoilProfile1DReader(Path)) { - soilProfile1DReader.Dispose(); - soilProfile1DReader = null; + soilProfile1DReader.Initialize(); + + while (soilProfile1DReader.HasNext) + { + try + { + SoilProfile1D soilProfile1D = soilProfile1DReader.ReadSoilProfile(); + + long soilProfileId = soilProfile1D.Id; + if (!soilProfile1Ds.ContainsKey(soilProfileId)) + { + soilProfile1Ds.Add(soilProfileId, soilProfile1D); + } + } + catch (SoilProfileReadException) + { + soilProfile1DReader.MoveNext(); + } + } } - base.Dispose(disposing); } private StochasticSoilModel TryReadStochasticSoilModel() @@ -131,16 +154,54 @@ } StochasticSoilModel stochasticSoilModel = CreateStochasticSoilModel(); - long currentSegmentSoilModelId = ReadStochasticSoilModelSegmentId(); - do + long currentSoilModelId = ReadStochasticSoilModelSegmentId(); + + stochasticSoilModel.Geometry.AddRange(segmentPointReader.ReadSegmentPoints(currentSoilModelId)); + stochasticSoilModel.StochasticSoilProfiles.AddRange(ReadStochasticSoilProfiles(currentSoilModelId)); + + return stochasticSoilModel; + } + + private IEnumerable ReadStochasticSoilProfiles(long stochasticSoilModelId) + { + while (HasNext && ReadStochasticSoilModelSegmentId() == stochasticSoilModelId) { - stochasticSoilModel.Geometry.AddRange(segmentPointReader.ReadSegmentPoints(currentSegmentSoilModelId)); + double probability = ReadStochasticSoilProfileProbability(); + + long? soilProfile1D = ReadSoilProfile1DId(); + if (soilProfile1D.HasValue) + { + if (soilProfile1Ds.ContainsKey(soilProfile1D.Value)) + { + yield return new StochasticSoilProfile(probability, soilProfile1Ds[soilProfile1D.Value]); + } + } + MoveNext(); - } while (HasNext && ReadStochasticSoilModelSegmentId() == currentSegmentSoilModelId); + } + } - return stochasticSoilModel; + private double ReadStochasticSoilProfileProbability() + { + return Convert.ToDouble(dataReader[StochasticSoilProfileTableDefinitions.Probability]); } + private long? ReadSoilProfile1DId() + { + object soilProfile1DId = dataReader[StochasticSoilProfileTableDefinitions.SoilProfile1DId]; + return soilProfile1DId == Convert.DBNull + ? (long?) null + : Convert.ToInt64(soilProfile1DId); + } + + private long? ReadSoilProfile2DId() + { + object soilProfile2DId = dataReader[StochasticSoilProfileTableDefinitions.SoilProfile2DId]; + return soilProfile2DId == Convert.DBNull + ? (long?) null + : Convert.ToInt64(soilProfile2DId); + } + private long ReadStochasticSoilModelSegmentId() { return Convert.ToInt64(dataReader[StochasticSoilModelTableDefinitions.StochasticSoilModelId]); @@ -166,9 +227,6 @@ segmentPointReader = new SegmentPointReader(Path); segmentPointReader.Initialize(); - - soilProfile1DReader = new SoilProfile1DReader(Path); - soilProfile1DReader.Initialize(); } private void MoveNext()