// Copyright (C) Stichting Deltares 2025. 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.CsvImporters;
using Deltares.Dam.Data.DataPlugins.Configuration;
using Deltares.Maps;
namespace Deltares.Dam.Data.Importers;
internal class LocationShapeFileImporter : IImporter
{
private readonly IEnumerable configuredAttributes;
private readonly ICollection locationRecords;
private readonly IList errors;
///
/// Constructs a location importer class were data will be imported from ShapeFiles
///
///
///
public LocationShapeFileImporter(IEnumerable existingLocationRecords, IEnumerable configuredAttributes)
{
this.configuredAttributes = configuredAttributes;
locationRecords = new List();
if (existingLocationRecords != null)
{
foreach (CsvImporterLocations.LocationRecord location in existingLocationRecords.Where(location => location != null))
{
locationRecords.Add(location);
}
}
errors = new List();
}
public IEnumerable ImportErrors
{
get
{
return errors;
}
}
#region Implementation of IImporter
public IEnumerable ImportedItems
{
get
{
return locationRecords;
}
}
#endregion
///
/// Creates a location shape file importer instance.
///
/// The existing location list.
/// The configured attributes.
/// The shape file location.
///
public static LocationShapeFileImporter Create(IEnumerable existingLocationRecords, IEnumerable configuredAttributes, string shapeFileLocation)
{
if (string.IsNullOrEmpty(shapeFileLocation) || shapeFileLocation.Trim() == "")
{
throw new ArgumentException("shapeFileLocation");
}
const string locationAttrId = LocationShapeFileAttributeMap.LocationAttributeId;
DataAttribute configuredAttribute = configuredAttributes.SingleOrDefault(a => a.AttributeId.Equals(locationAttrId, StringComparison.InvariantCultureIgnoreCase));
if (configuredAttribute == null)
{
return null;
}
LocationAttributeMapping locationMapping = LocationShapeFileAttributeMap.GetAttributeMapping(configuredAttribute, locationAttrId);
var file = new ShapeFileLocation(shapeFileLocation, locationMapping.File);
if (!file.Exists)
{
throw new FileNotFoundException($"The file '{file.FullPath}' is not found");
}
var importer = new LocationShapeFileImporter(existingLocationRecords, configuredAttributes)
{
Repository = FeatureRepository.CreateFromShapeFile(file)
};
return importer;
}
public void Import()
{
if (Repository == null)
{
return;
}
const string locationAttrId = LocationShapeFileAttributeMap.LocationAttributeId;
DataAttribute configuredAttribute = configuredAttributes.Single(a => a.AttributeId.Equals(locationAttrId, StringComparison.InvariantCultureIgnoreCase));
LocationAttributeMapping locationMapping = LocationShapeFileAttributeMap.GetAttributeMapping(configuredAttribute, locationAttrId);
foreach (IFeature feature in Repository.Features)
{
var locationIDValue = feature.Attributes[locationMapping.Name].ToString();
//Locations are read from shapefile after the locations have been read from locations.csv
//If a location already exists, overwrite the existing location
//otherwise create a new location
CsvImporterLocations.LocationRecord locationRecord = locationRecords.FirstOrDefault(l => l.LocationId == locationIDValue);
bool newLocation = locationRecord == null;
if (newLocation)
{
locationRecord = new CsvImporterLocations.LocationRecord();
}
locationRecord.LocationId = locationIDValue;
locationRecord.GeoX = feature.Geometry.Coordinate.X;
locationRecord.GeoY = feature.Geometry.Coordinate.Y;
if (newLocation)
{
locationRecords.Add(locationRecord);
}
}
}
internal IFeatureRepository Repository { get; set; }
}