// Copyright (C) Stichting Deltares 2023. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// DAM - UI 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.Collections.Generic;
using System.Linq;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
using NUnit.Framework;
using Rhino.Mocks;
namespace Deltares.Maps.Tests.Services
{
[TestFixture]
public class FeatueRepositoryExtensionsTest
{
private MockRepository mocks;
private IFeatureRepository rep1, rep2;
private LineString dikeRing;
[Test]
public void GetLineIntersectionPoints_OneValidProfileIntersects_ListContainsOneElement()
{
var profile1 = new LineString(new[]
{
new Coordinate(-3, 1),
new Coordinate(0, 1)
});
var profile2 = new LineString(new[]
{
new Coordinate(-3, -3),
new Coordinate(0, -3)
});
var geom1 = Feature.Create(profile1);
var geom2 = Feature.Create(profile2);
Expect.Call(rep1.Features).Return(new[]
{
geom1,
geom2
});
var dikeGeom = Feature.Create(dikeRing);
Expect.Call(rep2.Query((IFeature) null)).IgnoreArguments().Return(new[]
{
dikeGeom
});
mocks.ReplayAll();
IEnumerable> results = rep1.GetIntersectionPoints(rep2);
Assert.IsNotNull(results);
Assert.AreEqual(2, results.Count());
Assert.IsTrue(results.Any(r => r.Key == 1));
Assert.IsTrue(results.Any(r => r.Key == 0));
IntersectionResult result = results.GetResultsHavingCount(1).Single();
Assert.AreEqual(result.Source.Id, geom1.Id);
Assert.AreEqual(result.Target.Id, dikeGeom.Id);
Assert.AreEqual(-2, result.IntersectionPoints.ElementAt(0).X, 0.0001);
Assert.AreEqual(1, result.IntersectionPoints.ElementAt(0).Y, 0.0001);
}
[Test]
public void GetLineIntersectionPoints_TwoValidProfilesIntersects_ListContainsTwoElements()
{
var profile1 = new LineString(new[]
{
new Coordinate(-3, 1),
new Coordinate(0, 1)
});
var profile2 = new LineString(new[]
{
new Coordinate(-3, -1),
new Coordinate(0, -1)
});
var geom1 = Feature.Create(profile1);
var geom2 = Feature.Create(profile2);
Expect.Call(rep1.Features).Return(new[]
{
geom1,
geom2
});
var dikeGeom = Feature.Create(dikeRing);
Expect.Call(rep2.Query((IFeature) null)).IgnoreArguments().Return(new[]
{
dikeGeom
});
mocks.ReplayAll();
IEnumerable> results = rep1.GetIntersectionPoints(rep2);
Assert.IsNotNull(results);
Assert.AreEqual(1, results.Count());
Assert.IsTrue(results.Any(r => r.Key == 1));
Assert.AreEqual(2, results.GetResultsHavingCount(1).Count());
IntersectionResult result = results.ElementAt(0).First();
Assert.AreEqual(result.Source.Id, geom1.Id);
Assert.AreEqual(result.Target.Id, dikeGeom.Id);
Assert.AreEqual(-2, result.IntersectionPoints.ElementAt(0).X, 0.0001);
Assert.AreEqual(1, result.IntersectionPoints.ElementAt(0).Y, 0.0001);
}
[Test]
public void GetLineIntersectionPoints_ProfileIntersectsOnTwoSegements_ListContainsOneElementAndTwoIntersectionPoints()
{
var profile1 = new LineString(new[]
{
new Coordinate(-3, 1),
new Coordinate(3, 1)
});
var geom1 = Feature.Create(profile1);
Expect.Call(rep1.Features).Return(new[]
{
geom1
});
var dikeGeom = Feature.Create(dikeRing);
Expect.Call(rep2.Query((IFeature) null)).IgnoreArguments().Return(new[]
{
dikeGeom
});
mocks.ReplayAll();
IEnumerable> results = rep1.GetIntersectionPoints(rep2);
Assert.IsNotNull(results);
Assert.AreEqual(1, results.Count());
Assert.AreEqual(1, results.GetResultsHavingCount(2).Count());
IntersectionResult result = results.ElementAt(0).First();
Assert.AreEqual(result.Source.Id, geom1.Id);
Assert.AreEqual(result.Target.Id, dikeGeom.Id);
}
[Test]
public void GetLineIntersectionPoints_ProfileDoesNotIntersects_ListContainsOneElementAndZeroIntersectionPoints()
{
var profile1 = new LineString(new[]
{
new Coordinate(-3, -3),
new Coordinate(3, -3)
});
var geom1 = Feature.Create(profile1);
Expect.Call(rep1.Features).Return(new[]
{
geom1
});
var dikeGeom = Feature.Create(dikeRing);
Expect.Call(rep2.Query((IFeature) null)).IgnoreArguments().Return(new[]
{
dikeGeom
});
mocks.ReplayAll();
IEnumerable> results = rep1.GetIntersectionPoints(rep2);
Assert.IsNotNull(results);
Assert.AreEqual(1, results.Count());
Assert.AreEqual(1, results.GetResultsHavingCount(0).Count());
}
#region Setup
[SetUp]
public void FixtureSetup()
{
mocks = new MockRepository();
}
[TearDown]
public void FixtureTearDown() {}
[SetUp]
public void TestSetup()
{
rep1 = mocks.DynamicMock();
rep2 = mocks.DynamicMock();
dikeRing = new LineString(new[]
{
new Coordinate(-2, 2),
new Coordinate(2, 2),
new Coordinate(2, -2),
new Coordinate(-2, -2),
new Coordinate(-2, 2)
});
}
[TearDown]
public void TestTearDown()
{
mocks.VerifyAll();
}
#endregion
}
}