using System; using System.Collections.Generic; using System.IO; using System.Linq; using Deltares.Dam.Data.DataPlugins.Configuration; using Deltares.Maps; using GeoAPI.Geometries; namespace Deltares.Dam.Data.Importers { internal class LocationShapeFileImporter : IImporter { /// /// Creates a location shape file importer instance. /// /// The existing location list. /// The configured attributes. /// The shape file location. /// public static LocationShapeFileImporter Create(IEnumerable existingLocationList, IEnumerable configuredAttributes, string shapeFileLocation) { if (string.IsNullOrEmpty(shapeFileLocation) || shapeFileLocation.Trim() == "") throw new ArgumentException("shapeFileLocation"); const string locationAttrId = LocationShapeFileAttributeMap.LocationAttributeId; var configuredAttribute = configuredAttributes.SingleOrDefault(a => a.AttributeId.Equals(locationAttrId, StringComparison.InvariantCultureIgnoreCase)); if (configuredAttribute == null) return null; var locationMapping = LocationShapeFileAttributeMap.GetAttributeMapping(configuredAttribute, locationAttrId); var file = new ShapeFileLocation(shapeFileLocation, locationMapping.File); if (!file.Exists) throw new FileNotFoundException(string.Format("The file '{0}' is not found", file.FullPath)); var importer = new LocationShapeFileImporter(existingLocationList, configuredAttributes) { Repository = FeatureRepository.CreateFromShapeFile(file) }; return importer; } private readonly IEnumerable configuredAttributes; private readonly ICollection locations; private readonly IList errors; /// /// Constructs a location importer class were data will be imported from ShapeFiles /// /// /// public LocationShapeFileImporter(IEnumerable existingLocationList, IEnumerable configuredAttributes) { this.configuredAttributes = configuredAttributes; this.locations = new List(); if (existingLocationList != null) { foreach (var location in existingLocationList.Where(location => location != null)) { this.locations.Add(location); } } this.errors = new List(); } public IEnumerable ImportErrors { get { return this.errors; } } internal IFeatureRepository Repository { get; set; } #region Implementation of IImporter public IEnumerable ImportedItems { get { return locations; } } #endregion public void Import() { if (Repository == null) return; const string locationAttrId = LocationShapeFileAttributeMap.LocationAttributeId; var configuredAttribute = configuredAttributes.Single(a => a.AttributeId.Equals(locationAttrId, StringComparison.InvariantCultureIgnoreCase)); var locationMapping = LocationShapeFileAttributeMap.GetAttributeMapping(configuredAttribute, locationAttrId); const string damTypeAttrId = LocationShapeFileAttributeMap.DamTypeAttributeId; configuredAttribute = configuredAttributes.SingleOrDefault(a => a.AttributeId.Equals(damTypeAttrId, StringComparison.InvariantCultureIgnoreCase)); var damTypeMapping = LocationShapeFileAttributeMap.GetAttributeMapping(configuredAttribute, damTypeAttrId); const string dikeRingAttrId = LocationShapeFileAttributeMap.DikeRingAttributeId; configuredAttribute = configuredAttributes.SingleOrDefault(a => a.AttributeId.Equals(dikeRingAttrId, StringComparison.InvariantCultureIgnoreCase)); var dikeRingMapping = LocationShapeFileAttributeMap.GetAttributeMapping(configuredAttribute, dikeRingAttrId); foreach (var feature in Repository.Features) { var locationIDValue = feature.Attributes[locationMapping.Name].ToString(); var dikeRingIdValue = feature.Attributes[dikeRingMapping.Name].ToString(); var damTypeValue = LocationImportHelper.ToDamType(feature.Attributes[damTypeMapping.Name]); var location = this.locations.FirstOrDefault(l => l.Name == locationIDValue); if (location == null) { location = new Location(); location.SetDefaultValues(damTypeValue, Location.DamProjectType); this.locations.Add(location); } location.Name = locationIDValue; location.DamType = damTypeValue; location.DikeRingId = dikeRingIdValue; location.XRd = feature.Geometry.Coordinate.X; location.YRd = feature.Geometry.Coordinate.Y; } } } }