// 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 into .
///
/// 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 : IFeatureBasedMapDataConverter
where TFeatureBasedMapData : FeatureBasedMapData
where TMapFeatureLayer : FeatureLayer, IMapFeatureLayer
{
public bool CanConvertMapData(FeatureBasedMapData data)
{
return data is TFeatureBasedMapData;
}
public IMapFeatureLayer Convert(FeatureBasedMapData data)
{
ValidateParameters(data);
var layer = CreateLayer();
ConvertLayerFeaturesInternal(data, layer);
ConvertLayerPropertiesInternal(data, layer);
return layer;
}
public void ConvertLayerFeatures(FeatureBasedMapData data, IMapFeatureLayer layer)
{
ValidateParameters(data);
ConvertLayerFeaturesInternal(data, layer);
}
public void ConvertLayerProperties(FeatureBasedMapData data, IMapFeatureLayer layer)
{
ValidateParameters(data);
ConvertLayerPropertiesInternal(data, layer);
}
///
/// Creates a new .
///
/// The newly created .
protected abstract IMapFeatureLayer CreateLayer();
///
/// 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 void ValidateParameters(FeatureBasedMapData data)
{
if (data == null)
{
throw new ArgumentNullException("data", @"Null data cannot be converted into a feature layer.");
}
if (!CanConvertMapData(data))
{
var message = string.Format("The data of type {0} cannot be converted by this converter.", data.GetType());
throw new ArgumentException(message);
}
}
private void ConvertLayerFeaturesInternal(FeatureBasedMapData data, IFeatureLayer layer)
{
ClearLayerData(layer);
SetDataTableColumns(data, layer);
var columnNameLookup = GetColumnNameLookup(data);
foreach (MapFeature mapFeature in data.Features)
{
IEnumerable features = CreateFeatures(mapFeature);
foreach (var feature in features)
{
layer.DataSet.Features.Add(feature);
foreach (var attribute in mapFeature.MetaData)
{
feature.DataRow[columnNameLookup[attribute.Key].ToString()] = attribute.Value;
}
}
}
layer.DataSet.InitializeVertices();
layer.AssignFastDrawnStates();
}
private void ConvertLayerPropertiesInternal(FeatureBasedMapData data, IFeatureLayer layer)
{
layer.IsVisible = data.IsVisible;
((TMapFeatureLayer) layer).Name = data.Name;
layer.ShowLabels = data.ShowLabels;
layer.LabelLayer = GetLabelLayer(GetColumnNameLookup(data), layer.DataSet, data.ShowLabels, data.SelectedMetaDataAttribute);
layer.Symbolizer = CreateSymbolizer((TFeatureBasedMapData) data);
}
private static void ClearLayerData(IFeatureLayer layer)
{
layer.DataSet.Features.Clear();
layer.DataSet.DataTable.Reset();
}
private static void SetDataTableColumns(FeatureBasedMapData data, IFeatureLayer layer)
{
for (var i = 1; i <= data.MetaData.Count(); i++)
{
layer.DataSet.DataTable.Columns.Add(i.ToString(), typeof(string));
}
}
///
/// 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 GetColumnNameLookup(FeatureBasedMapData data)
{
return Enumerable.Range(0, data.MetaData.Count())
.ToDictionary(md => data.MetaData.ElementAt(md), mdi => mdi + 1);
}
private static MapLabelLayer GetLabelLayer(IDictionary metaDataLookup, IFeatureSet featureSet, bool showLabels, string labelToShow)
{
var labelLayer = new MapLabelLayer();
if (featureSet.DataTable.Columns.Count > 0 && showLabels)
{
labelLayer.Symbology.Categories[0].Symbolizer = new LabelSymbolizer
{
Orientation = ContentAlignment.MiddleRight,
OffsetX = 5
};
labelLayer.Symbology.Categories[0].Expression = string.Format(CultureInfo.CurrentCulture, "[{0}]", metaDataLookup[labelToShow]);
}
return labelLayer;
}
}
}