// 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 : IMapFeatureLayer { public bool CanConvertMapData(FeatureBasedMapData data) { return data is TFeatureBasedMapData; } public IMapFeatureLayer Convert(FeatureBasedMapData data) { ValidateParameters(data); return Convert((TFeatureBasedMapData) data); } public void ConvertLayerFeatures(FeatureBasedMapData data, IFeatureLayer layer) { ValidateParameters(data); ClearLayerData(layer); SetDataTableColumns(data, layer); ConvertLayerFeatures((TFeatureBasedMapData) data, (TMapFeatureLayer) layer); layer.DataSet.InitializeVertices(); layer.AssignFastDrawnStates(); } public void ConvertLayerProperties(FeatureBasedMapData data, IFeatureLayer layer) { ValidateParameters(data); ConvertLayerProperties((TFeatureBasedMapData) data, (TMapFeatureLayer) layer); } protected abstract IEnumerable CreateFeatures(MapFeature mapFeature); /// /// Creates a based on the that was given. /// /// The data to transform into a . /// A new . protected abstract IMapFeatureLayer Convert(TFeatureBasedMapData data); /// /// Converts all feature related data from to . /// Any features already part of are cleared. /// /// The data to convert the feature related data from. /// The layer to convert the feature related data to. protected abstract void ConvertLayerFeatures(TFeatureBasedMapData data, TMapFeatureLayer layer); /// /// Converts all general properties (like and ) /// from to . /// /// The data to convert the general properties from. /// The layer to convert the general properties to. protected abstract void ConvertLayerProperties(TFeatureBasedMapData data, TMapFeatureLayer layer); /// /// 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)); } protected static void ClearLayerData(IFeatureLayer layer) { layer.DataSet.Features.Clear(); layer.DataSet.DataTable.Reset(); } protected 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)); } } protected static Dictionary GetColumnNameLookup(FeatureBasedMapData data) { return Enumerable.Range(0, data.MetaData.Count()) .ToDictionary(md => data.MetaData.ElementAt(md), mdi => mdi + 1); } /// /// Gets a new . /// /// The lookup to use for determining the label layer expression. /// The to add the to. /// Indicator whether to show the labels or not. /// The key of the attribute to show the labels for. /// A new . protected 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; } 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); } } } }