// Copyright (C) Stichting Deltares and State of the Netherlands 2025. 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.Data; using System.IO; using System.Linq; using Core.Common.Base.IO; using Core.Common.Util; using Core.Common.Util.Builders; using Core.Components.Gis.Data; using Core.Components.Gis.Features; using DotSpatial.Data; using CoreCommonUtilResources = Core.Common.Util.Properties.Resources; namespace Core.Components.Gis.IO.Readers { /// /// The base class to read data from a shapefile. /// public abstract class ShapeFileReaderBase : IDisposable { protected Shapefile ShapeFile; /// /// Creates a new instance of . /// /// The path to the shapefile. /// Thrown when is invalid. /// Thrown when /// points to a file that doesn't exist. protected ShapeFileReaderBase(string filePath) { IOUtils.ValidateFilePath(filePath); if (!File.Exists(filePath)) { string message = new FileReaderErrorMessageBuilder(filePath) .Build(CoreCommonUtilResources.Error_File_does_not_exist); throw new CriticalFileReadException(message); } FilePath = filePath; } /// /// Gets the number of features in the shapefile. /// public int GetNumberOfFeatures() { return ShapeFile?.Features.Count ?? 0; } /// /// Determines whether the specified attribute has been defined in the shapefile attributes. /// /// Name of the attribute. /// true is the attribute is defined, false otherwise. /// This check is case-sensitive. public bool HasAttribute(string attributeName) { return ShapeFile != null && ShapeFile.Attributes.Columns.Any(c => c.ColumnName == attributeName); } /// /// Reads a feature from the shapefile. /// /// The name of the . When null a default value will be set. /// The representing the shape, or /// null when at the end of the shapefile. public abstract FeatureBasedMapData ReadFeature(string name = null); /// /// Reads all features from the shapefile. /// /// The name of the . When null, a default value will be set. /// The representing the shape. public abstract FeatureBasedMapData ReadShapeFile(string name = null); /// /// Gets the feature at the given index. /// /// The index of which feature to retrieve. /// The feature. public abstract IFeature GetFeature(int index); public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Gets the path to the shapefile. /// protected string FilePath { get; } protected virtual void Dispose(bool disposing) { if (disposing) { ShapeFile?.Close(); } } /// /// Adds shapefile feature attributes to a as metadata. /// /// The whose metadata will be updated. /// The index of the feature in the shapefile on which the is based. /// Attributes with value are translated to a null value in the /// . protected void CopyMetaDataIntoFeature(MapFeature targetFeature, int sourceFeatureIndex) { DataRow dataRow = ShapeFile.GetAttributes(sourceFeatureIndex, 1).Rows[0]; List columns = ShapeFile.DataTable.Columns.OfType().ToList(); for (var i = 0; i < columns.Count; i++) { object dataRowValue = dataRow[i]; object newValue = null; Field column = columns[i]; if (!(dataRowValue is DBNull)) { if (column.TypeCharacter != 'C' && dataRowValue is string) { string nullValue = string.Join(string.Empty, Enumerable.Repeat('*', column.Length)); if (!Equals(dataRowValue, nullValue)) { newValue = dataRowValue; } } else { newValue = dataRowValue; } } targetFeature.MetaData[column.ColumnName] = newValue; } } } }