// 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 Core.Common.Base; using Core.Common.Base.Data; using Ringtoets.ClosingStructures.Data; using Ringtoets.Common.Data; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.IO.FileImporters; using Ringtoets.Common.IO.FileImporters.MessageProviders; using Ringtoets.Common.IO.Structures; namespace Ringtoets.ClosingStructures.IO { /// /// Imports point shapefiles containing closing structure locations /// and csv files containing closing structure schematizations. /// public class ClosingStructuresImporter : StructuresImporter> { private readonly IStructureUpdateStrategy structureUpdateStrategy; /// /// Creates a new instance of . /// /// The closing structures to import on. /// The reference line used to check if the /// objects found in the file are intersecting it. /// The path to the file to import from. /// The message provider to provide messages during importer actions. /// The strategy to update the structures with imported data. /// Thrown when any of the input parameters is null. public ClosingStructuresImporter(StructureCollection importTarget, ReferenceLine referenceLine, string filePath, IImporterMessageProvider messageProvider, IStructureUpdateStrategy updateStrategy) : base(importTarget, referenceLine, filePath, messageProvider) { if (updateStrategy == null) { throw new ArgumentNullException(nameof(updateStrategy)); } structureUpdateStrategy = updateStrategy; } protected override IEnumerable UpdateWithCreatedStructures(ICollection structureLocations, Dictionary> groupedStructureParameterRows) { return structureUpdateStrategy.UpdateStructuresWithImportedData(ImportTarget, CreateClosingStructures(structureLocations.ToList(), groupedStructureParameterRows), FilePath); } private IEnumerable CreateClosingStructures(IEnumerable structureLocations, IDictionary> groupedStructureParameterRows) { var closingStructures = new List(); foreach (StructureLocation structureLocation in structureLocations) { string id = structureLocation.Id; List structureParameterRows = groupedStructureParameterRows.ContainsKey(id) ? groupedStructureParameterRows[id] : new List(); ValidationResult parameterRowsValidationResult = StructuresParameterRowsValidator.ValidateClosingStructuresParameters(structureParameterRows); if (!parameterRowsValidationResult.IsValid) { ThrowValidationErrorForStructure(structureLocation.Name, structureLocation.Id, parameterRowsValidationResult.ErrorMessages); } ClosingStructure closingStructure = CreateClosingStructure(structureLocation, structureParameterRows); closingStructures.Add(closingStructure); } return closingStructures; } private ClosingStructure CreateClosingStructure(StructureLocation structureLocation, IEnumerable structureParameterRows) { Dictionary rowData = structureParameterRows.ToDictionary( row => row.ParameterId, row => row, StringComparer.OrdinalIgnoreCase); string structureName = structureLocation.Name; string structureId = structureLocation.Id; var constructionProperties = new ClosingStructure.ConstructionProperties { Name = structureName, Id = structureId, Location = structureLocation.Point }; TrySetConstructionProperty((rows, key) => { constructionProperties.StorageStructureArea.Mean = (RoundedDouble) rows[key].NumericalValue; constructionProperties.StorageStructureArea.CoefficientOfVariation = GetCoefficientOfVariation(rows[key], structureName); }, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword1, structureName, structureId); TrySetConstructionProperty((rows, key) => { constructionProperties.AllowedLevelIncreaseStorage.Mean = (RoundedDouble) rows[key].NumericalValue; constructionProperties.AllowedLevelIncreaseStorage.StandardDeviation = GetStandardDeviation(rows[key], structureName); }, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword2, structureName, structureId); TrySetConstructionProperty((rows, key) => constructionProperties.StructureNormalOrientation = (RoundedDouble) rows[key].NumericalValue, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword3, structureName, structureId); TrySetConstructionProperty((rows, key) => { constructionProperties.WidthFlowApertures.Mean = (RoundedDouble) rows[key].NumericalValue; constructionProperties.WidthFlowApertures.StandardDeviation = GetStandardDeviation(rows[key], structureName); }, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword4, structureName, structureId); TrySetConstructionProperty((rows, key) => { constructionProperties.LevelCrestStructureNotClosing.Mean = (RoundedDouble) rows[key].NumericalValue; constructionProperties.LevelCrestStructureNotClosing.StandardDeviation = GetStandardDeviation(rows[key], structureName); }, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword5, structureName, structureId); TrySetConstructionProperty((rows, key) => { constructionProperties.InsideWaterLevel.Mean = (RoundedDouble) rows[key].NumericalValue; constructionProperties.InsideWaterLevel.StandardDeviation = GetStandardDeviation(rows[key], structureName); }, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword6, structureName, structureId); TrySetConstructionProperty((rows, key) => { constructionProperties.ThresholdHeightOpenWeir.Mean = (RoundedDouble) rows[key].NumericalValue; constructionProperties.ThresholdHeightOpenWeir.StandardDeviation = GetStandardDeviation(rows[key], structureName); }, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword7, structureName, structureId); TrySetConstructionProperty((rows, key) => { constructionProperties.AreaFlowApertures.Mean = (RoundedDouble) rows[key].NumericalValue; constructionProperties.AreaFlowApertures.StandardDeviation = GetStandardDeviation(rows[key], structureName); }, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword8, structureName, structureId); TrySetConstructionProperty((rows, key) => { constructionProperties.CriticalOvertoppingDischarge.Mean = (RoundedDouble) rows[key].NumericalValue; constructionProperties.CriticalOvertoppingDischarge.CoefficientOfVariation = GetCoefficientOfVariation(rows[key], structureName); }, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword9, structureName, structureId); TrySetConstructionProperty((rows, key) => { constructionProperties.FlowWidthAtBottomProtection.Mean = (RoundedDouble) rows[key].NumericalValue; constructionProperties.FlowWidthAtBottomProtection.StandardDeviation = GetStandardDeviation(rows[key], structureName); }, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword10, structureName, structureId); TrySetConstructionProperty((rows, key) => constructionProperties.ProbabilityOrFrequencyOpenStructureBeforeFlooding = rows[key].NumericalValue, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword11, structureName, structureId); TrySetConstructionProperty((rows, key) => constructionProperties.FailureProbabilityOpenStructure = rows[key].NumericalValue, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword12, structureName, structureId); TrySetConstructionProperty((rows, key) => constructionProperties.IdenticalApertures = (int) rows[key].NumericalValue, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword13, structureName, structureId); TrySetConstructionProperty((rows, key) => constructionProperties.FailureProbabilityReparation = rows[key].NumericalValue, rowData, StructureFilesKeywords.ClosingStructureParameterKeyword14, structureName, structureId); TrySetConstructionProperty((rows, key) => constructionProperties.InflowModelType = GetClosingStructureInflowModelType(rows[key]), rowData, StructureFilesKeywords.ClosingStructureParameterKeyword15, structureName, structureId); return new ClosingStructure(constructionProperties); } private static ClosingStructureInflowModelType GetClosingStructureInflowModelType( StructuresParameterRow structureParameterRow) { string keywordValue = structureParameterRow.AlphanumericValue.ToLower(); switch (keywordValue) { case StructureFilesKeywords.InflowModelTypeVerticalWall: return ClosingStructureInflowModelType.VerticalWall; case StructureFilesKeywords.InflowModelTypeLowSill: return ClosingStructureInflowModelType.LowSill; case StructureFilesKeywords.InflowModelTypeFloodedCulvert: return ClosingStructureInflowModelType.FloodedCulvert; default: throw new NotSupportedException(); } } } }