// 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.Globalization;
using System.IO;
using System.Linq;
using Deltares.Dam.Data.IO;
using Deltares.Maps;
namespace Deltares.Dam.Data.Exporters
{
public class ScenarioExporter : IScenarioExporter
{
const string NoRunValue = "-99999";
const string CalculationFailureValue = "-99998";
private FileInfo file;
private readonly List jobs;
public ScenarioExporter(IEnumerable jobs, string outputPath)
{
if (jobs == null)
throw new ArgumentNullException("jobs");
if (outputPath == null)
throw new ArgumentNullException("outputPath");
this.jobs = jobs.Cast().ToList();
this.file = new FileInfo(Path.Combine(outputPath, "Scenario.shp"));
}
public ScenarioExporter(IEnumerable jobs, FileInfo file) :
this(jobs.Cast(), file)
{
}
public ScenarioExporter(IEnumerable jobs, FileInfo file)
{
if (jobs == null)
throw new ArgumentNullException("jobs");
this.jobs = jobs.ToList();
this.file = file;
}
#region Implementation of IScenarioExporter
public void Export()
{
var repository = new FeatureRepository();
if (this.jobs.Count == 0)
return;
// add number to file if it already exists
if (File.Exists(file.FullName))
{
string fileName = file.FullName;
int i = 1;
while (File.Exists(fileName) && i < int.MaxValue)
{
fileName = string.Format(@"{0}\{1}({3}){2}",
Path.GetDirectoryName(file.FullName),
Path.GetFileNameWithoutExtension(file.FullName),
Path.GetExtension(file.FullName),
i++); ;
}
this.file = new FileInfo(fileName);
}
foreach (var job in this.jobs)
{
var attributes = new Dictionary();
foreach (var name in Enum.GetNames(typeof (ScenarioType)))
{
attributes.Add(name, NoRunValue);
}
if (job.HasRWScenarioResults)
{
foreach (var result in job.RWScenarioResults)
{
attributes[result.ScenarioType.ToString()] = double.IsNaN(result.SafetyFactor) ?
CalculationFailureValue : result.SafetyFactor.ToString(CultureInfo.InvariantCulture);
}
}
var feature = Feature.Create(job.Location.XRd, job.Location.YRd, attributes);
repository.Add(feature);
var exporter = new ShapeFileExporter(
Path.GetFileNameWithoutExtension(this.file.Name),
Path.GetDirectoryName(this.file.FullName),
repository);
exporter.Export();
}
}
#endregion
}
}