// 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.Data; using System.IO; using System.Linq; using Core.Common.IO.Exceptions; using Core.Common.Utils; using Core.Common.Utils.Builders; using Core.Components.Gis.Data; using Core.Components.Gis.Features; using DotSpatial.Data; using CoreCommonUtilsResources = Core.Common.Utils.Properties.Resources; using GisIOResources = Core.Components.Gis.IO.Properties.Resources; namespace Core.Components.Gis.IO.Readers { /// /// The base class to read data from a shapefile. /// public abstract class ShapeFileReaderBase : IDisposable { protected readonly string FilePath; 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) { FileUtils.ValidateFilePath(filePath); if (!File.Exists(filePath)) { string message = new FileReaderErrorMessageBuilder(filePath) .Build(CoreCommonUtilsResources.Error_File_does_not_exist); throw new CriticalFileReadException(message); } FilePath = filePath; } /// /// Gets the number of features in the shapefile. /// public int GetNumberOfFeatures() { return ShapeFile != null ? 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() { if (ShapeFile != null) { 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. protected void CopyMetaDataIntoFeature(MapFeature targetFeature, int sourceFeatureIndex) { DataTable table = ShapeFile.GetAttributes(sourceFeatureIndex, 1); DataRow dataRow = table.Rows[0]; for (int i = 0; i < table.Columns.Count; i++) { targetFeature.MetaData[table.Columns[i].ColumnName] = dataRow[i]; } } } }