// 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 Core.Common.Base.Geometry;
using log4net;
using Ringtoets.Common.IO.Properties;
using Ringtoets.Common.IO.SurfaceLines;
using Ringtoets.Piping.Primitives;
namespace Ringtoets.Piping.IO.Importers
{
///
/// Extension methods for the class.
///
public static class RingtoetsPipingSurfaceLineExtensions
{
private static readonly ILog log = LogManager.GetLogger(typeof(RingtoetsPipingSurfaceLineExtensions));
///
/// Tries to set the at the location of
/// .
///
/// The to set the
/// for.
/// The point at which to set the .
private static void TrySetDitchPolderSide(this RingtoetsPipingSurfaceLine surfaceLine, Point3D point)
{
if (point != null)
{
try
{
surfaceLine.SetDitchPolderSideAt(point);
}
catch (ArgumentException e)
{
LogError(surfaceLine, e);
}
}
}
///
/// Tries to set the at the location of
/// .
///
/// The to set the
/// for.
/// The point at which to set the .
private static void TrySetBottomDitchPolderSide(this RingtoetsPipingSurfaceLine surfaceLine, Point3D point)
{
if (point != null)
{
try
{
surfaceLine.SetBottomDitchPolderSideAt(point);
}
catch (ArgumentException e)
{
LogError(surfaceLine, e);
}
}
}
///
/// Tries to set the at the location of
/// .
///
/// The to set the
/// for.
/// The point at which to set the .
private static void TrySetBottomDitchDikeSide(this RingtoetsPipingSurfaceLine surfaceLine, Point3D point)
{
if (point != null)
{
try
{
surfaceLine.SetBottomDitchDikeSideAt(point);
}
catch (ArgumentException e)
{
LogError(surfaceLine, e);
}
}
}
///
/// Tries to set the at the location of
/// .
///
/// The to set the
/// for.
/// The point at which to set the .
private static void TrySetDitchDikeSide(this RingtoetsPipingSurfaceLine surfaceLine, Point3D point)
{
if (point != null)
{
try
{
surfaceLine.SetDitchDikeSideAt(point);
}
catch (ArgumentException e)
{
LogError(surfaceLine, e);
}
}
}
///
/// Tries to set the at the location of
/// .
///
/// The to set the
/// for.
/// The point at which to set the .
/// true if the was set, false if
/// is null or there is no point in at the location
/// of .
private static void TrySetDikeToeAtRiver(this RingtoetsPipingSurfaceLine surfaceLine, Point3D point)
{
if (point != null)
{
try
{
surfaceLine.SetDikeToeAtRiverAt(point);
}
catch (ArgumentException e)
{
LogError(surfaceLine, e);
}
}
}
///
/// Tries to set the at the location of
/// .
///
/// The to set the
/// for.
/// The point at which to set the .
private static void TrySetDikeToeAtPolder(this RingtoetsPipingSurfaceLine surfaceLine, Point3D point)
{
if (point != null)
{
try
{
surfaceLine.SetDikeToeAtPolderAt(point);
}
catch (ArgumentException e)
{
LogError(surfaceLine, e);
}
}
}
///
/// Tries to set the relevant characteristic points from the
/// on the .
///
/// The surface line to set characteristic points for.
/// The characteristic points to set, if the collection is valid.
/// Thrown when is null.
/// Thrown when defines
/// a dike toe at polder side in front of the dike toe at river side.
public static void SetCharacteristicPoints(this RingtoetsPipingSurfaceLine surfaceLine, CharacteristicPoints characteristicPoints)
{
if (surfaceLine == null)
{
throw new ArgumentNullException(nameof(surfaceLine));
}
if (characteristicPoints == null)
{
return;
}
surfaceLine.ValidateDikeToesInOrder(characteristicPoints);
surfaceLine.TrySetDikeToeAtRiver(characteristicPoints.DikeToeAtRiver);
surfaceLine.TrySetDitchDikeSide(characteristicPoints.DitchDikeSide);
surfaceLine.TrySetBottomDitchDikeSide(characteristicPoints.BottomDitchDikeSide);
surfaceLine.TrySetBottomDitchPolderSide(characteristicPoints.BottomDitchPolderSide);
surfaceLine.TrySetDitchPolderSide(characteristicPoints.DitchPolderSide);
surfaceLine.TrySetDikeToeAtPolder(characteristicPoints.DikeToeAtPolder);
}
private static void LogError(RingtoetsPipingSurfaceLine surfaceLine, ArgumentException e)
{
log.ErrorFormat(Resources.SurfaceLinesCsvImporter_CharacteristicPoint_of_SurfaceLine_0_skipped_cause_1_,
surfaceLine.Name,
e.Message);
}
private static void ValidateDikeToesInOrder(this RingtoetsPipingSurfaceLine readSurfaceLine, CharacteristicPoints characteristicPoints)
{
if (characteristicPoints != null && characteristicPoints.DikeToeAtRiver != null && characteristicPoints.DikeToeAtPolder != null)
{
Point2D localDikeToeAtRiver = readSurfaceLine.GetLocalPointFromGeometry(characteristicPoints.DikeToeAtRiver);
Point2D localDikeToeAtPolder = readSurfaceLine.GetLocalPointFromGeometry(characteristicPoints.DikeToeAtPolder);
if (localDikeToeAtPolder.X <= localDikeToeAtRiver.X)
{
string message = string.Format(Resources.SurfaceLinesCsvImporter_CheckCharacteristicPoints_EntryPointL_greater_or_equal_to_ExitPointL_for_0_, characteristicPoints.Name);
throw new SurfaceLineTransformException(message);
}
}
}
}
}