Beispiel #1
0
        private static bool ExportCalculationsForTargetProbability(
            Tuple <IEnumerable <HydraulicBoundaryLocationCalculation>, double> calculationsForTargetProbability,
            HydraulicBoundaryLocationCalculationsType calculationsType,
            ICollection <string> exportedCalculationFileNames,
            string folderPath)
        {
            IEnumerable <HydraulicBoundaryLocationCalculation> calculations = calculationsForTargetProbability.Item1;
            double targetProbability = calculationsForTargetProbability.Item2;

            string exportType = calculationsType == HydraulicBoundaryLocationCalculationsType.WaterLevel
                                    ? Resources.WaterLevels_DisplayName
                                    : Resources.WaveHeights_DisplayName;

            string uniqueName = NamingHelper.GetUniqueName(
                exportedCalculationFileNames, $"{exportType}_{ReturnPeriodFormattingHelper.FormatFromProbability(targetProbability)}",
                c => c);

            exportedCalculationFileNames.Add(uniqueName);

            string tempFilePath = Path.Combine(folderPath, $"{uniqueName}.{RiskeerCommonIOResources.Shape_file_filter_Extension}");

            var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilityExporter(
                calculations, tempFilePath, calculationsType);

            return(exporter.Export());
        }
 private static bool ExportLocationCalculationsForTargetProbabilities(
     IEnumerable <Tuple <IEnumerable <HydraulicBoundaryLocationCalculation>, double> > calculationsForTargetProbabilities,
     HydraulicBoundaryLocationCalculationsType calculationsType, string folderPath)
 {
     return(HydraulicBoundaryLocationCalculationsExportHelper.ExportLocationCalculationsForTargetProbabilities(
                calculationsForTargetProbabilities, calculationsType, folderPath));
 }
        /// <summary>
        /// Creates a new instance of <see cref="HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter"/>.
        /// </summary>
        /// <param name="locationCalculationsForTargetProbabilities">The collection of calculations to export.</param>
        /// <param name="calculationsType">The type of the calculations to export.</param>
        /// <param name="filePath">The path of the file to export to.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="locationCalculationsForTargetProbabilities"/>
        /// is <c>null</c>.</exception>
        /// <exception cref="InvalidEnumArgumentException">Thrown when <paramref name="calculationsType"/>
        /// is invalid.</exception>
        /// <exception cref="ArgumentException">Thrown when <paramref name="filePath"/> is invalid.</exception>
        public HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter(
            IEnumerable <Tuple <IEnumerable <HydraulicBoundaryLocationCalculation>, double> > locationCalculationsForTargetProbabilities,
            HydraulicBoundaryLocationCalculationsType calculationsType, string filePath)
        {
            if (locationCalculationsForTargetProbabilities == null)
            {
                throw new ArgumentNullException(nameof(locationCalculationsForTargetProbabilities));
            }

            if (!Enum.IsDefined(typeof(HydraulicBoundaryLocationCalculationsType), calculationsType))
            {
                throw new InvalidEnumArgumentException(nameof(calculationsType),
                                                       (int)calculationsType,
                                                       typeof(HydraulicBoundaryLocationCalculationsType));
            }

            IOUtils.ValidateFilePath(filePath);

            this.locationCalculationsForTargetProbabilities = locationCalculationsForTargetProbabilities;
            this.calculationsType = calculationsType;
            this.filePath         = filePath;
            string folderPath = Path.GetDirectoryName(filePath);

            tempFolderPath = Path.Combine(folderPath, "~temp");
        }
Beispiel #4
0
        private static string GetExpectedShapeFileName(HydraulicBoundaryLocationCalculationsType calculationsType,
                                                       double targetProbability)
        {
            string exportType = calculationsType == HydraulicBoundaryLocationCalculationsType.WaterLevel
                                    ? "Waterstanden"
                                    : "Golfhoogten";

            return($"{exportType}_{ReturnPeriodFormattingHelper.FormatFromProbability(targetProbability)}");
        }
        public void Constructor_InvalidHydraulicBoundaryLocationCalculationsType_ThrowsInvalidEnumArgumentException()
        {
            // Setup
            const HydraulicBoundaryLocationCalculationsType calculationsType = (HydraulicBoundaryLocationCalculationsType)99;

            // Call
            void Call() => new HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter(
                Enumerable.Empty <Tuple <IEnumerable <HydraulicBoundaryLocationCalculation>, double> >(), calculationsType, string.Empty);

            // Assert
            var expectedMessage = $"The value of argument 'calculationsType' ({calculationsType}) is invalid for Enum type '{nameof(HydraulicBoundaryLocationCalculationsType)}'.";
            var exception       = TestHelper.AssertThrowsArgumentExceptionAndTestMessage <InvalidEnumArgumentException>(Call, expectedMessage);

            Assert.AreEqual("calculationsType", exception.ParamName);
        }
Beispiel #6
0
        private static MapPointData CreateCalculationData(HydraulicBoundaryLocationCalculation calculation,
                                                          HydraulicBoundaryLocationCalculationsType calculationsType)
        {
            string metaDataHeader = calculationsType == HydraulicBoundaryLocationCalculationsType.WaterLevel
                                        ? Resources.HydraulicBoundaryLocationCalculationsWriter_WaterLevelCalculationType_WaterLevel_DisplayName
                                        : Resources.HydraulicBoundaryLocationCalculationsWriter_WaterLevelCalculationType_WaveHeight_DisplayName;

            return(new MapPointData(calculation.HydraulicBoundaryLocation.Name)
            {
                Features = new[]
                {
                    HydraulicBoundaryLocationMapDataFeaturesFactory.CreateHydraulicBoundaryLocationCalculationFeature(
                        calculation, metaDataHeader)
                }
            });
        }
        public void WriteHydraulicBoundaryLocationCalculations_InvalidHydraulicBoundaryLocationCalculationsType_ThrowsInvalidEnumArgumentException()
        {
            // Setup
            const HydraulicBoundaryLocationCalculationsType hydraulicBoundaryLocationCalculationsType = (HydraulicBoundaryLocationCalculationsType)99;

            // Call
            void Call() => HydraulicBoundaryLocationCalculationsForTargetProbabilityWriter.WriteHydraulicBoundaryLocationCalculations(
                Enumerable.Empty <HydraulicBoundaryLocationCalculation>(), string.Empty, hydraulicBoundaryLocationCalculationsType);

            // Assert
            string expectedMessage = $"The value of argument 'calculationsType' ({hydraulicBoundaryLocationCalculationsType}) " +
                                     $"is invalid for Enum type '{nameof(HydraulicBoundaryLocationCalculationsType)}'.";
            var exception = TestHelper.AssertThrowsArgumentExceptionAndTestMessage <InvalidEnumArgumentException>(Call, expectedMessage);

            Assert.AreEqual("calculationsType", exception.ParamName);
        }
        public void Constructor_InvalidHydraulicBoundaryLocationCalculationsType_ThrowsInvalidEnumArgumentException()
        {
            // Setup
            string filePath = TestHelper.GetScratchPadPath(Path.Combine("export", "test.shp"));
            const HydraulicBoundaryLocationCalculationsType hydraulicBoundaryLocationCalculationsType = (HydraulicBoundaryLocationCalculationsType)99;

            // Call
            void Call() => new HydraulicBoundaryLocationCalculationsForTargetProbabilityExporter(
                Enumerable.Empty <HydraulicBoundaryLocationCalculation>(), filePath, hydraulicBoundaryLocationCalculationsType);

            // Assert
            string expectedMessage = $"The value of argument 'calculationsType' ({hydraulicBoundaryLocationCalculationsType}) " +
                                     $"is invalid for Enum type '{nameof(HydraulicBoundaryLocationCalculationsType)}'.";
            var exception = TestHelper.AssertThrowsArgumentExceptionAndTestMessage <InvalidEnumArgumentException>(Call, expectedMessage);

            Assert.AreEqual("calculationsType", exception.ParamName);
        }
        public void Export_WithHydraulicBoundaryLocationCalculationsForTargetProbabilities_WritesFilesAndReturnsTrue(
            HydraulicBoundaryLocationCalculationsType calculationsType, string expectedCalculationsTypeName)
        {
            // Setup
            string directoryPath = TestHelper.GetScratchPadPath(nameof(Export_WithHydraulicBoundaryLocationCalculationsForTargetProbabilities_WritesFilesAndReturnsTrue));

            Directory.CreateDirectory(directoryPath);
            string filePath = Path.Combine(directoryPath, "export.zip");

            var random = new Random(21);

            var calculationsForTargetProbabilities = new[]
            {
                new Tuple <IEnumerable <HydraulicBoundaryLocationCalculation>, double>(
                    new List <HydraulicBoundaryLocationCalculation>(), random.NextDouble(0, 0.1)),
                new Tuple <IEnumerable <HydraulicBoundaryLocationCalculation>, double>(
                    new List <HydraulicBoundaryLocationCalculation>(), random.NextDouble(0, 0.01))
            };

            var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter(
                calculationsForTargetProbabilities, calculationsType, filePath);

            try
            {
                // Call
                bool isExported = exporter.Export();

                // Assert
                Assert.IsTrue(isExported);

                string[] expectedFiles =
                {
                    $"{expectedCalculationsTypeName}_{GetReturnPeriodText(calculationsForTargetProbabilities.First().Item2)}.shp",
                    $"{expectedCalculationsTypeName}_{GetReturnPeriodText(calculationsForTargetProbabilities.Last().Item2)}.shp"
                };

                using (ZipArchive zipArchive = ZipFile.OpenRead(filePath))
                {
                    CollectionAssert.IsSubsetOf(expectedFiles, zipArchive.Entries.Select(e => e.FullName));
                }
            }
            finally
            {
                DirectoryHelper.TryDelete(directoryPath);
            }
        }
        /// <summary>
        /// Creates a new instance of <see cref="HydraulicBoundaryLocationCalculationsForTargetProbabilityExporter"/>.
        /// </summary>
        /// <param name="calculations">The calculations to export.</param>
        /// <param name="filePath">The path of the file to export to.</param>
        /// <param name="calculationsType">The type of calculations.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="calculations"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Thrown when <paramref name="filePath"/> is invalid.</exception>
        /// <exception cref="InvalidEnumArgumentException">Thrown when the <see cref="calculationsType"/>
        /// is an invalid value.</exception>
        public HydraulicBoundaryLocationCalculationsForTargetProbabilityExporter(IEnumerable <HydraulicBoundaryLocationCalculation> calculations,
                                                                                 string filePath, HydraulicBoundaryLocationCalculationsType calculationsType)
        {
            if (calculations == null)
            {
                throw new ArgumentNullException(nameof(calculations));
            }

            IOUtils.ValidateFilePath(filePath);

            if (!Enum.IsDefined(typeof(HydraulicBoundaryLocationCalculationsType), calculationsType))
            {
                throw new InvalidEnumArgumentException(nameof(calculationsType),
                                                       (int)calculationsType,
                                                       typeof(HydraulicBoundaryLocationCalculationsType));
            }

            this.calculations     = calculations;
            this.filePath         = filePath;
            this.calculationsType = calculationsType;
        }
        public void Export_ValidData_ReturnsTrueAndWritesCorrectData(HydraulicBoundaryLocationCalculationsType calculationsType,
                                                                     string expectedExportFileName)
        {
            // Setup
            const string fileName = "test";

            string directoryPath = TestHelper.GetScratchPadPath(nameof(Export_ValidData_ReturnsTrueAndWritesCorrectData));

            Directory.CreateDirectory(directoryPath);
            string filePath = Path.Combine(directoryPath, $"{fileName}.shp");

            var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilityExporter(new[]
            {
                new HydraulicBoundaryLocationCalculation(new HydraulicBoundaryLocation(123, "aName", 1.1, 2.2))
            }, filePath, calculationsType);

            // Precondition
            FileTestHelper.AssertEssentialShapefilesExist(directoryPath, fileName, false);

            try
            {
                // Call
                bool isExported = exporter.Export();

                // Assert
                FileTestHelper.AssertEssentialShapefilesExist(directoryPath, fileName, true);
                FileTestHelper.AssertEssentialShapefileMd5Hashes(
                    directoryPath, fileName,
                    Path.Combine(TestHelper.GetTestDataPath(TestDataPath.Riskeer.Integration.IO),
                                 nameof(HydraulicBoundaryLocationCalculationsForTargetProbabilityExporter)),
                    expectedExportFileName, 28, 8, 628);
                Assert.IsTrue(isExported);
            }
            finally
            {
                DirectoryHelper.TryDelete(directoryPath);
            }
        }
Beispiel #12
0
        /// <summary>
        /// Exports the location calculations for a collection of target probabilities.
        /// </summary>
        /// <param name="calculationsForTargetProbabilities">The collection of calculations to export.</param>
        /// <param name="calculationsType">The type of the calculations.</param>
        /// <param name="folderPath">The path of the folder to export to.</param>
        /// <returns><c>true</c> when the export was successful; <c>false</c> otherwise.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="calculationsForTargetProbabilities"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Thrown when <paramref name="folderPath"/> is invalid.</exception>
        /// <exception cref="InvalidEnumArgumentException">Thrown when <paramref name="calculationsType"/>
        /// is invalid.</exception>
        public static bool ExportLocationCalculationsForTargetProbabilities(
            IEnumerable <Tuple <IEnumerable <HydraulicBoundaryLocationCalculation>, double> > calculationsForTargetProbabilities,
            HydraulicBoundaryLocationCalculationsType calculationsType,
            string folderPath)
        {
            if (calculationsForTargetProbabilities == null)
            {
                throw new ArgumentNullException(nameof(calculationsForTargetProbabilities));
            }

            if (!Enum.IsDefined(typeof(HydraulicBoundaryLocationCalculationsType), calculationsType))
            {
                throw new InvalidEnumArgumentException(nameof(calculationsType),
                                                       (int)calculationsType,
                                                       typeof(HydraulicBoundaryLocationCalculationsType));
            }

            IOUtils.ValidateFolderPath(folderPath);

            var exportedCalculationFileNames = new List <string>();

            return(calculationsForTargetProbabilities.All(calculations => ExportCalculationsForTargetProbability(
                                                              calculations, calculationsType, exportedCalculationFileNames, folderPath)));
        }
Beispiel #13
0
        public void ExportLocationCalculationsForTargetProbabilities_ValidData_ReturnsTrueAndWritesCorrectData(HydraulicBoundaryLocationCalculationsType calculationsType,
                                                                                                               string expectedExportFileName)
        {
            // Setup
            const double targetProbability = 0.05;

            string directoryPath = TestHelper.GetScratchPadPath(nameof(ExportLocationCalculationsForTargetProbabilities_ValidData_ReturnsTrueAndWritesCorrectData));

            Directory.CreateDirectory(directoryPath);

            string shapeFileName = GetExpectedShapeFileName(calculationsType, targetProbability);

            // Precondition
            FileTestHelper.AssertEssentialShapefilesExist(directoryPath, $"{shapeFileName}.shp", false);

            try
            {
                // Call
                bool isExported = HydraulicBoundaryLocationCalculationsExportHelper.ExportLocationCalculationsForTargetProbabilities(
                    new[]
                {
                    new Tuple <IEnumerable <HydraulicBoundaryLocationCalculation>, double>(new[]
                    {
                        new HydraulicBoundaryLocationCalculation(new HydraulicBoundaryLocation(123, "aName", 1.1, 2.2))
                    }, targetProbability)
                }, calculationsType, directoryPath);

                // Assert
                Assert.IsTrue(isExported);

                FileTestHelper.AssertEssentialShapefilesExist(directoryPath, shapeFileName, true);
                FileTestHelper.AssertEssentialShapefileMd5Hashes(
                    directoryPath, shapeFileName,
                    Path.Combine(TestHelper.GetTestDataPath(TestDataPath.Riskeer.Integration.IO),
                                 nameof(HydraulicBoundaryLocationCalculationsExportHelper)),
                    expectedExportFileName, 28, 8, 628);
            }
            finally
            {
                DirectoryHelper.TryDelete(directoryPath);
            }
        }
Beispiel #14
0
        public void ExportLocationCalculationsForTargetProbabilities_DuplicateTargetProbability_ReturnsTrueAndWritesExpectedFiles(HydraulicBoundaryLocationCalculationsType calculationsType)
        {
            // Setup
            const double targetProbability = 0.00005;

            string directoryPath = TestHelper.GetScratchPadPath(nameof(ExportLocationCalculationsForTargetProbabilities_DuplicateTargetProbability_ReturnsTrueAndWritesExpectedFiles));

            Directory.CreateDirectory(directoryPath);

            string shapeFileName = GetExpectedShapeFileName(calculationsType, targetProbability);

            // Precondition
            FileTestHelper.AssertEssentialShapefilesExist(directoryPath, $"{shapeFileName}.shp", false);

            try
            {
                // Call
                bool isExported = HydraulicBoundaryLocationCalculationsExportHelper.ExportLocationCalculationsForTargetProbabilities(
                    new[]
                {
                    new Tuple <IEnumerable <HydraulicBoundaryLocationCalculation>, double>(
                        Enumerable.Empty <HydraulicBoundaryLocationCalculation>(), targetProbability),
                    new Tuple <IEnumerable <HydraulicBoundaryLocationCalculation>, double>(
                        Enumerable.Empty <HydraulicBoundaryLocationCalculation>(), targetProbability)
                }, calculationsType, directoryPath);

                // Assert
                Assert.IsTrue(isExported);

                FileTestHelper.AssertEssentialShapefilesExist(directoryPath, shapeFileName, true);
                FileTestHelper.AssertEssentialShapefilesExist(directoryPath, $"{shapeFileName} (1)", true);
            }
            finally
            {
                DirectoryHelper.TryDelete(directoryPath);
            }
        }
Beispiel #15
0
        /// <summary>
        /// Writes the collection of <see cref="HydraulicBoundaryLocationCalculation"/> as point features in a shapefile.
        /// </summary>
        /// <param name="calculations">The hydraulic boundary locations calculations to be written to file.</param>
        /// <param name="filePath">The path to the shapefile.</param>
        /// <param name="calculationsType">The type of calculations.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="calculations"/> or
        /// <paramref name="filePath"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Thrown when <paramref name="filePath"/> is invalid.</exception>
        /// <exception cref="InvalidEnumArgumentException">Thrown when the <see cref="calculationsType"/>
        /// is an invalid value.</exception>
        /// <exception cref="CriticalFileWriteException">Thrown when the shapefile cannot be written.</exception>
        public static void WriteHydraulicBoundaryLocationCalculations(IEnumerable <HydraulicBoundaryLocationCalculation> calculations,
                                                                      string filePath, HydraulicBoundaryLocationCalculationsType calculationsType)
        {
            if (calculations == null)
            {
                throw new ArgumentNullException(nameof(calculations));
            }

            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (!Enum.IsDefined(typeof(HydraulicBoundaryLocationCalculationsType), calculationsType))
            {
                throw new InvalidEnumArgumentException(nameof(calculationsType),
                                                       (int)calculationsType,
                                                       typeof(HydraulicBoundaryLocationCalculationsType));
            }

            var pointShapeFileWriter = new PointShapeFileWriter();

            foreach (MapPointData mapDataLocation in calculations.Select(c => CreateCalculationData(c, calculationsType)))
            {
                pointShapeFileWriter.CopyToFeature(mapDataLocation);
            }

            pointShapeFileWriter.SaveAs(filePath);
        }
        public void WriteHydraulicBoundaryLocationCalculations_ValidData_WritesShapeFile(HydraulicBoundaryLocationCalculationsType calculationsType,
                                                                                         string expectedExportFileName)
        {
            // Setup
            const string fileName      = "test";
            string       directoryPath = TestHelper.GetScratchPadPath(nameof(WriteHydraulicBoundaryLocationCalculations_ValidData_WritesShapeFile));
            string       filePath      = Path.Combine(directoryPath, $"{fileName}.shp");

            // Precondition
            FileTestHelper.AssertEssentialShapefilesExist(directoryPath, fileName, false);

            var calculation = new HydraulicBoundaryLocationCalculation(new TestHydraulicBoundaryLocation("location 1"))
            {
                Output = new TestHydraulicBoundaryLocationCalculationOutput(0.1)
            };

            try
            {
                // Call
                HydraulicBoundaryLocationCalculationsForTargetProbabilityWriter.WriteHydraulicBoundaryLocationCalculations(new[]
                {
                    calculation
                }, filePath, calculationsType);

                // Assert
                FileTestHelper.AssertEssentialShapefilesExist(directoryPath, fileName, true);
                FileTestHelper.AssertEssentialShapefileMd5Hashes(
                    directoryPath, fileName,
                    Path.Combine(TestHelper.GetTestDataPath(TestDataPath.Riskeer.Integration.IO),
                                 nameof(HydraulicBoundaryLocationCalculationsForTargetProbabilityWriter)),
                    expectedExportFileName, 28, 8, 628);
            }
            finally
            {
                DirectoryHelper.TryDelete(directoryPath);
            }
        }