// 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 Lesser 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser 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.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using Core.Common.Base.Geometry;
using Core.Components.DotSpatial.Converter;
using Core.Components.Gis.Data;
using Core.Components.Gis.Features;
using Core.Components.Gis.Geometries;
using Core.Components.Gis.Style;
using Core.Components.Gis.Theme;
using Core.Components.Gis.Theme.Criteria;
using DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Symbology;
using DotSpatial.Topology;
using NUnit.Framework;
using LineStyle = Core.Components.Gis.Style.LineStyle;
namespace Core.Components.DotSpatial.Test.Converter
{
[TestFixture]
public class MapLineDataConverterTest
{
[Test]
public void DefaultConstructor_IsMapLineDataConverter()
{
// Call
var converter = new MapLineDataConverter();
// Assert
Assert.IsInstanceOf>(converter);
}
[Test]
public void ConvertLayerFeatures_MapLineDataWithMultipleFeatures_ConvertsAllFeaturesToMapLineLayer()
{
// Setup
var converter = new MapLineDataConverter();
var mapLineLayer = new MapLineLayer();
var mapLineData = new MapLineData("test")
{
Features = new[]
{
new MapFeature(Enumerable.Empty()),
new MapFeature(Enumerable.Empty()),
new MapFeature(Enumerable.Empty())
}
};
// Call
converter.ConvertLayerFeatures(mapLineData, mapLineLayer);
// Assert
Assert.AreEqual(mapLineData.Features.Count(), mapLineLayer.DataSet.Features.Count);
}
[Test]
public void ConvertLayerFeatures_MapLineDataWithSingleGeometryFeature_ConvertsFeaturesToMapLineLayerAsLineStringData()
{
// Setup
var converter = new MapLineDataConverter();
var mapLineLayer = new MapLineLayer();
var random = new Random(21);
var mapFeature = new MapFeature(new[]
{
new MapGeometry(new[]
{
new[]
{
new Point2D(random.NextDouble(), random.NextDouble()),
new Point2D(random.NextDouble(), random.NextDouble()),
new Point2D(random.NextDouble(), random.NextDouble())
}
})
});
var mapLineData = new MapLineData("test data")
{
Features = new[]
{
mapFeature
}
};
// Call
converter.ConvertLayerFeatures(mapLineData, mapLineLayer);
// Assert
IFeature feature = mapLineLayer.DataSet.Features[0];
Assert.AreEqual(mapLineData.Features.Count(), mapLineLayer.DataSet.Features.Count);
Assert.IsInstanceOf(feature.BasicGeometry);
IEnumerable expectedCoordinates = mapFeature.MapGeometries.ElementAt(0).PointCollections.ElementAt(0)
.Select(p => new Coordinate(p.X, p.Y));
CollectionAssert.AreEqual(expectedCoordinates, feature.Coordinates);
}
[Test]
public void ConvertLayerFeatures_MapLineDataWithMultipleGeometryFeature_ConvertsFeaturesToMapLineLayerAsMultiLineStringData()
{
// Setup
var converter = new MapLineDataConverter();
var mapLineLayer = new MapLineLayer();
var random = new Random(21);
var mapFeature = new MapFeature(new[]
{
new MapGeometry(new[]
{
new[]
{
new Point2D(random.NextDouble(), random.NextDouble()),
new Point2D(random.NextDouble(), random.NextDouble()),
new Point2D(random.NextDouble(), random.NextDouble())
}
}),
new MapGeometry(new[]
{
new[]
{
new Point2D(random.NextDouble(), random.NextDouble()),
new Point2D(random.NextDouble(), random.NextDouble()),
new Point2D(random.NextDouble(), random.NextDouble())
}
})
});
var mapLineData = new MapLineData("test data")
{
Features = new[]
{
mapFeature
}
};
// Call
converter.ConvertLayerFeatures(mapLineData, mapLineLayer);
// Assert
IFeature feature = mapLineLayer.DataSet.Features[0];
Assert.AreEqual(mapLineData.Features.Count(), mapLineLayer.DataSet.Features.Count);
Assert.IsInstanceOf(feature.BasicGeometry);
IEnumerable expectedCoordinates = mapFeature.MapGeometries.SelectMany(mg => mg.PointCollections.ElementAt(0).Select(p => new Coordinate(p.X, p.Y)));
CollectionAssert.AreEqual(expectedCoordinates, feature.Coordinates);
}
[Test]
[Combinatorial]
public void ConvertLayerProperties_MapLineDataWithStyle_ConvertsStyleToMapLineLayer(
[Values(KnownColor.AliceBlue, KnownColor.Azure)] KnownColor color,
[Values(1, 5)] int width,
[Values(LineDashStyle.Solid, LineDashStyle.Dash)] LineDashStyle lineStyle)
{
// Setup
Color expectedColor = Color.FromKnownColor(color);
var converter = new MapLineDataConverter();
var mapLineLayer = new MapLineLayer();
var mapLineData = new MapLineData("test", new LineStyle
{
Color = expectedColor,
Width = width,
DashStyle = lineStyle
});
// Call
converter.ConvertLayerProperties(mapLineData, mapLineLayer);
// Assert
AssertAreEqual(new LineSymbolizer(expectedColor, expectedColor, width, MapDataHelper.Convert(lineStyle), LineCap.Round), mapLineLayer.Symbolizer);
}
[Test]
public void ConvertLayerProperties_MapLineDataWithStyleAndValueCriteria_ConvertDataToMapPointLayer()
{
// Setup
const string metadataAttribute = "Meta";
var random = new Random(21);
var unequalCriteria = new ValueCriteria(ValueCriteriaOperator.UnequalValue,
random.NextDouble());
var equalCriteria = new ValueCriteria(ValueCriteriaOperator.EqualValue,
random.NextDouble());
var theme = new MapTheme(metadataAttribute, new[]
{
new CategoryTheme(Color.FromKnownColor(random.NextEnum()),
equalCriteria),
new CategoryTheme(Color.FromKnownColor(random.NextEnum()),
unequalCriteria)
});
var lineStyle = new LineStyle
{
Color = Color.FromKnownColor(random.NextEnum()),
Width = random.Next(1, 48),
DashStyle = LineDashStyle.DashDotDot
};
var mapLineData = new MapLineData("test", lineStyle)
{
Features = new[]
{
CreateMapFeatureWithMetaData(metadataAttribute)
},
MapTheme = theme
};
var mapLineLayer = new MapLineLayer();
var converter = new MapLineDataConverter();
// Call
converter.ConvertLayerProperties(mapLineData, mapLineLayer);
// Assert
const DashStyle expectedDashStyle = DashStyle.DashDotDot;
ILineSymbolizer expectedSymbolizer = CreateExpectedSymbolizer(lineStyle,
expectedDashStyle,
lineStyle.Color);
ILineScheme appliedScheme = mapLineLayer.Symbology;
Assert.AreEqual(3, appliedScheme.Categories.Count);
ILineCategory baseCategory = appliedScheme.Categories[0];
AssertAreEqual(expectedSymbolizer, baseCategory.Symbolizer);
Assert.IsNull(baseCategory.FilterExpression);
ILineCategory equalSchemeCategory = appliedScheme.Categories[1];
string expectedFilter = $"[1] = {equalCriteria.Value}";
Assert.AreEqual(expectedFilter, equalSchemeCategory.FilterExpression);
expectedSymbolizer = CreateExpectedSymbolizer(lineStyle,
expectedDashStyle,
theme.CategoryThemes.ElementAt(0).Color);
AssertAreEqual(expectedSymbolizer, equalSchemeCategory.Symbolizer);
ILineCategory unEqualSchemeCategory = appliedScheme.Categories[2];
expectedFilter = $"[1] != {unequalCriteria.Value}";
Assert.AreEqual(expectedFilter, unEqualSchemeCategory.FilterExpression);
expectedSymbolizer = CreateExpectedSymbolizer(lineStyle,
expectedDashStyle,
theme.CategoryThemes.ElementAt(1).Color);
AssertAreEqual(expectedSymbolizer, unEqualSchemeCategory.Symbolizer);
}
[Test]
public void ConvertLayerProperties_MapLineDataWithStyleAndRangeCriteria_ConvertDataToMapPointLayer()
{
// Setup
const string metadataAttribute = "Meta";
var random = new Random(21);
var allBoundsInclusiveCriteria = new RangeCriteria(RangeCriteriaOperator.AllBoundsInclusive,
random.NextDouble(),
1 + random.NextDouble());
var lowerBoundInclusiveCriteria = new RangeCriteria(RangeCriteriaOperator.LowerBoundInclusiveUpperBoundExclusive,
random.NextDouble(),
1 + random.NextDouble());
var upperBoundInclusiveCriteria = new RangeCriteria(RangeCriteriaOperator.LowerBoundExclusiveUpperBoundInclusive,
random.NextDouble(),
1 + random.NextDouble());
var allBoundsExclusiveCriteria = new RangeCriteria(RangeCriteriaOperator.AllBoundsExclusive,
random.NextDouble(),
1 + random.NextDouble());
var theme = new MapTheme(metadataAttribute, new[]
{
new CategoryTheme(Color.FromKnownColor(random.NextEnum()),
allBoundsInclusiveCriteria),
new CategoryTheme(Color.FromKnownColor(random.NextEnum()),
lowerBoundInclusiveCriteria),
new CategoryTheme(Color.FromKnownColor(random.NextEnum()),
upperBoundInclusiveCriteria),
new CategoryTheme(Color.FromKnownColor(random.NextEnum()),
allBoundsExclusiveCriteria)
});
var lineStyle = new LineStyle
{
Color = Color.FromKnownColor(random.NextEnum()),
Width = random.Next(1, 48),
DashStyle = LineDashStyle.DashDotDot
};
var mapLineData = new MapLineData("test", lineStyle)
{
Features = new[]
{
CreateMapFeatureWithMetaData(metadataAttribute)
},
MapTheme = theme
};
var mapLineLayer = new MapLineLayer();
var converter = new MapLineDataConverter();
// Call
converter.ConvertLayerProperties(mapLineData, mapLineLayer);
// Assert
const DashStyle expectedDashStyle = DashStyle.DashDotDot;
ILineSymbolizer expectedSymbolizer = CreateExpectedSymbolizer(lineStyle,
expectedDashStyle,
lineStyle.Color);
ILineScheme appliedScheme = mapLineLayer.Symbology;
Assert.AreEqual(5, appliedScheme.Categories.Count);
ILineCategory baseCategory = appliedScheme.Categories[0];
AssertAreEqual(expectedSymbolizer, baseCategory.Symbolizer);
Assert.IsNull(baseCategory.FilterExpression);
ILineCategory allBoundsInclusiveCategory = appliedScheme.Categories[1];
string expectedFilter = $"[1] >= {allBoundsInclusiveCriteria.LowerBound} AND [1] <= {allBoundsInclusiveCriteria.UpperBound}";
Assert.AreEqual(expectedFilter, allBoundsInclusiveCategory.FilterExpression);
expectedSymbolizer = CreateExpectedSymbolizer(lineStyle,
expectedDashStyle,
theme.CategoryThemes.ElementAt(0).Color);
AssertAreEqual(expectedSymbolizer, allBoundsInclusiveCategory.Symbolizer);
ILineCategory lowerBoundInclusiveCategory = appliedScheme.Categories[2];
expectedFilter = $"[1] >= {lowerBoundInclusiveCriteria.LowerBound} AND [1] < {lowerBoundInclusiveCriteria.UpperBound}";
Assert.AreEqual(expectedFilter, lowerBoundInclusiveCategory.FilterExpression);
expectedSymbolizer = CreateExpectedSymbolizer(lineStyle,
expectedDashStyle,
theme.CategoryThemes.ElementAt(1).Color);
AssertAreEqual(expectedSymbolizer, lowerBoundInclusiveCategory.Symbolizer);
ILineCategory upperBoundInclusiveCategory = appliedScheme.Categories[3];
expectedFilter = $"[1] > {upperBoundInclusiveCriteria.LowerBound} AND [1] <= {upperBoundInclusiveCriteria.UpperBound}";
Assert.AreEqual(expectedFilter, upperBoundInclusiveCategory.FilterExpression);
expectedSymbolizer = CreateExpectedSymbolizer(lineStyle,
expectedDashStyle,
theme.CategoryThemes.ElementAt(2).Color);
AssertAreEqual(expectedSymbolizer, upperBoundInclusiveCategory.Symbolizer);
ILineCategory allBoundsExclusiveCategory = appliedScheme.Categories[4];
expectedFilter = $"[1] > {allBoundsExclusiveCriteria.LowerBound} AND [1] < {allBoundsExclusiveCriteria.UpperBound}";
Assert.AreEqual(expectedFilter, allBoundsExclusiveCategory.FilterExpression);
expectedSymbolizer = CreateExpectedSymbolizer(lineStyle,
expectedDashStyle,
theme.CategoryThemes.ElementAt(3).Color);
AssertAreEqual(expectedSymbolizer, allBoundsExclusiveCategory.Symbolizer);
}
private static ILineSymbolizer CreateExpectedSymbolizer(LineStyle expectedLineStyle,
DashStyle expectedDashStyle,
Color expectedColor)
{
return new LineSymbolizer(expectedColor, expectedColor, expectedLineStyle.Width, expectedDashStyle, LineCap.Round);
}
private static MapFeature CreateMapFeatureWithMetaData(string metadataAttributeName)
{
var random = new Random(21);
var mapFeature = new MapFeature(new[]
{
new MapGeometry(new[]
{
new[]
{
new Point2D(random.NextDouble(), random.NextDouble())
}
})
});
mapFeature.MetaData[metadataAttributeName] = new object();
return mapFeature;
}
private static void AssertAreEqual(ILineSymbolizer firstSymbolizer, ILineSymbolizer secondSymbolizer)
{
IList firstStrokes = firstSymbolizer.Strokes;
IList secondStrokes = secondSymbolizer.Strokes;
Assert.AreEqual(firstStrokes.Count, secondStrokes.Count, "Unequal amount of strokes defined.");
for (var i = 0; i < firstStrokes.Count; i++)
{
var firstStroke = (CartographicStroke) firstStrokes[i];
var secondStroke = (CartographicStroke) secondStrokes[i];
Assert.AreEqual(firstStroke.Color, secondStroke.Color);
Assert.AreEqual(firstStroke.EndCap, secondStroke.EndCap);
Assert.AreEqual(firstStroke.DashStyle, secondStroke.DashStyle);
Assert.AreEqual(firstStroke.Width, secondStroke.Width);
}
}
}
}