// Copyright (C) Stichting Deltares 2018. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// 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.IO;
using System.Linq;
using Deltares.Dam.Data.DataPlugins.Configuration;
using Deltares.Maps;
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;
}
}
}
}