// Copyright (C) Stichting Deltares and State of the Netherlands 2024. All rights reserved. // // This file is part of Riskeer. // // Riskeer 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 Core.Components.Gis.Theme; using DotSpatial.Controls; using DotSpatial.Data; using DotSpatial.Projections; using DotSpatial.Symbology; using GeoAPI.Geometries; 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. /// The type of category theme. public abstract class FeatureBasedMapDataConverter where TFeatureBasedMapData : FeatureBasedMapData where TMapFeatureLayer : FeatureLayer, IMapFeatureLayer where TCategoryTheme : CategoryTheme { /// /// 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(data, layer); SetDataTableColumns(data.MetaData, layer); ProjectionInfo originalLayerProjection = layer.Projection; if (originalLayerProjection == null || !originalLayerProjection.Equals(MapDataConstants.FeatureBasedMapDataCoordinateSystem)) { layer.Projection = MapDataConstants.FeatureBasedMapDataCoordinateSystem; } Dictionary attributeMapping = GetAttributeMapping(data); foreach (MapFeature mapFeature in data.Features) { IEnumerable features = CreateFeatures(mapFeature); foreach (IFeature feature in features) { AddFeatureToLayer(layer, feature, mapFeature, attributeMapping); } } layer.DataSet.InitializeVertices(); layer.DataSet.UpdateExtent(); if (originalLayerProjection != null && !originalLayerProjection.Equals(MapDataConstants.FeatureBasedMapDataCoordinateSystem)) { layer.Reproject(originalLayerProjection); } layer.AssignFastDrawnStates(); } /// /// 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; ((IMapFeatureLayer) layer).LabelLayer = GetLabelLayer(GetAttributeMapping(data), layer.DataSet, data.SelectedMetaDataAttribute); if (data.Theme != null) { layer.Symbology = CreateCategoryScheme(data); } else { layer.Symbolizer = CreateSymbolizer(data); } } /// /// Creates an of based on /// that have been defined in the coordinate system /// given by . /// /// 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); /// /// Creates a new based on . /// /// The map data to base the category on. /// The newly created . /// null should never be returned as this will break DotSpatial. protected abstract IFeatureCategory CreateDefaultCategory(TFeatureBasedMapData mapData); /// /// Converts an of to a array. /// /// The of to convert. /// The converted array. protected static Coordinate[] ConvertPoint2DElementsToCoordinates(IEnumerable points) { return points.Select(point => new Coordinate(point.X, point.Y)).ToArray(); } /// /// Creates the . /// /// A configured . /// null should never be returned as this will break DotSpatial. protected abstract IFeatureScheme CreateScheme(); /// /// Creates the based on the type of category theme. /// /// The type of category theme to create a for. /// A based on . /// null should never be returned as this will break DotSpatial. protected abstract IFeatureCategory CreateFeatureCategory(TCategoryTheme categoryTheme); private IFeatureScheme CreateCategoryScheme(TFeatureBasedMapData mapData) { IFeatureScheme scheme = CreateScheme(); scheme.ClearCategories(); scheme.AddCategory(CreateDefaultCategory(mapData)); MapTheme mapTheme = mapData.Theme; Dictionary attributeMapping = GetAttributeMapping(mapData); if (attributeMapping.ContainsKey(mapTheme.AttributeName)) { int attributeIndex = attributeMapping[mapTheme.AttributeName]; foreach (TCategoryTheme categoryTheme in mapTheme.CategoryThemes) { IFeatureCategory category = CreateFeatureCategory(categoryTheme); category.FilterExpression = CreateFilterExpression(attributeIndex, categoryTheme.Criterion); scheme.AddCategory(category); } } return scheme; } private void ClearFeatureScheme(TFeatureBasedMapData mapData, IScheme scheme) { if (mapData.Theme != null) { scheme.ClearCategories(); scheme.AddCategory(CreateDefaultCategory(mapData)); } } private static void ValidateParameters(TFeatureBasedMapData data, TMapFeatureLayer layer) { if (data == null) { throw new ArgumentNullException(nameof(data), @"Null data cannot be converted into feature layer data."); } if (layer == null) { throw new ArgumentNullException(nameof(layer), @"Null data cannot be used as conversion target."); } } private void ClearLayerData(TFeatureBasedMapData mapData, IFeatureLayer layer) { layer.DataSet.Features.Clear(); layer.DataSet.DataTable.Reset(); ClearFeatureScheme(mapData, layer.Symbology); } private static void SetDataTableColumns(IEnumerable metaData, IFeatureLayer layer) { int 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 (KeyValuePair 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; } /// /// Creates a filter expression based for an attribute and the criteria to apply. /// /// The index of the attribute in the metadata table. /// The criterion to convert to an expression. /// The filter expression based on the /// and . /// Thrown when the /// cannot be used to create a filter expression. private static string CreateFilterExpression(int attributeIndex, ValueCriterion criterion) { ValueCriterionOperator valueOperator = criterion.ValueOperator; switch (valueOperator) { case ValueCriterionOperator.EqualValue: return $"[{attributeIndex}] = '{criterion.Value}'"; case ValueCriterionOperator.UnequalValue: return $"NOT [{attributeIndex}] = '{criterion.Value}'"; default: throw new NotSupportedException(); } } } }