// Copyright (C) Stichting Deltares 2017. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections.Generic; using System.Linq; using Ringtoets.Common.IO.Exceptions; using Ringtoets.Common.IO.SoilProfile; using Ringtoets.Piping.IO.Properties; using Ringtoets.Piping.Primitives; using RingtoetsCommonIOResources = Ringtoets.Common.IO.Properties.Resources; namespace Ringtoets.Piping.IO.SoilProfiles { /// /// Transforms generic into . /// internal static class PipingSoilProfileTransformer { /// /// Transforms the generic into a piping specific /// soil profile of type . /// /// The soil profile to use in the transformation. /// A new based on the given data. /// Thrown when is null. /// Thrown when transformation would not result /// in a valid transformed instance. public static PipingSoilProfile Transform(ISoilProfile soilProfile) { if (soilProfile == null) { throw new ArgumentNullException(nameof(soilProfile)); } var soilProfile1D = soilProfile as SoilProfile1D; if (soilProfile1D != null) { return CreatePipingSoilProfile(soilProfile1D); } var soilProfile2D = soilProfile as SoilProfile2D; if (soilProfile2D != null) { return CreatePipingSoilProfile(soilProfile2D); } string message = string.Format(RingtoetsCommonIOResources.SoilProfileTransformer_Cannot_tranform_Type_0_Only_types_Type_1_and_Type_2_are_supported, soilProfile.GetType().Name, nameof(SoilProfile1D), nameof(SoilProfile2D)); throw new ImportedDataTransformException(message); } /// /// Creates a new instances of the based on the . /// /// The soil profile to use in the transformation. /// The created . /// Thrown when: /// /// The cannot be used to determine intersections with; /// Transforming the failed. /// /// private static PipingSoilProfile CreatePipingSoilProfile(SoilProfile2D soilProfile2D) { string profileName = soilProfile2D.Name; double intersectionX = soilProfile2D.IntersectionX; if (double.IsNaN(intersectionX)) { string message = string.Format(Resources.Error_SoilProfileBuilder_cant_determine_intersect_SoilProfileName_0_at_double_NaN, profileName); throw new ImportedDataTransformException(message); } var layers = new List(); double bottom = double.MaxValue; foreach (SoilLayer2D soilLayer2D in soilProfile2D.Layers) { double newBottom; layers.AddRange(PipingSoilLayerTransformer.Transform(soilLayer2D, intersectionX, out newBottom)); bottom = Math.Min(bottom, newBottom); } try { return new PipingSoilProfile(profileName, bottom, layers, SoilProfileType.SoilProfile2D); } catch (ArgumentException e) { string message = CreateErrorMessage(soilProfile2D.Name, e.Message); throw new ImportedDataTransformException(message, e); } } /// /// Creates a based on the information of /// /// The soil profile to use in the transformation. /// The created . /// Thrown when the /// cannot be transformed in a . private static PipingSoilProfile CreatePipingSoilProfile(SoilProfile1D soilProfile1D) { IEnumerable layers = soilProfile1D.Layers.Select(PipingSoilLayerTransformer.Transform); try { return new PipingSoilProfile(soilProfile1D.Name, soilProfile1D.Bottom, layers.ToArray(), SoilProfileType.SoilProfile1D); } catch (ArgumentException e) { string message = CreateErrorMessage(soilProfile1D.Name, e.Message); throw new ImportedDataTransformException(message, e); } } private static string CreateErrorMessage(string soilProfileName, string errorMessage) { return string.Format(RingtoetsCommonIOResources.Transform_Error_occurred_when_transforming_SoilProfile_0_ErrorMessage_1_, soilProfileName, errorMessage); } } }