// Copyright (C) Stichting Deltares 2016. 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.Globalization;
using System.Linq;
using Core.Common.Base.Geometry;
using Core.Components.Gis.Data;
using Core.Components.Gis.Features;
using DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Symbology;
using DotSpatial.Topology;
namespace Core.Components.DotSpatial.Converter
{
///
/// Abstract base class for transforming data into data.
///
/// The type of feature based map data to convert.
/// The type of map feature layer to set the converted data to.
public abstract class FeatureBasedMapDataConverter
where TFeatureBasedMapData : FeatureBasedMapData
where TMapFeatureLayer : FeatureLayer, IMapFeatureLayer
{
///
/// Converts all feature related data from to .
///
/// The data to convert the feature related data from.
/// The layer to convert the feature related data to.
/// Thrown when or is null.
public void ConvertLayerFeatures(TFeatureBasedMapData data, TMapFeatureLayer layer)
{
ValidateParameters(data, layer);
ClearLayerData(layer);
SetDataTableColumns(data.MetaData, layer);
var attributeMapping = GetAttributeMapping(data);
foreach (MapFeature mapFeature in data.Features)
{
IEnumerable features = CreateFeatures(mapFeature);
foreach (var feature in features)
{
AddFeatureToLayer(layer, feature, mapFeature, attributeMapping);
}
}
}
///
/// Converts all general properties (like and )
/// from to .
///
/// The data to convert the general properties from.
/// The layer to convert the general properties to.
/// Thrown when or is null.
public void ConvertLayerProperties(TFeatureBasedMapData data, TMapFeatureLayer layer)
{
ValidateParameters(data, layer);
layer.IsVisible = data.IsVisible;
layer.Name = data.Name;
layer.ShowLabels = data.ShowLabels;
layer.LabelLayer = GetLabelLayer(GetAttributeMapping(data), layer.DataSet, data.SelectedMetaDataAttribute);
layer.Symbolizer = CreateSymbolizer(data);
}
///
/// Creates an of based on .
///
/// The to create features for.
/// An of .
protected abstract IEnumerable CreateFeatures(MapFeature mapFeature);
///
/// Creates a new .
///
/// The map data to create the symbolizer for.
/// The newly created .
/// Null should never be returned as this will break DotSpatial.
protected abstract IFeatureSymbolizer CreateSymbolizer(TFeatureBasedMapData mapData);
///
/// Converts an of to an
/// of .
///
/// The of to convert.
/// The converted of .
protected static IEnumerable ConvertPoint2DElementsToCoordinates(IEnumerable points)
{
return points.Select(point => new Coordinate(point.X, point.Y));
}
private static void ValidateParameters(TFeatureBasedMapData data, TMapFeatureLayer layer)
{
if (data == null)
{
throw new ArgumentNullException("data", @"Null data cannot be converted into a feature layer data.");
}
if (layer == null)
{
throw new ArgumentNullException("layer", @"Null data cannot be used as conversion target.");
}
}
private static void ClearLayerData(IFeatureLayer layer)
{
layer.DataSet.Features.Clear();
layer.DataSet.DataTable.Reset();
}
private static void SetDataTableColumns(IEnumerable metaData, IFeatureLayer layer)
{
var count = metaData.Count();
for (var i = 1; i <= count; i++)
{
layer.DataSet.DataTable.Columns.Add(i.ToString(), typeof(string));
}
}
private static void AddFeatureToLayer(TMapFeatureLayer layer, IFeature feature, MapFeature mapFeature, Dictionary attributeMapping)
{
layer.DataSet.Features.Add(feature);
AddMetaDataToFeature(feature, mapFeature, attributeMapping);
}
private static void AddMetaDataToFeature(IFeature feature, MapFeature mapFeature, Dictionary attributeMapping)
{
foreach (var attribute in mapFeature.MetaData)
{
feature.DataRow[attributeMapping[attribute.Key].ToString()] = attribute.Value;
}
}
///
/// This method is used for obtaining a mapping between map data attribute names and DotSpatial
/// attribute names. This mapping is needed because DotSpatial can't handle special characters.
///
private static Dictionary GetAttributeMapping(TFeatureBasedMapData data)
{
return Enumerable.Range(0, data.MetaData.Count())
.ToDictionary(md => data.MetaData.ElementAt(md), mdi => mdi + 1);
}
private static MapLabelLayer GetLabelLayer(IDictionary attributeMapping, IFeatureSet featureSet, string labelToShow)
{
var labelLayer = new MapLabelLayer();
if (!string.IsNullOrEmpty(labelToShow)
&& attributeMapping.ContainsKey(labelToShow)
&& featureSet.DataTable.Columns.Contains(attributeMapping[labelToShow].ToString()))
{
labelLayer.Symbology.Categories[0].Symbolizer = new LabelSymbolizer
{
Orientation = ContentAlignment.MiddleRight,
OffsetX = 5
};
labelLayer.Symbology.Categories[0].Expression = string.Format(CultureInfo.CurrentCulture, "[{0}]", attributeMapping[labelToShow]);
}
return labelLayer;
}
}
}