// Copyright (C) Stichting Deltares 2019. All rights reserved. // // This file is part of the application DAM - Clients Library. // // DAM - UI is free software: you can redistribute it and/or modify // it under the terms of the GNU 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 General Public License for more details. // // You should have received a copy of the GNU 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 Deltares.Standard.Extensions; using Deltares.Standard.IO.DtoAssembler; using Deltares.Standard.Language; using DotSpatial.Data; using DataAttribute = Deltares.Dam.Data.DataPlugins.Configuration.DataAttribute; namespace Deltares.Dam.Data.Importers { public class LocationShapeFileImporterDotSpatialException : ApplicationException { public LocationShapeFileImporterDotSpatialException(string message) : base(message) { } } public class LocationShapeFileImporterDotSpatial { public static List ImportLocations(string fileName, IEnumerable configuredAttributes) { // Determine column headers conform the attribute mappings string locationIdHeader = LocationShapeFileAttributeMap.LocationAttributeId; string dikeRingIdHeader = LocationShapeFileAttributeMap.DikeRingAttributeId; DataAttribute configuredAttribute = configuredAttributes.SingleOrDefault(a => a.AttributeId.Equals(LocationShapeFileAttributeMap.LocationAttributeId, StringComparison.InvariantCultureIgnoreCase)); if (configuredAttribute != null) { if (configuredAttribute.AttributeName != null) { locationIdHeader = configuredAttribute.AttributeName; } } configuredAttribute = configuredAttributes.SingleOrDefault(a => a.AttributeId.Equals(LocationShapeFileAttributeMap.DikeRingAttributeId, StringComparison.InvariantCultureIgnoreCase)); if (configuredAttribute != null) { if (configuredAttribute.AttributeName != null) { dikeRingIdHeader = configuredAttribute.AttributeName; } } try { IFeatureSet fs = FeatureSet.Open(fileName); //!!! this line is necessary otherwise the code doesn't work //this will load the attribute table of the layer into memory. fs.FillAttributes(); var locations = new List(); // Determine the column indices conform the column headers int colLocationId = -1; int colDikeRingId = -1; DataColumn[] columns = fs.GetColumns(); for (int i = 0; i < columns.Count(); i++) { if (locationIdHeader.Equals(columns[i].ToString(), StringComparison.InvariantCultureIgnoreCase)) { colLocationId = i; } if (dikeRingIdHeader.Equals(columns[i].ToString(), StringComparison.InvariantCultureIgnoreCase)) { colDikeRingId = i; } } // Read all attributes and collect locations foreach (IFeature feature in fs.Features) { var location = new Location(); if (colLocationId >= 0) { location.Name = feature.DataRow.ItemArray[colLocationId].ToString(); } else { var locationIdNotFoundError = LocalizationManager.GetTranslatedText(typeof(LocationShapeFileImporterDotSpatial), "locationIdNotFoundError"); locationIdNotFoundError = String.Format(locationIdNotFoundError, locationIdHeader); throw new LocationShapeFileImporterDotSpatialException(String.Format("{0} : {1}", Path.GetFileName(fileName), locationIdNotFoundError)); } if (colDikeRingId >= 0) { location.DikeRingId = feature.DataRow.ItemArray[colDikeRingId].ToString(); } else { var dikeRingIdNotFoundError = LocalizationManager.GetTranslatedText(typeof(LocationShapeFileImporterDotSpatial), "dikeRingIdNotFoundError"); throw new LocationShapeFileImporterDotSpatialException(String.Format("{0} : {1} {2}", fileName, dikeRingIdNotFoundError, dikeRingIdHeader)); } if (location.Name.HasValidStringValue()) { locations.Add(location); } else { // Discard location: location.Dispose(); } } return locations; } finally { } } } }