// Copyright (C) Stichting Deltares 2020. All rights reserved. // // This file is part of the Layer On Slope Tool. // // The Layer On Slope Tool is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero 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 Affero General Public License for more details. // // You should have received a copy of the GNU Affero 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.IO; using System.Linq; using Deltares.LayerOnSlopeTool.Data; namespace Deltares.LayerOnSlopeTool.Importer { /// /// Holds static methods for reading data from csv files. /// public class CsvImporter { /// /// Reads the locations. /// /// Name of the input folder. /// The locations. /// The errors. /// public static bool ReadLocations(string inputFolderName, out List locations, out List errors) { locations = new List(); errors = new List(); const string importFile = "slopelayers.csv"; string fullFilePath = Path.Combine(inputFolderName, importFile); try { var locationsImporter = new CsvImporterLocations(fullFilePath); if (locationsImporter.ErrorMessages.Count > 0) { errors.AddRange(locationsImporter.ErrorMessages); return false; } foreach (var locationRecord in locationsImporter.ImportedItems) { var location = new Location(); location.LocationId = locationRecord.LocationId; location.SurfacelineId = locationRecord.SurfaceLineId; location.SoilGeometryName = locationRecord.SoilGeometryName; location.XOffset = locationRecord.XOffset.GetValueOrDefault(location.XOffset); location.DikeMaterial = locationRecord.DikeEmbankmentMaterial; location.LayerMaterial = locationRecord.LayerMaterial; location.LayerThickness = locationRecord.LayerThickness; locations.Add(location); } return true; } catch (Exception e) { errors.Add("Fatal error whilst reading locations: " + e.Message); return false; } } /// /// Reads the surface lines. /// /// Name of the input folder. /// The surface lines. /// The errors. /// public static bool ReadSurfaceLines(string inputFolderName, out List surfaceLines, out List errors) { var result = ReadSurfaceLinesCsv(inputFolderName, out surfaceLines, out errors); if (result) { result = ReadCharacteristicPointsCsv(inputFolderName, surfaceLines, errors); } return result; } private static bool ReadSurfaceLinesCsv(string inputFolderName, out List surfaceLines, out List errors) { surfaceLines = new List(); errors = new List(); const string importFile = "surfacelines.csv"; string fullFilePath = Path.Combine(inputFolderName, importFile); try { var surfaceLinesImporter = new CsvImporterSurfaceLines(fullFilePath); if (surfaceLinesImporter.ErrorMessages.Count > 0) { errors.AddRange(surfaceLinesImporter.ErrorMessages); return false; } foreach (var surfaceLineRecord in surfaceLinesImporter.ImportedItems) { var surfaceLine = new SurfaceLine(); surfaceLine.SurfaceLineId = surfaceLineRecord.SurfaceLineId; foreach (var xCoor in surfaceLineRecord.Xcoors) { var surfaceLinePoint = new SurfaceLinePoint(); surfaceLinePoint.XCoordinate = xCoor; surfaceLine.SurfaceLinePoints.Add(surfaceLinePoint); surfaceLinePoint.PointType = CharacteristicPointType.None; } var index = 0; foreach (var zCoor in surfaceLineRecord.Zcoors) { surfaceLine.SurfaceLinePoints[index].ZCoordinate = zCoor; index++; } surfaceLines.Add(surfaceLine); } return true; } catch (Exception e) { errors.Add("Fatal error whilst reading surface lines: " + e.Message); return false; } } private static bool ReadCharacteristicPointsCsv(string inputFolderName, List surfaceLines, List errors) { const string importFile = "characteristicpoints.csv"; const double tolerance = 1e-4; string fullFilePath = Path.Combine(inputFolderName, importFile); try { var characteristicPointsImporter = new CsvImporterCharacteristicPoints(fullFilePath); if (characteristicPointsImporter.ErrorMessages.Count > 0) { errors.AddRange(characteristicPointsImporter.ErrorMessages); return false; } foreach (var characteristicPointRecord in characteristicPointsImporter.ImportedItems) { // See which surface line this record belongs to. var surfaceLine = surfaceLines.FirstOrDefault(s => s.SurfaceLineId == characteristicPointRecord.SurfaceLineId); if (surfaceLine == null) { errors.Add(string.Format("Characteristic Points csv has record {0} that can not be matched with a surface line.", characteristicPointRecord.SurfaceLineId)); return false; } foreach (var point in characteristicPointRecord.Points) { // ignore 'unset' points i.e. points with -1, -1, -1 as coordinates. if (!(Math.Abs(point.X + 1) < tolerance && Math.Abs(point.Y + 1) < tolerance && Math.Abs(point.Z + 1) < tolerance)) { var surfaceLinePoint = surfaceLine.GetSurfaceLinePointByLocation(point.X, point.Z); if (surfaceLinePoint == null) { errors.Add(string.Format("Characteristic Points csv has record {0} that has a point at (x = {1}, z = {2}) " + "that can not be matched with a surface line point.", characteristicPointRecord.SurfaceLineId, point.X, point.Z)); return false; } if (!(surfaceLinePoint.PointType == CharacteristicPointType.DikeTopAtPolder && point.Type == CharacteristicPointType.TrafficLoadInside)) { surfaceLinePoint.PointType = point.Type; } } } } return true; } catch (Exception e) { errors.Add("Fatal error whilst reading surface lines: " + e.Message); return false; } } } }